|
-
Jun 5th, 2010, 02:34 PM
#1
Thread Starter
Hyperactive Member
The "X" Button
If I wanted to place some "cleanup" code in an application that would execute when the X is clicked to close a form, where would it need to be? There seems to be four candidates:
Deactivate
Disposed
FormClosed
FormClosing
There are lot of others, but they do not seem to have anything to do with ending a process.
-
Jun 5th, 2010, 02:35 PM
#2
Re: The "X" Button
FormClosing. You can e.Cancel = True, clean up, and the call Me.Close
Though you may not have to cancel the close. You'll need to play around with it to see what's best.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Jun 5th, 2010, 02:51 PM
#3
Thread Starter
Hyperactive Member
Re: The "X" Button
It's been tricky over the years to make anything work in this situation. I never had any luck with this in VB6, but that was then. I am thinking of situations like closing files, and so on. I have a button on a form for stopping, but sometimes I am prone to go for the "X" instead.
-
Jun 5th, 2010, 02:54 PM
#4
Re: The "X" Button
 Originally Posted by storm5510
It's been tricky over the years to make anything work in this situation. I never had any luck with this in VB6, but that was then. I am thinking of situations like closing files, and so on. I have a button on a form for stopping, but sometimes I am prone to go for the "X" instead.
Right. And in this case, you can use the FormClosing event to do as you like.
I often use it to make sure the User wants to close it.
There's no reason it won't work in your case.
If it doesn't work, then let us know and we'll help you figure it out
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Jun 5th, 2010, 03:25 PM
#5
Re: The "X" Button
 Originally Posted by weirddemon
FormClosing. You can e.Cancel = True, clean up, and the call Me.Close
Though you may not have to cancel the close. You'll need to play around with it to see what's best.
I don't think setting e.Cancel = True would really be required, as you would be executing your code in the FormClosing event and the form won't exit until it has finished executing that event code.
-
Jun 5th, 2010, 03:48 PM
#6
Re: The "X" Button
 Originally Posted by Pradeep1210
I don't think setting e.Cancel = True would really be required, as you would be executing your code in the FormClosing event and the form won't exit until it has finished executing that event code.
Right. Which is why I mentioned it. But, you never know
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Jun 5th, 2010, 04:01 PM
#7
Re: The "X" Button
 Originally Posted by weirddemon
FormClosing. You can e.Cancel = True, clean up, and the call Me.Close
Though you may not have to cancel the close. You'll need to play around with it to see what's best.
This doesn't make any sense. If you set e.Cancel to true (which takes effect after the sub has finished) and you call me.Close from inside of it, you're going to get the FormClosing event raised again, where you're setting e.Cancel to True all over again. It'd never let the form close.
What you should do is set e.Cancel to True under 2 conditions: you've prompted the user for confirmation and they choose the cancel the form's close or something fails during the cleanup process and you want to keep the form open a little longer to let the user try closing it again later.
Also, when canceling the FormClosing event, always let the form close if: Windows is shutting down, or the user kills the app from the Taskmanager. Both of these can be tested for under e.CloseReason
-
Jun 5th, 2010, 04:13 PM
#8
Thread Starter
Hyperactive Member
Re: The "X" Button
I used "FormClosing" with a Try..End Try block. It all works very well. I had to put a trap in to check a specific condition before entering the block so it would exit the sub before entering the Try. Otherwise, it will display an error message every time I close the form.
-
Jun 5th, 2010, 09:47 PM
#9
Re: The "X" Button
 Originally Posted by JuggaloBrotha
This doesn't make any sense. If you set e.Cancel to true (which takes effect after the sub has finished) and you call me.Close from inside of it, you're going to get the FormClosing event raised again, where you're setting e.Cancel to True all over again. It'd never let the form close.
What you should do is set e.Cancel to True under 2 conditions: you've prompted the user for confirmation and they choose the cancel the form's close or something fails during the cleanup process and you want to keep the form open a little longer to let the user try closing it again later.
Also, when canceling the FormClosing event, always let the form close if: Windows is shutting down, or the user kills the app from the Taskmanager. Both of these can be tested for under e.CloseReason
I thought checking for conditions would be implied.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Jun 6th, 2010, 12:16 AM
#10
Re: The "X" Button
The FormClosing event is for doing something BEFORE the form closes, allowing you to prevent the closure under certain circumstances. Cleanup code is most appropriately performed on the FormClosed event.
-
Jun 6th, 2010, 01:06 PM
#11
Thread Starter
Hyperactive Member
Re: The "X" Button
Either way, it works and that's all I wanted.
If I wanted to "disable" the function of "X" to close the form, then I could put some code in the FormClosing Sub? Something like "Exit Sub", for example?
-
Jun 6th, 2010, 01:11 PM
#12
Re: The "X" Button
 Originally Posted by storm5510
Either way, it works and that's all I wanted.
If I wanted to "disable" the function of "X" to close the form, then I could put some code in the FormClosing Sub? Something like "Exit Sub", for example?
To disable form closing, you set e.Cancel = True in FormClosing
Code:
If e.CloseReason = CloseReason.UserClosing Then
e.Cancel = True
End If
-
Jun 6th, 2010, 03:07 PM
#13
Thread Starter
Hyperactive Member
Re: The "X" Button
Thank you. I added that to my snippet collection.
-
Jun 6th, 2010, 04:37 PM
#14
Re: The "X" Button
I told you how to do that in my very first post >.>
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Jun 6th, 2010, 04:48 PM
#15
Re: The "X" Button
Also worth noting that if you do something in your formclosing event that hangs or takes quite a while then it can stop windows from shutting down if your program is open. So I generally check the e.CloseReason property from the FormClosing event to decide what to do.
EDIT: Oops didnt realise someone else already mentioned this
Last edited by chris128; Jun 7th, 2010 at 02:38 AM.
-
Jun 6th, 2010, 07:30 PM
#16
Thread Starter
Hyperactive Member
Re: The "X" Button
 Originally Posted by weirddemon
I told you how to do that in my very first post >.>
You did, but I didn't have a clue what you were talking about. Then what is below came along:
Code:
If e.CloseReason = CloseReason.UserClosing Then
e.Cancel = True
End If
Then it made sense. Sometimes, a little effort goes a long way.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|