Home > Net >  C# Method makes forms dynamically via string
C# Method makes forms dynamically via string

Time:09-17

First ever post in here, if I made any mistake in the post pls tell me so I can fix it

So I'm trying to make classes to handle most of the code, one of the things I wanted to do was to have one handle all the opening and making of form instances. Doing a .Show(); on instances that already exist was simple as I know the order they are created I can just Form _form = Application.OpenForms[i]; to grab the instance, but when it's not already created I couldn't find a way to deal with it, I read a bit unto it but couldn't find something that really fit what I wanted to do, something something about reflection seemed to be the right path but couldn't get it to work, so some light in the matter would be very appreciated.

In a nutshell I'm trying to make something like: (I know something similar is not possible but I think this is the easiest way to explain exactly what I seek. A workaround I made was to have the code to generate each of the Forms into a switch and just send their number, so it's what I'm gonna use if I can't find a better solution, but I wanted to learn a "proper/cleaner" way of achieving this)

static public bool MakeForm(string name)
{
   name _name = new name();
   _name.Show();
}

[Edit: I realized that this is irrelevant for my project cuz I can just ready up every single Form on login, but I still hope to know how to do this if any of yall can show me how to/where to read]

CodePudding user response:

I am not sure If I understand you correctly, but I think you need a factory class.

    public static class Factory
    {
        public static Form Create(string name)
        {
            switch (name)
            {
                case "FormA":
                    return new FormA();
                case "FormB":
                    return new FormB();
            }
        }
    }

Than you can create your forms by name.

  Factory.Create("FormA").Show();

CodePudding user response:

Here's a simple example using the Reflection approach:

private void button1_Click(object sender, EventArgs e)
{
    Form f2 = TryGetFormByName("Form2");
    if (f2 != null)
    {
        f2.Show();
    }
}

public Form TryGetFormByName(string formName)
{
    var formType = System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
        .Where(T => (T.BaseType == typeof(Form)) && (T.Name == formName))
        .FirstOrDefault();
    return formType == null ? null : (Form)Activator.CreateInstance(formType);
}

Here's an alternate version that checks to see if the form is already open:

public Form TryGetFormByName(string formName)
{
    // See if it's already open:
    foreach (Form frm in Application.OpenForms)
    {
        if (frm.Name == formName)
        {
            return frm;
        }
    }

    // It's not, so attempt to create one:
    var formType = System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
        .Where(T => (T.BaseType == typeof(Form)) && (T.Name == formName))
        .FirstOrDefault();
    return formType == null ? null : (Form)Activator.CreateInstance(formType);
}
  • Related