473,574 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15732
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.Componen tModel.CancelEv entArgs) Handles MyBase.Closing
....
e.Cancel = true
....
End Sub

"Oenone" <no***@nowhere. com> schreef in bericht
news:uv******** ******@tk2msftn gp13.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.Componen tModel
..
..
..
Private Sub Form1_Closing( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles MyBase.Closing
If _
MsgBox( _
"Really close?", _
MsgBoxStyle.Yes No Or MsgBoxStyle.Que stion _
) = 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.DialogResu lt to None
to cancel closing the form.

Private Sub buttonCancel_Cl ick(ByVal sender As System.Object, _
ByVal e As System.EventArg s) Handles buttonCancel.Cl ick
If MessageBox.Show ("Are you sure?", Application.Pro ductName, _
MessageBoxButto ns.YesNo, MessageBoxIcon. Question) =
DialogResult.No Then
Me.DialogResult = DialogResult.No ne
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******** ********@TK2MSF TNGP12.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
4738
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 code behind the OnOpen Event Procedure of the report structured correctly. Anyone's assistance will be appreciated, although I would like to stay...
5
2784
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 from closing the form through the menu commands file-close.I want to make him click the button and not to allow him to do any other actions.And to...
2
1823
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 I can enter data in the three fields and save them - no problem Once I dirty a record in the dialog form, I get the little 'pencil' in
14
3754
by: Simon Abolnar | last post by:
I would like to know how to open child form from dialog form. Thanks for help! Simon
9
1523
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. My question is this - how can I determine if a user clicked the cross in the top right of the form to close it. Thanks in advance (and sorry again...
4
8820
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 which allows me to edit the record in the subform. What I want to happen is for subform with the new edits to be updated on the main form when I...
3
2233
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 form after the call is made. How can I do this ??? I tried the following code, but I get "Cant Find Field" Error. I also noticed that error...
7
2912
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 form when the user presses the escape key, but that's only when no control on the form is responsible for the escape key. For example, it has a...
3
4941
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() { $("#dialog").dialog("destroy"); $("#dialog-form").dialog({
0
7730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8058
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8100
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5624
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5301
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2249
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1061
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.