(I will explain as detailed as possible so that you can understand the question more clearly)
I have this method to open forms inside a panel, with which I can allow forms that contain constructors with parameters and without parameters, this based on this question How can I pass Parameters to Form with this Generic Method? which I did here on SO.
public void AbrirFormulario<T>(Func<T> metodofactory) where T : Form
{
//Buscar el formulario dentro del panel.
Form formulario = panel_contenedor.Controls.OfType<T>().FirstOrDefault();
if (formulario != null)
{
//Si la instancia esta minimizada la dejamos en su estado normal
if (formulario.WindowState == FormWindowState.Minimized)
{
formulario.WindowState = FormWindowState.Normal;
}
//Si la instancia existe la pongo en primer plano
formulario.BringToFront();
return;
}
//Se abre el form
formulario = metodofactory();
formulario.TopLevel = false;
panel_contenedor.Controls.Add(formulario);
panel_contenedor.Tag = formulario;
formulario.Show();
}
This method currently only allows to open a single instance of the same form, if at the moment of invoking the option the form does not exist in the panel, it shows it, and if the form exists then I do a BringToFront();
to put it in the foreground.
With this I perform the search for the form within the panel.
Form formulario = panel_contenedor.Controls.OfType<T>().FirstOrDefault();
Menu
Suppose I have the following menu with the options:
MENU
Process 1 (
Form1
With 3 parameters) (Allow Multiple Instances)process 2 (
Form2
No parameters) ( DO NOT Allow Multiple Instances)Process 3 (
Form3
With 1 Parameters) ( DO NOT Allow Multiple Instances)Process 4 (
Form1
)Process 5 (
Form1
)
The structure above means the following: from menu option Proceso1
to Proceso3
are three different forms, and menu options Proceso4
and Proceso5
are exactly the same form ( Form1
).
In few words the option
Proceso 1
,Proceso 4
,Proceso 5
is a single formForm1
. Which will behave differently depending on which menu option is called.
Conditions or Restrictions
It occurs to me to condition it through a parameter bool permitirInstancias = true;
.
The conditions or restrictions that must be met to allow opening multiple instances of a form are the following:
1 - If the form has the parameterpermitirInstancias = true;
2 - If the Text property of the form is not the same as the one that is open, for example:
- The
Form1
according to the option invoked has a different name in the propertyText
at runtimeForm1.Text = "Proceso1";
,Form1.Text = "Proceso2";
etc...
I have tried the following
I suppose I could use Any()
to condition it, but I don't know how to apply the two restrictions mentioned above, instead I have done this: To the method I have added the following code:
Form formulario;
var control = panel_contenedor.Controls.OfType<T>().FirstOrDefault();
if (control != null)
{
if (control.Name.Equals("Form1"))
formulario = panel_contenedor.Controls.OfType<T>() as Form;
else
formulario = panel_contenedor.Controls.OfType<T>().FirstOrDefault();
}
else
formulario = panel_contenedor.Controls.OfType<T>().FirstOrDefault();
With this in a forced way, I am forcing that if the form is called, it
Form1
allows opening several instances and it works, but even if the propertyText
is the same, it opens the same form.
The call of the menu options would be the following:
AbrirFormularios(() => new Form1("A", 1, true)); //Opcion: Proceso1 (true) indica que si debe permitir varias instancias.
AbrirFormularios(() => new Form2()); //Proceso2 (NO permite varias instancias)
AbrirFormularios(() => new Form3("Parametro")); //Proceso3 (NO permite varias intancias)
AbrirFormularios(() => new Form1("B", 1, true)); //Opcion: Proceso4
AbrirFormularios(() => new Form1("C", 1, true)); //Opcion: Proceso5
How could I under these conditions allow a specific form to be opened more than once within the panel?
As I told you in a comment on your previous question, a very simple way would be to use the
Tag
form property.Your code would look like this:
As you can see, a new parameter is added to the method that will receive if the form will support multiple instances,
multiplesInstancias
which hasfalse
a default value. The value of this parameter is added to thatTag
of the form when it is created. When checking if you already have an instance of that form, it checks if it supports multiple instances, and if it doesn't, the existing form is displayed and the method is exited. Otherwise, a new one is created.Edit:
1- I have placed the parameter
textoForm
as optional in the functiontextoForm = ""
. This is because I will not put the title on all the forms through the function, this will only be when the form must allow several instances.2- If the parameter
textoForm
is not blank then I look for the form inside the panel using the method suggested by@Dariel Ramos Díaz de Villegas
with a slight change of the propertyname
by the propertyText
3- I have added this other condition to the one
if
that validates thetag
andtextoForm
for the case of forms that do not allow multiple instances.4- I only add the
tag
to the form if it allows multiple instances.5- And finally I only add the title to the form, if it allows several instances and if
textoForm
it is not blank.Clever!.