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

11,327

Solution 1

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

Solution 2

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

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

Does this help?

Share:
11,327
SpongeBob SquarePants
Author by

SpongeBob SquarePants

Top Secret I live Deep down in the Pacific Ocean in the subterranean city of Bikini Bottom in a pineapple along with my pet Gary. I make delicious Krabby Patty Burger at Krusty Krabs. My favorite quote: Isn't this great Squidward? Its just the 3 of us. You, me, and this brick wall you built between us

Updated on July 16, 2022

Comments

  • SpongeBob SquarePants
    SpongeBob SquarePants almost 2 years

    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?

  • SpongeBob SquarePants
    SpongeBob SquarePants about 13 years
    That was great but there is one more problem, I want all my forms to go to normal state when I click any one of the forms minimized in the taskbar.
  • Cody Gray
    Cody Gray about 13 years
    @abcd: Hoo boy. That's certainly possible; give me a minute to update my answer.
  • SpongeBob SquarePants
    SpongeBob SquarePants about 13 years
    Muhahah...it works. @Cody Thanks a lot, I learn a lot each time I get an answer. Don't know much about levels , how many levels are there ?
  • Cody Gray
    Cody Gray about 13 years
    @abcd: Yeah, you can go a lot lower than what the .NET Framework normally exposes to you. It's often called P/Invoking, short for platform invoking, but all it means is that you're calling native Windows API functions, rather than their managed counterparts provided by the managed .NET Framework environment. It's often necessary when the functionality you want isn't provided by .NET. But I updated my answer after some testing; it looks like the .NET designers got it right and OnSizeChanged isn't called needlessly. No reason to overcomplicate things unless you have an actual problem.