简体   繁体   中英

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. In the textbox properties I can see that "textbox.Changed" is using the event 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:

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

Since this is not part of the properties window you will have attach the handler as such

CustTextBox.LostFocus += new EventHandler(IDCustTextBox_LostFocus);

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