the form unload event

genu

Well-known member
Joined
Jun 9, 2005
Messages
94
Programming Experience
1-3
[RESOLVED]the form unload event

ok here is my situation:
I have "FORM1"
there is a button on form1 that opens up form2
user can do something on form2, but when they click the x button in the corner of form2....form2 just closes.

I want to make it show a dialog like "Are you sure you want to discard changes..." YES button and CANCEL button.

the question is where do I put the code? in the form2.close event? cause i tried it, but it shows the dialog but the form still closes.

please help
 
Last edited:
closing ... that's the event

PHP:
PrivateSub Form1_Closing(ByVal sender AsObject, ByVal e As System.ComponentModel.CancelEventArgs) HandlesMyBase.Closing
  
If MessageBox.Show("are you sure you want to quit ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.None) = DialogResult.No Then
 
e.Cancel = True
 
ExitSub
 
Else
 
Application.Exit()
 
EndIf
Cheers ;)
 
that works...thank...but Now that I solved the problem of them clicking the "X" in the corner what do I do if I have a button "OK" to not show the message box when its unloading?
 
What kulrom has posted will work, but I have a few points to make.

1. In this case the "Exit Sub" statement is superfluous.

2. The "Application.Exit()" statement is superfluous. The app will exit anyway if the current form is the startup object. If the current form is not the startup object, calling Application.Exit() will bypass the Closing event handlers of all other forms as their Closing events will not be raised.

3. As I have mentioned in some other threads, this is a misuse of MessageBoxButtons.YesNo. It is more correct to use MessageBoxButtons.OKCancel in this case. If you were asking something like "Would you like to save before exiting?" then YesNo would be correct, because selecting No would still carry out the original request (Exit) but not perform the additional action (Save). If pressing a button cancels the action the user originally requested it should be a Cancel button and not a No button. If you're wondering about Abort, that is correctly used to discontinue a process that has already begun.
 
So how do I make it when they click the x button it asks them if they really want to dispose, and if they click the ok button at the bottom it will automatically save the work and close the form?
 
You will need to have some variable, probably of type Boolean, that indicates whether the contents of your form have been saved. If you're talking about data held in a DataSet, you already have this in the HasChanges function. Otherwise it's up to you. You could have a boolean variable, called changesSaved for example, that is set to False whenever data is changed. If the user then saves the data by pressing a save or OK button, you save the data and set the variable to True. In your form's Closing event handler, you then check the value of this variable and only ask for confirmation if it is False. This is a situation where you might want to ask "Would you like to save before closing?" and use MessageBoxButtons.YesNoCancel. Pressing Yes would save and close; pressing No would close without saving; pressing Cancel would not close at all. This is a good demonstration of the difference between a No button and a Cancel button.
 
Ok I figured it out. I made the close event check if the saved boolean is false. it it is false then it displays the dialog box. if its not false then it just doesnt show it. Now to make it so it doesnt show the msgbox, I just made the saved = True when they click ok and the date was saved, which is before the form closes. so it was like this:

saved = true
me.close

that way the close even is called after the saved is true which skips the msgbox.
 
Back
Top