473,385 Members | 1,934 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,385 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 4008
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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...

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.