views:

289

answers:

2
+1  Q: 

Binding Textboxes

I have been binding textboxes in a winform with C# to a dataset. Whenever the data doesn't validate with what the database except it silently forces the focus to remain on the textbox. How can I catch the validation error and tell the user about it (and free the focus)? The BindingSource event OnDataError isn't fired.

+1  A: 

Never rely on just what "Visual studio has done for me" if you don't fully understand what it's doing. I would strongly urge you to take the time and figure out how to do what it is you want to do all by yourself (meaning without designer generated code). To get you started, there are some events on the TextBox that can help you out. Start here:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validated.aspx

Specifically the validating and validated events should be what you're looking for.

BFree
It's forcing me to rely on it because I don't know how to override it's controls, which is what I am asking.How do I override what it is doing>
Malfist
+1  A: 

I had a similar problem once. The focus remained in the textbox which was binded to some numeric database field when the user modified text in a textbox and then deleted it so the text property was an empty string. I solved it with something like:

textbox.DataBindings["Text"].NullValue = "";

It solved the problem for empty inputs. I don't know if it would be of any use in your case, but I'd be also interested in more general solution.

Here's also some related question on SO:

http://stackoverflow.com/questions/217912/data-bound-textbox-cant-exit

Mr. Brownstone
That other question was the answer. Thank you.
Malfist