C validating cancel button

posted by | Leave a comment

So I wrote a function which is reponsible for validation and I fire this function if user click Ok button.

The problem is that I can't stop form from closing even if validation function return false.

It happens because of the fact that I assigned acceptbutto property to my Ok button.

Is there any way to prevent form from closing if validation fails without removing acceptbutton properties ??

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.

//form init, auto-generated code (this is the case described) private void Initialize Component() //event handlers private void btn OK_Click(object sender, Event Args e) private void Form_Form Closing(object sender, Form Closing Event Args e) in the Designer or somewhere else (don't confuse with Form. In the Button's click handler do the validation and if successful set Form.

I have a Windows Form with an edit box and a Cancel button. The code is executed every time the edit box loses focus.

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

The following code example uses the derived class Text Box and validates an e-mail address that the user enters.

If the user enters invalid data they’ll see an icon shown in Figure 3.

Leave a Reply

lovedatingmarriage com