472,353 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Stopping the CancelButton from closing a Dialog form

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
5 15439
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dalan | last post by:
I designed a dialog box with a combo to select individual vendors from a form and its related data for print ouput. Though the dialog box seems to...
5
by: Peter yeshew | last post by:
Is it possible to forbid closing the form through the File- Close menu ? On my form i have a command button called CmdDeleteInvoice. When this...
2
by: Roger | last post by:
form A has a command button that opens form B in dialog mode DoCmd.openForm "frmProductPrice", acNormal, , , acFormEdit, acDialog, productId ...
14
by: Simon Abolnar | last post by:
I would like to know how to open child form from dialog form. Thanks for help! Simon
9
by: G .Net | last post by:
Hi I'm soooo sorry but I've had to re-post this message again. I have no idea what has been happening but for some reason I couldn't seem to be...
4
by: Macbane | last post by:
Hi, I have a 'main' form called frmIssues which has a subform control (named linkIssuesDrug) containing the subform sfrmLink_Issues_Drugs. A...
3
by: ApexData | last post by:
I have a Dialog PopUp Form that is called by the following code: DoCmd.OpenForm stDocName, , , stLinkCriteria, cFormReadOnly, acDialog I would...
7
by: Sin Jeong-hun | last post by:
I have a dialog form which pops up from the main window using the ShowDialog() method. this dialog has no or button, and it has quite a lot of...
3
pradeepjain
by: pradeepjain | last post by:
hii.. i use cope like this <td><a href='/login.php' id="view-user" >Pradeep</a></td> and defauly jquery dialog code <script...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.