473,434 Members | 1,431 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,434 software developers and data experts.

Creating a confirmation box when clicking on the X

I am wondering how I would create and code a box that asks if you want to close when you try to close out by hitting the x in the window bar of my form?

I dont want to disable the standard issued close..but i want them to confirm close before closing a window. I have my buttons that confirm this...but not for when they hit the close box up top...my form just closes. :(

I am using Visual studio.net with vb.net codes (so far..)
Apr 13 '10 #1

✓ answered by balabaster

@MrMancunian
You missed a vital point that this needs to be done in the form's closing event.

@Dcurvez Create an event handler for the Form.Closing event, in the event handler, fire a confirmation box in the manner suggested by MrMancunian. If the result is .No or .Cancel then call e.Cancel (e is of type FormClosingEventArgs - calling cancel prevents the window closing). This event handler will fire even if the form is closed using the control toolbox or the x icon in the top right corner.

9 4011
tlhintoq
3,525 Expert 2GB
You want to handle the Form.Closing event, then ask your question to the user.

In C# it looks like this. You should be able to translate it fairly easily.
Expand|Select|Wrap|Line Numbers
  1.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  2.         {
  3.             this.TopMost = true; // so that our dialog will be on top and seen.
  4.             System.Windows.Forms.DialogResult result = MessageBox.Show(this,"Are you sure", "Close?", 
  5.                                                                        MessageBoxButtons.YesNo,
  6.                                                                        MessageBoxIcon.Question);
  7.             if (result == System.Windows.Forms.DialogResult.No) e.Cancel = true; // Cancel the close
  8.         }
  9.  
Apr 13 '10 #2
i am wondering if anyone can tell me how to get a confirmation box before closing when people close a window by using the x to close? i am using vb.net
Apr 14 '10 #3
MrMancunian
569 Expert 512MB
You declare a variable as MsgBoxResult, assigning a MsgBox to it with a MsgBoxStyle.YesNoCancel. After that, you check the value of the variable you created. Possible values are MsgBoxResult.Yes or MsgBoxResult.No. Then, according to the outcome, do something :-)

Steven
Apr 14 '10 #4
balabaster
797 Expert 512MB
@MrMancunian
You missed a vital point that this needs to be done in the form's closing event.

@Dcurvez Create an event handler for the Form.Closing event, in the event handler, fire a confirmation box in the manner suggested by MrMancunian. If the result is .No or .Cancel then call e.Cancel (e is of type FormClosingEventArgs - calling cancel prevents the window closing). This event handler will fire even if the form is closed using the control toolbox or the x icon in the top right corner.
Apr 14 '10 #5
MrMancunian
569 Expert 512MB
True, I missed that ;-)
Apr 14 '10 #6
tlhintoq
3,525 Expert 2GB
Please don't create second threads for the same question.
This was already answered, complete with code in:
http://bytes.com/topic/visual-basic-...hen-clicking-x
The two threads have been merged
Apr 14 '10 #7
balabaster
797 Expert 512MB
@tlhintoq That could also be tidied up slightly by condensing:

Expand|Select|Wrap|Line Numbers
  1. this.topmost = true;
  2. e.cancel = MessageBox.Show(this,
  3.     "Are you sure", 
  4.     "Close?",  
  5.     MessageBoxButtons.YesNo, 
  6.     MessageBoxIcon.Question) == DialogResult.No;
It saves a check operation in the cases where No will be pushed and it's just as performant to set the value true as it is to check the result of the user input.
Apr 14 '10 #8
I am sorry for the double post but for some reason when I got the first reply from tlhintoq I was not able to see the response. I checked and waited for a bit thinking it was just some fluke thing..but still nothing, so I decided to go and retry posting again. I did not mean to muck up anything..and I want to thank all of you that took the time to reply, it was all most helpful :)
Apr 15 '10 #9
tlhintoq
3,525 Expert 2GB
I thought I would toss in my typical set of exit methods (including the nice addition from BalaBaster)

Expand|Select|Wrap|Line Numbers
  1. // Menu:  File | Exit
  2. private void closeToolStripMenuItem_Click(object sender, EventArgs e)
  3. {
  4.     LogThis("File | Exit", tlhintoq.Logger.LogEvents.DEFAULT);
  5.     this.Close();
  6. }
  7.  
  8. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  9. {
  10.     LogThis("Closing");
  11.     ConfirmQuit(e);
  12. }
  13.  
  14. void ConfirmQuit(FormClosingEventArgs e)
  15. {
  16.     LogThis("Confirm Quit");
  17.     this.TopMost = true;
  18.     e.Cancel = MessageBox.Show(this,
  19.                                 "Are you sure",
  20.                                 "Close?",
  21.                                 MessageBoxButtons.YesNo,
  22.                                 MessageBoxIcon.Question) == DialogResult.No;
  23.  
  24.     // True means cancel the close
  25.     // So false means don't cancel, we *are* closing.
  26.     if (!e.Cancel)
  27.     {
  28.         Stop();// Politely stop all workers and other "Stop" behaviors
  29.         Quit();// Perform clean up, save status, check for threads not yet aborted, then quite the application
  30.     }
  31. }
  32.  
  33. private void Quit()
  34. {
  35.     LogThis("Quitting");
  36.     tlhintoq.Environment.ThisPC.FindAndKillProcess(Application.ProductName);//Search out threads and kill them
  37.     Application.Exit();
  38.     Environment.Exit(0);
  39. }
  40.  
Apr 22 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dave | last post by:
I am using vs.net 2003 on windows xp. After clicking on a project within my solution and selecting create new folder vs.net responds back with , the "directory already exists". If i look at the...
9
by: ScooterMX | last post by:
I have a <a href="javascript:top.window.close()"><img src="exit.gif"></a> tag on my page that closes itself when you click on it. What I NEED is for it to put up a confirmation message that...
6
by: Nedu N | last post by:
Hi, I want to have confirmation(Yes/No) on a button of the webform in which there are many validation controls. I want all the validation controls to be triggered first and then Yes/No...
0
by: Suzanne | last post by:
I'd like to know how can I put up a confirmation question when the user tries to delete a row in the datagrid by clicking on the row header and pressing the Delete key? I have found this code on...
1
by: Steve | last post by:
Is it possible to have a confirmation dialogue appear when the delete command is clicked on a gridview? i.e. asking the "Are you sure you wish to delete this record?" question. I just don't like...
11
by: Prince of Code | last post by:
Hey There, I have been to this group for a couple of days. I am really impressed by the way people respond. I get answers for all my doubts and that too more info. So I am putting across a doubt...
6
by: timstu | last post by:
Hi all, thanks for your help in advance. I have a page that creates a .CSV file that has been migrated over to Windows Server 2003 from Windows 2000 Server. It worked fine before I moved it,...
2
by: bienwell | last post by:
Hi all, I still have a problem when using Confirmation box in ASP.NET program. I need to get the value return of YES or NO from confirmation box in Sub function of VB program to do some tasks....
1
by: brianpmccullough | last post by:
I have an ASP.NET page that has a few buttons on it. One of the buttons kicks off a long running process on the web server and I want to prevent the user from clicking the button again while the...
2
by: busybeeitsme | last post by:
i am a beginner to php.i have a doubt. could anybody clarify my doubt? i am having a 'delete' page. in that the user has to enter the user id which is to be deleted. while clicking delete button a...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.