Results 1 to 16 of 16

Thread: [RESOLVED] exit

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    259

    Resolved [RESOLVED] exit

    how can I make the programme exit itself If I press a button, simular to if somebody pressed the red cross in the corner?
    thankyou.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: exit

    Thread moved from CodeBank forum (which is for code examples, not questions). As your other posts are in Classic VB, I assumed this is where you want it

    If you are actually using Classic VB, simply use this:
    Code:
    Unload Me

  3. #3
    Lively Member
    Join Date
    Jun 2006
    Posts
    98

    Re: exit

    Yeah thats they way but with everything it will look like
    vb Code:
    1. Private Sub Command1_Click()
    2.     Unload Me
    3. End Sub

    Since i can tell you are new you may of not known that.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: exit

    Unload Me is the proper answer if you have only one form loaded. If there's any chance that there's more than one then do this

    Code:
    Dim frm As Form
    
    For Each frm In Forms
        Unload frm
        set frm = Nothing
    Next

  5. #5
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: exit

    Just a note on martin's code you shouldn't really put it in the unload event of the main form for the reason being. It will try to unload the form even though it is already in the process of unloading.

  6. #6
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: exit

    Hey cant we Use End or End Sys ???
    Like

    Code:
       Private Sub Command1_Click()
           End
       End Sub
    Since Kaimonington says that exit the programme, not close a form ????

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: exit

    Quote Originally Posted by Hell-Lord
    Just a note on martin's code you shouldn't really put it in the unload event of the main form for the reason being. It will try to unload the form even though it is already in the process of unloading.
    There's no problem putting the code in the Unload event.

  8. #8
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: exit

    End is not an appropriate method of closing a project.

    The reason being it doesn't allow the program to clean up after it self. Memory issue can also occur.
    Last edited by Paul M; Aug 3rd, 2007 at 01:45 AM.

  9. #9
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    280

    Re: exit

    Hiya Martin

    If I have already unloaded a form then why do I have to set it equal to nothing and if it has already been unloaded why does assigning it to anything not generate an error? I do it only because I have been told I have to but it makes me cringe every time.

  10. #10
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: exit

    yes Hell-Lord is right. This is what is said about End in MSDN

    Remarks

    When executed, the End statement resets all module-level variables and all static local variables in all modules. To preserve the value of these variables, use the Stop statement instead. You can then resume execution while preserving the value of those variables.

    Note The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events of forms and class modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.

    The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing


    so dont use it to exit the program

  11. #11
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: exit

    Code:
    Set Frm = Nothing
    release the handle of the window

  12. #12
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: exit

    Just to note
    Code:
    Set frm = Nothing
    should come after the
    Code:
    unload frm
    otherwise the unload frm line will fail.

  13. #13
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: exit

    So basically the complete run down is....

    vb Code:
    1. Dim frm as Form
    2. For Each frm In Forms
    3.      Unload frm          ' deactivate the form
    4.      Set frm = Nothing   ' remove from memory  
    5. Next

    And when you do it properly the events fired in order are:

    Form_QueryUnload
    Form_Unload
    Terminate
    '
    and to go a little further in....

    the UnloadMode variable in the QueryUnload routine has the following values.

    '0 : Closed from the X in the corner of the window.
    '1 : Code in application has been invoked and is closing the application.
    '2 : Windows session is ending.
    '3 : Task Manager.
    '4 : MDI Parent form is closing so child forms close to.
    Last edited by Paul M; Aug 3rd, 2007 at 03:05 AM.

  14. #14
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: exit

    Also on another note those who say ExitProcess is good. It is but again like End it skips the routines .......

    Form_QueryUnload
    Form_Unload
    Terminate

  15. #15
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: exit

    Yes true but as i said the order of firing is.....

    QueryUnload
    Unload
    Terminate

    So what it would be doing is skipping QueryUnload first then once it gets to unload going back to complete QueryUnload. But yea it is no big deal thats just my logic.

  16. #16
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: exit

    Quote Originally Posted by Hell-Lord
    Yes true but as i said the order of firing is.....

    QueryUnload
    Unload
    Terminate

    So what it would be doing is skipping QueryUnload first then once it gets to unload going back to complete QueryUnload. But yea it is no big deal thats just my logic.
    Adding my code in the Unload event has no effect on the number of times or the order in which QueryUnload, Unload and Terminate are executed.

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