473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 15699
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 work okay, I apparently do not have the filtering...
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 command button is visible ,i want to forbid the user...
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 form B is a tabular form with 3 'required' fields ...
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 able to see messages that I'd posted earlier. ...
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 control button on the main form opens a pop-up form...
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 like to set the Visible propery of a Label in this...
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 controls on it. Now, I want to close this dialog...
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 type="text/javascript"> $(function() {...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.