Results 1 to 16 of 16

Thread: The "X" Button

  1. #1

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    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.


  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    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.

  4. #4
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: The "X" Button

    Quote Originally Posted by storm5510 View Post
    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: The "X" Button

    Quote Originally Posted by weirddemon View Post
    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: The "X" Button

    Quote Originally Posted by Pradeep1210 View Post
    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  7. #7
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: The "X" Button

    Quote Originally Posted by weirddemon View Post
    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
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  8. #8

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    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.

  9. #9
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: The "X" Button

    Quote Originally Posted by JuggaloBrotha View Post
    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    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?

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: The "X" Button

    Quote Originally Posted by storm5510 View Post
    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: The "X" Button

    Thank you. I added that to my snippet collection.

  14. #14
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: The "X" Button

    I told you how to do that in my very first post >.>
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  15. #15
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  16. #16

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: The "X" Button

    Quote Originally Posted by weirddemon View Post
    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
  •  



Click Here to Expand Forum to Full Width