想實現實時監測文本框內容,等對應文本框內容不爲空時按鈕可以點擊
private void txtBoxEmpty_Validating(object sender, CancelEventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.Length == 0)
{
tb.BackColor = Color.Red;
}
else
{
tb.BackColor = System.Drawing.SystemColors.Window;
}
ValidateOK();
}
private void ValidateOK()
{
this.buttonOK.Enabled = (textBoxName.Text.Length != 0 && textBoxAddress.Text.Length != 0 && textBoxAge.Text.Length != 0);
}
寫完方法後當是不知道怎麼調用在Form1方法里加上this.Validating+=new CancelEventHandler(this.txtBoxAge_KeyPress);也不管用.
原來不需要這樣只要在設計窗體那裏找到對應的文本框屬性Validating事件將方法填入就行