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

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