简体   繁体   中英

How to prevent Load event from execution more than one time

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("يرجى مراجعة الإشعارات");
                }
        }

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("يرجى مراجعة الإشعارات");
}
bool MessageShown = false;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM