SpongeBob SquarePants
SpongeBob SquarePants

Reputation: 1055

How do I minimize all active forms in my application using VB.NET?

How do I minimize all active forms in my application with a single button click?

I have multiple forms visible at a time, and I want all my active forms to minimize when I click on a single button on one of the forms.

How can I achieve this?

Upvotes: 3

Views: 12317

Answers (2)

Cody Gray
Cody Gray

Reputation: 244971

If you are not trying to minimize MDI child windows, you can simply loop through all of the open forms in your application and set their WindowState property to "Minimized". VB.NET provides an OpenForms collection for your Application class that makes this mind-blowingly simple.

Place the following sample code into the Click event handler of a button control, or similar method:

For Each frm As Form in Application.OpenForms
    frm.WindowState = FormWindowState.Minimized
Next frm


If you want to minimize all of the forms when the user clicks the system minimize box on the title bar of a single form, you will need to listen in on that event, and execute the above code. Do this by overriding the OnSizeChanged method for each form whose minimize events you want to apply to all open forms.

You could also cause all of your forms to restore to the normal state whenever one of them is restored by clicking on its taskbar icon. Just reverse the same procedure used to minimize the windows, specifying a "Normal" window state instead of "Minimized".

For example, you might write the following code:

Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs)
    ' Call the base class first
    MyBase.OnSizeChanged(e)

    ' See if this form was just minimized
    If Me.WindowState = FormWindowState.Minimized Then
        ' Minimize all open forms
        For Each frm As Form In Application.OpenForms
            frm.WindowState = FormWindowState.Minimized
        Next frm
    ElseIf Me.WindowState = FormWindowState.Normal Then
        ' Restore all open forms
        For Each frm As Form In Application.OpenForms
            frm.WindowState = FormWindowState.Normal
        Next frm
    End If
End Sub

Upvotes: 7

Alex Hope O'Connor
Alex Hope O'Connor

Reputation: 9704

You can iterate through the Application.Forms collection like so.

For Each form as Form in Application.OpenForms
     .....
End For

Does this help?

Upvotes: 3

Related Questions