想实现实时监测文本框内容,等对应文本框内容不为空时按钮可以点击
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事件将方法填入就行