How can I avoid opening a User Control?

1

I'm working on a windows forms app. At the moment of calling a user control if it is open that you do not call it again. I want to do something like this:

public static Boolean OpenForm(Form Formulario)
    {
        Form F = Application.OpenForms[Formulario.Name];
        if (F == null) return false;

        if (F.WindowState == FormWindowState.Minimized)
            F.WindowState = FormWindowState.Normal;

        F.Focus();
        return true;
    }
    
asked by Pedro Ávila 31.05.2016 в 03:37
source

1 answer

4

Let's start from the base that the user control do not open, they are instantiated.

Also these are located inside a container and be a Panel, so you could validate the list of Controls of the container to see if it already exists.

bool existe = Panel1.Controls.OfType<UserControl1>().Any();

if(!existe){
     UserControl1 uc = new UserControl1();
     Panel1.Controls.Add(uc);
}
    
answered by 31.05.2016 / 04:12
source