Cancel a click event on a button

  • Thread starter Thread starter Gallarpri
  • Start date Start date
G

Gallarpri

Hi, I'm making a dialog form with a textbox and 2 buttons: OK and
Cancel.

I want when I click on OK button, the program check the textbox, and if
there is any error in the text introduced there, then it shows a
MessageBox and cancel the click event, so the dialog form won't close.

How can I cancel the click event on a button? I have looked at
"EventArgs e", but it hasnt any property to cancel the vent.

Thank you! :)
Jose.
 
You don't need to cancel the click event.
I would do like this:
Handle the textbox validation event. If there are some error then just
inform the user, otherwise set the form's DialogResult property to Ok.
Do it manually without setting it visually, otherwise it will always close.

HTH
Fabrizio
 
Thanks for answering.

I have finally resolved it putting the DialogResult property of the OK
button to "None", so the form doesnt close now when I click on this
button. I do the check, and if all its correct, then I call
this.Close()

Hope it helps to someone too :)
 
Gallarpri wrote:

[...snip...]
I have finally resolved it putting the DialogResult property of the OK
button to "None", so the form doesnt close now when I click on this
button. I do the check, and if all its correct, then I call
this.Close()
[...snip...]

As Fabrizio mentioned, set the DialogResult property of your Form to
DialogResult.OK before closing, so you can find out wether your user clicked
on "OK" or "Cancel"....
 
Back
Top