Stopping the CancelButton from closing a Dialog form

O

Oenone

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,
 
Q

Qwert

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
 
H

Herfried K. Wagner [MVP]

Oenone said:
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
///
 
O

Oenone

Herfried said:
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.
 
J

Jay B. Harlow [MVP - Outlook]

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 said:
Herfried said:
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

Oenone

Jay said:
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.
 

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