c#窗体中的CancelEventArgs跟Validating事件

想实现实时监测文本框内容,等对应文本框内容不为空时按钮可以点击

 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事件将方法填入就行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章