Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class to minimize all forms

Tags:

c#

class

winforms

How I will Convert this into class which minimize all the childform? When I try to transfer it into class I got error :

The type or namespace name 'MdiChildren' could not be found (are you missing a using directive or an assembly reference?)

public void minimizeAll()
{
   foreach (Form childForm in MdiChildren)
   {
      childForm.WindowState = FormWindowState.Minimized;
   }
}
like image 965
Bags Banny Avatar asked Aug 24 '26 23:08

Bags Banny


2 Answers

MdiChildren is not a static property, you need to use an instance of a form. You could pass in a Form as a parameter.

public void minimizeAll(Form parentForm)
{
   foreach (Form childForm in parentForm.MdiChildren)
   {
      childForm.WindowState = FormWindowState.Minimized;
   }
}
like image 62
keyboardP Avatar answered Aug 27 '26 12:08

keyboardP


You may try using something like this (hardly you have many MDI parent forms):

public static class Minimizer {
  public static void MinimizeMdiChildren() {
    // Enumerate all forms, minimize MDI Children
    foreach (Form form in Application.OpenForms) 
      if (form.IsMdiChild)
        form.WindowState = FormWindowState.Minimized;
  }
}
...
Minimizer.MinimizeMdiChildren();
like image 38
Dmitry Bychenko Avatar answered Aug 27 '26 12:08

Dmitry Bychenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!