Validating event of textbox in c

And, what should be Datasource Update Mode- On Property Changed or On Validation?

I want to check what the user is writing in a textbox before I save it in a database. I guess I can always write some ifs or some try-catch blocks, but I was wondering if there's a better method.

So before saving data, I want to validate whether all the required fields are filled (Text Boxes) by the user. I would rather use a generic function that would be passed each control on the form and based on the type of the control, separate validation logic would be written.

validating event of textbox in c-82

Validating event of textbox in c

Now practice your newly learned skills on your own and experiment with the error provider control!

In that case you should also somehow tell the user that there’s a problem with the field. For example, if the user presses Tab, clicks on another control, presses Enter to trigger the Accept button, or presses Esc to trigger the Cancel button.

Doing so can cause your application or the operating system to stop responding.

For more information, see the WM_KILLFOCUS topic in the "Keyboard Input Reference" section, and the "Message Deadlocks" section of the "About Messages and Message Queues" topic in the MSDN library at

Let’s assume when we click our save button that we wish to validate the controls and display an icon if there is a problem.

First, add the following code to the Form’s constructor after the Initialize Component method: This is a handy trick to prevent implicit validation of our controls when they lose focus.

Note also that the user can leave a field if it doesn’t have a This entry was posted in controls and tagged C#, C# programming, controls, example, example program, validate field, validate form, Validating event, Windows Forms programming.

In a Win Form I'm doing initial validation in the Form. To make the code short only three fields are shown in the code.

The validated event works the same, only pass it the name textbox instead.

Tags: , ,