我的textBox离开触发器两次?为什么......(winform) [英] My textBox leave triggers twice? why it is ...(winform)

查看:116
本文介绍了我的textBox离开触发器两次?为什么......(winform)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private   void  txtAccCentre_Leave(对象发​​件人,EventArgs e)
{

如果(txtAccCentre.Text.Trim()! =
{

if (txtAccCentre.Text.Length > = 3
{
txtCurrency.Text = string .Empty;
cbCurrency.DataSource = null ;
txtAccCentre.LostFocus + = new EventHandler(txtAccCentre_LostFocus);
}
else
{
string result = CustomMessageBox.ShowBox( 过滤文本长度应大于或等于3 错误 );
if (txtAccCentre.Text.Length == 0
{
txtAccCentre.Text = Code / Desc;
txtAccCentre.ForeColor = SystemColors.GrayText;
}

}
}
}







我的textBox离开触发器两次?为什么...我尝试删除

 txtAccCentre.LostFocus + =  new  EventHandler(txtAccCentre_LostFocus); 



这一行。但它存在

解决方案

因为当它触发时你正在为LostFocus链添加一个事件处理程序。

因此,下次用户离开时,LostFocus事件处理程序方法被调用两次,然后三次,然后是四次......

在设计器中设置处理程序,并检查该处理程序内部如果它需要处理而不是动态添加它。



如果这不能解决问题,请检查您认为发生的事情正是发生的事情 - 添加调试.WriteLine语句给你的每个处理程序,以便你可以确切地识别调用哪些处理程序,以及按什么顺序。



我们无法帮助你直接在这里 - 这个小片段并没有告诉我们其余的代码是如何工作的!

private void txtAccCentre_Leave(object sender, EventArgs e)
       {

           if (txtAccCentre.Text.Trim() != "")
           {

               if (txtAccCentre.Text.Length >= 3)
               {
                   txtCurrency.Text = string.Empty;
                   cbCurrency.DataSource = null;
                   txtAccCentre.LostFocus += new EventHandler(txtAccCentre_LostFocus);
               }
               else
               {
                   string result = CustomMessageBox.ShowBox("Filter text length should be greater than or equal to 3", "Error", "");
                   if (txtAccCentre.Text.Length == 0)
                   {
                       txtAccCentre.Text = "Code/Desc";
                       txtAccCentre.ForeColor = SystemColors.GrayText;
                   }

               }
           }
       }




My textBox leave triggers twice? why it is ... I tried by removing

txtAccCentre.LostFocus += new EventHandler(txtAccCentre_LostFocus);


this line.but it exists

解决方案

Because when it triggers you are adding an event handler to the LostFocus chain.
So the next time the user leaves, the LostFocus event handler method is called twice, then three times, then four...
Set the handler in the designer, and check inside that handler if it needs to process rather than adding it dynamically.

If that doesn't cure it, check that what you think is happening is exactly what is happening - add Debug.WriteLine statements to each of your handlers so that you can identify exactly which handlers are being called, and in what order.

We can't do much to help you directly here - that little fragment doesn't tell us enough about how the rest of your code works!


这篇关于我的textBox离开触发器两次?为什么......(winform)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆