Validating WinForms TextBox (in C#)

前端 未结 5 1823
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 14:53

In TextBox_Leave event i need to check whether numbers entered in textbox is in serial number or not.If it is not in order then i need to display a message as \"number\" is

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-07 14:57

    One trick is to retain focus in the textbox when trying to leave (with TAB for instance) in case of some condition (missing number):

     private void textBox1_Leave(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
    
            if (tb.Text == "3")
                tb.Focus();
        }
    

    Assuming you are using a standard textbox. You could also use third party controls that where you can cancel an event (e.Cancel = true) on some condition.

提交回复
热议问题