Cancel a click event on a button

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.
 
F

Fabrizio Romano

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
 
G

Gallarpri

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 :)
 
M

Michael Voss

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"....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top