在我的表单中加载事件我检查两个条件并给出一条消息,如果其中一个或两个都是真的但是每当隐藏主表单然后再显示该消息时,每次主表单显示之后都会发生这种情况它被隐藏所以每次都会执行Load事件。我只想在条件成立时只显示一次该消息 这是我的加载事件代码
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("يرجى مراجعة الإشعارات");
}
}
答案 0 :(得分:1)
一个非常简单的解决方案是使用标志字段。
只需在表单中添加private bool _messageWasAlreadyShown;
即可。在您的Form1_Load
事件中,您必须在显示MessageBox后将其设置为true:
if (!_messageWasAlreadyShown && (debt == true || upComing == true))
{
_messageWasAlreadyShown = true;
MessageBox.Show("يرجى مراجعة الإشعارات");
}
答案 1 :(得分:0)
bool MessageShown = false;
private void Form1_Load(object sender, EventArgs e)
{
if (MessageShown == false)
{
//Code here
}
MessageShown = true;
}