C# 文本框的 LostFocus 事件问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15941562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issue with LostFocus event of a TextBox
提问by ShmuelCohen
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.
当我在CustName
文本框中键入 abc ,然后离开文本框时,我没有收到任何消息。在文本框属性中,我可以看到“textbox.Changed”正在使用事件 LostFocus。
How can I get this to show the Error message above?
我怎样才能让它显示上面的错误消息?
采纳答案by Ram Singh
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
}
回答by V4Vendetta
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 windowyou will have attach the handler as such
由于这不是属性窗口的一部分,因此您将附加处理程序
CustTextBox.LostFocus += new EventHandler(IDCustTextBox_LostFocus);