Questions

closing all forms in vb.net

+
0 Votes
Locked

closing all forms in vb.net

dej
i have 3 forms form1 form2, form3. when i close either one i want all forms to close.
in vb6 it was easily done with unload. i have tried a few things but not successful.

thank you
david
  • +
    0 Votes
    dej

    i believe this will close only the form indicated. i think i n eed some way to scan through the forms of the project just like we scanned through the controls on the form in previous discussion.

    +
    1 Votes
    devshop

    When starting a project I always setup a method (Sub/Function) to initailize the application (called "OpenApplication()") to make sure everything the app needs is available when the user interface appears, and one to close the app, (usually called "CloseApplication()") to make sure everything is closed and/or disposed when the user closes the app. You should put each of these methods in a Shared Class or Module so they're available from any call and then instead of using Me.Close() on each form, just call the CloseApplication() function, which should contain the code to close each of your forms. If you don't KNOW the names of the forms used in your app (if you've created one or more dynamically) you can loop through all forms in the app like this:

    For Each f As Form In My.Application.OpenForms
    f.Close()
    Next

    Hope this helps! :-)

    +
    0 Votes
    dej

    the loop is a good help.

    can you tell me how to " put each of these methods in a Shared Class or Module" i've tried a couple of things and did not work


    can you give a sketch of the closeapplication function

    +
    0 Votes
    dej

    the key was in your loop which allowed me to save the data in all of the forms. but i also had to set the application properties:
    the following is the stuff
    ' in myproject select the following
    ' startup form:
    ' --- form1
    ' shutdown mode:
    ' --- when startup form closes


    'in form1.formclosing put the global function saveallforms that will scan through all of the forms including form1

    ' do not need to close any form as when form1 closes the rest are removed by you setup of the application properties

    thank you for you answer.

    davide

    +
    0 Votes
    Slayer_

    Or Application.current.quit
    I can't remember which.

    +
    1 Votes
    devshop

    My bad... should have included the application properties setting for startup form and shutdown. However, to answer your question about implementing the CloseApplication code in a module, just create a new Module and add something like this:

    Friend Sub CloseApplication()

    Try
    For Each f As Form In My.Application.OpenForms
    f.Close()
    Next

    'add any other closedown code here...

    Catch

    End Try
    End Sub

    Then on the Form_Closing event of each form, add some code to check to see if it needs to save its data.

    And one moe comment... I do this for two reasons: 1) the module, if kept generic enough, is always reusable in any project; 2) It's easier to maintain code when it's off the forms and in one place. After all, code is EASY to write.... can be tough to MAINTAIN in a production environment.

    Happy coding!

    +
    0 Votes
    dej

    thanks again!

    one comment. when i do the stuff as i suggested above then i do not need to close any forms. unfortunatelly the statement f.close does not trigger any event in the form that f refers to.

    note that i put the savingformroutine in each formclosing event and the function as you suggest in the module.

    it works unbelievably well without changing any code i can add textboxes, radiobuttons etc to a form give them data and then they are automatically saved.

    i'm usually happy when i'm coding. again thanks for your, tonys, and slayers help.

  • +
    0 Votes
    dej

    i believe this will close only the form indicated. i think i n eed some way to scan through the forms of the project just like we scanned through the controls on the form in previous discussion.

    +
    1 Votes
    devshop

    When starting a project I always setup a method (Sub/Function) to initailize the application (called "OpenApplication()") to make sure everything the app needs is available when the user interface appears, and one to close the app, (usually called "CloseApplication()") to make sure everything is closed and/or disposed when the user closes the app. You should put each of these methods in a Shared Class or Module so they're available from any call and then instead of using Me.Close() on each form, just call the CloseApplication() function, which should contain the code to close each of your forms. If you don't KNOW the names of the forms used in your app (if you've created one or more dynamically) you can loop through all forms in the app like this:

    For Each f As Form In My.Application.OpenForms
    f.Close()
    Next

    Hope this helps! :-)

    +
    0 Votes
    dej

    the loop is a good help.

    can you tell me how to " put each of these methods in a Shared Class or Module" i've tried a couple of things and did not work


    can you give a sketch of the closeapplication function

    +
    0 Votes
    dej

    the key was in your loop which allowed me to save the data in all of the forms. but i also had to set the application properties:
    the following is the stuff
    ' in myproject select the following
    ' startup form:
    ' --- form1
    ' shutdown mode:
    ' --- when startup form closes


    'in form1.formclosing put the global function saveallforms that will scan through all of the forms including form1

    ' do not need to close any form as when form1 closes the rest are removed by you setup of the application properties

    thank you for you answer.

    davide

    +
    0 Votes
    Slayer_

    Or Application.current.quit
    I can't remember which.

    +
    1 Votes
    devshop

    My bad... should have included the application properties setting for startup form and shutdown. However, to answer your question about implementing the CloseApplication code in a module, just create a new Module and add something like this:

    Friend Sub CloseApplication()

    Try
    For Each f As Form In My.Application.OpenForms
    f.Close()
    Next

    'add any other closedown code here...

    Catch

    End Try
    End Sub

    Then on the Form_Closing event of each form, add some code to check to see if it needs to save its data.

    And one moe comment... I do this for two reasons: 1) the module, if kept generic enough, is always reusable in any project; 2) It's easier to maintain code when it's off the forms and in one place. After all, code is EASY to write.... can be tough to MAINTAIN in a production environment.

    Happy coding!

    +
    0 Votes
    dej

    thanks again!

    one comment. when i do the stuff as i suggested above then i do not need to close any forms. unfortunatelly the statement f.close does not trigger any event in the form that f refers to.

    note that i put the savingformroutine in each formclosing event and the function as you suggest in the module.

    it works unbelievably well without changing any code i can add textboxes, radiobuttons etc to a form give them data and then they are automatically saved.

    i'm usually happy when i'm coding. again thanks for your, tonys, and slayers help.