简体   繁体   English

TextBox的LostFocus事件的问题

[英]Issue with LostFocus event of a TextBox

I'm trying to use this function: 我正在尝试使用此功能:

private void IDCustTextBox_LostFocus(object sender, System.EventArgs e)
{
      if (CustName.Text == "abc")
          MessageBox.Show("Error");
}

When I type abc in CustName textbox, and then leave the textbox, I dont get any message. 当我在CustName文本框中键入abc,然后离开该文本框时,我没有收到任何消息。 In the textbox properties I can see that "textbox.Changed" is using the event LostFocus. 在文本框属性中,我可以看到“ textbox.Changed”正在使用事件LostFocus。

How can I get this to show the Error message above? 我怎样才能显示上面的错误消息?

There is no LostFocus event for textbox in property Window ,if you want to use this then you must need to add event handler, there is textbox leave event in property window, that could be used as below: 属性窗口中没有文本框的LostFocus事件,如果要使用此事件,则必须添加事件处理程序,属性窗口中有文本框离开事件,可以按如下方式使用:

private void textBox1_Leave(object sender, EventArgs e)
    {
     // do your stuff
    }

for adding event handler you need to write the following: 要添加事件处理程序,您需要编写以下内容:

textBox1.LostFocus += new EventHandler(textBox1_LostFocus);

then you can use it as below: 那么您可以按以下方式使用它:

private void textBox1_LostFocus(object sender, EventArgs e)
    {
     // do your stuff
    }

You will need to let the field know that there is a handler for the event LostFocus 您将需要让该字段知道事件LostFocus的处理程序

Since this is not part of the properties window you will have attach the handler as such 由于这不是属性窗口的一部分,因此您将像这样附加处理程序

CustTextBox.LostFocus += new EventHandler(IDCustTextBox_LostFocus);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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