2013-04-11 99 views
-3

嘿,我尝试使用此功能:问题有一个TextBox的LostFocus事件

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

好的,所以你正在尝试使用一种方法。那么,哪里出了问题?你还没有问过问题。 – 2013-04-11 05:49:01

+0

抱歉没有注意到我没有问,方法不起作用 – ShmuelCohen 2013-04-11 05:52:21

+1

请更新您的标题.. http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title – 2013-04-11 05:52:50

回答

6

有一个在属性窗口的文本框没有LostFocus事件,如果你想用这个,那么你一定需要添加事件处理程序,有在属性窗口的文本框离开时,可以用来如下:

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

添加事件处理程序,你需要写如下:

textBox1.LostFocus += new EventHandler(textBox1_LostFocus); 

那么你可以如下使用它:

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

这是不正确的[它只是没有显示在属性窗口](http://msdn.microsoft.com/en-us/library/system.componentmodel.browsableattribute.aspx) – V4Vendetta 2013-04-11 05:56:54

+1

TextBox从Control继承,因此它继承引发LostFocus。 – 2013-04-11 05:57:44

+2

您不以任何方式生成自定义事件,只是为现有事件添加处理程序 – V4Vendetta 2013-04-11 06:01:24

5

你需要让现场知道有该事件LostFocus

由于这一个处理程序not part of the properties window你会附上处理器本身

CustTextBox.LostFocus += new EventHandler(IDCustTextBox_LostFocus); 
+0

工作,谢谢 但我想问你,我想知道为什么我需要使用此处理程序,当我需要使用它和什么时候不? – ShmuelCohen 2013-04-11 06:05:00

+2

如果属性窗口中发生事件,则此部分将自动完成(您可以在窗体的designer.cs文件中看到该部分),但在这种情况下,您必须执行此操作。这实际上有助于了解“LostFocus”将由此方法处理 – V4Vendetta 2013-04-11 06:08:00