Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate on text change in TextBox

I have implemented validation rules on a textBox in my WinForm and it works well. However it checks the validation only when I tab out of the field. I would like it to check as soon as anything is entered in the box and everytime the content changes. Also I'd like it to check validation as soon as the WinForm opens.

I remember doing this fairly recently by setting some events and whatnot, but I can't seem to remember how.

like image 934
Sakkle Avatar asked Mar 04 '09 14:03

Sakkle


4 Answers

If you're using databinding, go to the Properties of the textbox. Open (DataBindings) at the top, click on the (Advanced) property, three dots will appear (...) Click on those. The advanced data binding screen appears. For each property of the TextBox that is bound, in your case Text, you can set when the databinding, and thus the validation, should "kick in" using the combobox Data Source Update mode. If you set it to OnPropertyChanged, it will re-evaluate as you type (the default is OnValidation which only updates as you tab).

like image 131
Kurt Schelfthout Avatar answered Oct 18 '22 23:10

Kurt Schelfthout


How will your data be valid if it isn't finished? i.e. a user types a number and you try and validate it as a date?

like image 34
cjk Avatar answered Oct 18 '22 22:10

cjk


TextChanged event

in the future you can find all of the events on the MSDN library, here's the TextBox class reference:

http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox(VS.80).aspx

like image 24
Jason Avatar answered Oct 19 '22 00:10

Jason


When binding your textbox to a bindingSource go to Advanced and select validation type
"On Property Changed". This will propagate your data to your entity on each key press. Here is the screen shot

like image 1
Valentin V Avatar answered Oct 19 '22 00:10

Valentin V