Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted

I have built this little project which allows you to Unload all open forms , before closing the main App form down ( to prevent any forms remaining open ) there's a sample code project .zip included , it's quite a simple task, but it may help a few people trying to find ways to close all forms in an App.

   Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
   Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

   Private frm2 As New Form2()

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
       frm2.Show()
       Me.BringToFront()
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim objType As Type() = Reflection.Assembly.GetExecutingAssembly.GetTypes()
       Dim x As Integer, i As Integer
       Try
           For x = LBound(objType) To UBound(objType)
               If Not objType(x).Name = Me.Name Then '/// make sure you dont unload this form yet.
                   i = FindWindow(vbNullString, objType(x).Name)
                   PostMessage(i, CInt(&H10), vbNullString, vbNullString)
               End If
           Next
       Catch ex As Exception
           MessageBox.Show("Oops, the following error occured:" & ex.Message)
       Finally
           MyBase.Close() '/// now that all the other forms are closed , unload this one.
       End Try
   End Sub

hope it helps a few people :)

how to unload forms.zip

  • *Experts*
Posted
Another (CLR compliant) way to do it is to keep a global Form collection (shared, in a class). Whenever you create a form, add it to this collection. When you're finished, it just takes a simple:
Dim f As New Form()

For Each f In Globals.AppForms 'Global is the class, AppForms is the shared collection
 f.Close()
Next

Posted

But VolteFace how would you add Forms to that variable? (f)?

 

 

I'm thinking this:

 


Dim Form3 As New Form3 (a new form has been added)

f.Add(Form3)

 

Would that work?

"Reality is fake, Dreams are for real"
  • *Experts*
Posted

He means createing a class which has a shared collection of forms that you add the forms to:

Public class Forms
   Public Shared AllForms As New ArrayList()
End Class

Something like this.

Then from wherever you create an instance of your new form you would do this:

Dim form1 as New Form1
Forms.AllForms.Add(form1) 'access the shared array list and add the form to it

  • Leaders
Posted
well the way to unload forms without adding them to any Global seems ok to me. a few lines of code and the forms will unload, there may be better / alternative ways, but it works and anything that makes me happy:)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...