-2

In my form Load event I check two conditions and give a message if one of them or both are true but whenever the main form is hidden and then showed up the message appears again and this happens every time the main form is showed after it was hidden so the Load event is executed each time ..I just want that message to be showed only once if the conditions are true
This is my Load event codes

 private void Form1_Load(object sender, EventArgs e)
        {
                try
                {
                    if (cn.State == ConnectionState.Closed)
                    {
                        cn.Open();
                    }
                    SqlCommand cmd = new SqlCommand("select clientData.Id,clientData.clientName,clientData.clientPhone,clientData.clientMobile,clientData.clientEmail,clientData.clientPage,marketingData.marketingBy,marketingData.marketingFor,marketingData.marketingCities,marketingData.marketingDurations,marketingData.marketingStartsFrom,marketingData.marketingEndsIn,marketingData.adDate,marketingData.adImage,priceAndProfits.marketingCost from clientData inner join marketingData on clientData.Id = marketingData.m_Id  inner join priceAndProfits on clientData.Id = priceAndProfits.p_Id where marketingData.marketingStartsFrom >= getdate()", cn);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        upComing = true;
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }

                if (DateTime.Now.Day >= 25 && DateTime.Now.Day < 31)
                {
                    try
                    {
                        if (cn.State == ConnectionState.Closed)
                        {
                            cn.Open();
                        }
                        SqlCommand cmd = new SqlCommand("select clientData.Id,clientData.clientName,clientData.clientMobile,clientData.clientPage,marketingData.marketingBy,priceAndProfits.marketingCost,priceAndProfits.marketingPrice,priceAndProfits.marketingProfit,priceAndProfits.dollarPrice,priceAndProfits.payment from clientData inner join marketingData on clientData.Id = marketingData.m_Id  inner join priceAndProfits on clientData.Id = priceAndProfits.p_Id where clientData.isDebtor=1", cn);
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        da.Fill(dt2);
                        if (dt2.Rows.Count > 0)
                        {
                            debt = true;
                        }
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                if (debt == true || upComing == true)
                {
                    MessageBox.Show("يرجى مراجعة الإشعارات");
                }
        }
Huda
  • 11
  • 4
  • 2
    How do you "hide" the form? Using `Form.Hide()`? and how do you show it again. Can you share those pieces of code. Because normally when you Hide a form and then Show it again it shouldn't trigger the Load event. – Peter Bons Oct 14 '17 at 14:26
  • 1
    It is a pretty good hint that you have a bug in your program. Like creating the form object again instead of using the existing one. Use the debugger, set a break on the Load event handler and when it hits the second time have a look-see at the Call Stack window. It shows you where the evil code is located. Instead of using Hide(), almost always a bad idea, consider just closing a form that you no longer need. [This snippet](https://stackoverflow.com/a/10769349/17034) ought to be useful to stop that from terminating your program. – Hans Passant Oct 14 '17 at 15:30
  • Oops, I meant the constructor, not the Load event. – Hans Passant Oct 21 '17 at 15:07

2 Answers2

1

A very simple solution would be to work with a flag field. Just add private bool _messageWasAlreadyShown; to your form. In your Form1_Load event you have to set it true, after your MessageBox was displayed:

if (!_messageWasAlreadyShown && (debt == true || upComing == true))
{
    _messageWasAlreadyShown = true;
    MessageBox.Show("يرجى مراجعة الإشعارات");
}
ChW
  • 3,168
  • 2
  • 21
  • 34
  • I tried this before but the message is still showing up :( – Huda Oct 14 '17 at 19:10
  • Like commented in your question I guess your Form object is reinialized every time it will be displayed after it was hidden. My solution should work anyway as long as you set the variable to static, but I would suggest you do a root cause analyse instead of setting it to static. – ChW Oct 15 '17 at 12:58
0
bool MessageShown = false;

private void Form1_Load(object sender, EventArgs e)
{
    if (MessageShown == false)
    {
        //Code here
    }
    MessageShown = true;
}