By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,614 Members | 1,620 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,614 IT Pros & Developers. It's quick & easy.

Stopping the CancelButton from closing a Dialog form

P: n/a
I have a VB.NET form which I'm displaying modally using the ShowDialog()
method.

Within the form is a Cancel button, and I've set this button into the Form's
CancelButton property so that pressing Escape automatically clicks the
button.

Within the button's Click event code I display a confirmation messagebox to
ensure the user really does want to cancel. However, regardless of what code
I put in this event, the dialog form always closes when the button is
clicked and the event code has finished executing. I've tried removing all
of the code from the event, and still clicking the button causes the form to
close.

Is there some way I can tell VB not to close the form if the user has
clicked "No" in the "Are you sure?" messagebox?

Many thanks,

--

(O)enone
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In the closing event of the form you stop the form from closing:

Private Sub frmMain_Closing(ByVal sender As System.Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
....
e.Cancel = true
....
End Sub

"Oenone" <no***@nowhere.com> schreef in bericht
news:uv**************@tk2msftngp13.phx.gbl...
I have a VB.NET form which I'm displaying modally using the ShowDialog()
method.

Within the form is a Cancel button, and I've set this button into the
Form's CancelButton property so that pressing Escape automatically clicks
the button.

Within the button's Click event code I display a confirmation messagebox
to ensure the user really does want to cancel. However, regardless of what
code I put in this event, the dialog form always closes when the button is
clicked and the event code has finished executing. I've tried removing all
of the code from the event, and still clicking the button causes the form
to close.

Is there some way I can tell VB not to close the form if the user has
clicked "No" in the "Are you sure?" messagebox?

Many thanks,

--

(O)enone

Nov 21 '05 #2

P: n/a
"Oenone" <no***@nowhere.com> schrieb:
Is there some way I can tell VB not to close the form if the user has
clicked "No" in the "Are you sure?" messagebox?


\\\
Imports System.ComponentModel
..
..
..
Private Sub Form1_Closing( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles MyBase.Closing
If _
MsgBox( _
"Really close?", _
MsgBoxStyle.YesNo Or MsgBoxStyle.Question _
) = MsgBoxResult.No _
Then
e.Cancel = True
End If
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3

P: n/a
Herfried K. Wagner [MVP] wrote:
Private Sub Form1_Closing( _

[...]

Thanks both for your answers, I'll look into getting that working.

I'm still rather surprised I can't do this within the button click event
handler itself though.

Also a rather strange feature I've found. If I set the CancelButton of a
form to point to a particular button, that button always causes the form to
unload even if I change the CancelButton property to point to a different
button (or to "(none)"). Even restarting VS.NET doesn't cure this. Is this a
bug in the WinForms engine? The only way I've found to STOP a button from
unloading once it has been associated with the CancelButton property is to
delete it and re-add it, which is a real pain.

--

(O)enone

Nov 21 '05 #4

P: n/a
Oenone,
In addition to the other comments.
I'm still rather surprised I can't do this within the button click event
handler itself though. Within the Cancel button's click event you can set Form.DialogResult to None
to cancel closing the form.

Private Sub buttonCancel_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles buttonCancel.Click
If MessageBox.Show("Are you sure?", Application.ProductName, _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) =
DialogResult.No Then
Me.DialogResult = DialogResult.None
End If
End Sub

NOTE: This is the DialogResult property on the form not the button!
Also a rather strange feature I've found. If I set the CancelButton of a
form to point to a particular button, that button always causes the form
to unload even if I change the CancelButton property to point to a
different button (or to "(none)"). Check to make sure the Cancel button's DialogResult property is not Cancel.

NOTE: This is the DialogResult property on the button not the form!

Hope this helps
Jay

"Oenone" <no***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... Herfried K. Wagner [MVP] wrote:
Private Sub Form1_Closing( _

[...]

Thanks both for your answers, I'll look into getting that working.

I'm still rather surprised I can't do this within the button click event
handler itself though.

Also a rather strange feature I've found. If I set the CancelButton of a
form to point to a particular button, that button always causes the form
to unload even if I change the CancelButton property to point to a
different button (or to "(none)"). Even restarting VS.NET doesn't cure
this. Is this a bug in the WinForms engine? The only way I've found to
STOP a button from unloading once it has been associated with the
CancelButton property is to delete it and re-add it, which is a real pain.

--

(O)enone

Nov 21 '05 #5

P: n/a
Jay B. Harlow [MVP - Outlook] wrote:
Oenone,
In addition to the other comments.

[...]

Thank you very much -- now it all makes sense. I'd completely overlooked the
DialogResult property on the buttons, having spotted that I can now see
what's going on at last.

--

(O)enone
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.