2013-04-11 96 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