Follow via:
RSS
Email Alert
Question
0 Votes
+ -

closing all forms in vb.net

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
Tags: windows, software
30th Dec

Answers (4)

0 Votes
+ -
Im not sure if its different for Win32 or WPF but isn't it just
Formname.close()
30th Dec

Replies

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.
dej@... 31st Dec
1 Vote
+ -
Here's what I do...
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! happy
Updated - 31st Dec

Replies

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
dej@... 31st Dec
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
dej@... 31st Dec
0 Votes
+ -
Application.quit
Or Application.current.quit
I can't remember which.
31st Dec
1 Vote
+ -
A little more info..
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!
1st Jan

Replies

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.
dej@... 1st Jan
Answer the question
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.