5

I have a Windows Application in C# and I need to call a Form whose name is saved into a string variable in run-time.

Like;

I already have the form; Login.cs

string formToCall = "Login"
Show(formToCall)

Is this possible ?

Mtok
  • 1,600
  • 11
  • 36
  • 62
  • 1
    Possible duplicate - http://stackoverflow.com/q/540066/969613 – JMK Dec 20 '12 at 15:32
  • 3
    Yes, but you almost certainly don't want to do it that way. There are better ways of designing your application. If you addressed your problem at a larger scope we could help you with that. – Servy Dec 20 '12 at 15:32
  • Are you sure that the string will be a valid form name? What about Namespaces? – Bridge Dec 20 '12 at 15:34
  • Yes I'm sure @Bridge. :) and namespace is something. I can do this using if or switch case. But I wonder if it is possible in this way. – Mtok Dec 20 '12 at 15:41
  • @Servy I'm preparing a Pocket PC program that is gonna be used in some factories. This is not a big issue but I just wondered. – Mtok Dec 20 '12 at 15:43
  • 1
    @Mtok Like I said, this is the wrong avenue to pursue down. You shouldn't be implementing any of the answers proposed, but a proper approach can't be given with the information you've provided. – Servy Dec 20 '12 at 15:43
  • @Mtok That's too far out in scope. How are you determining what form you should open (meaning where does the string come from). Do you have a fixed number of forms, is it a small or large number, is it likely to change much, a bit, never, etc. – Servy Dec 20 '12 at 15:45
  • @Servy The form name is coming from the Database. According to the buttons clicked, I specify which form should be opened. Forms are fixed. Fixed number of forms (small number) with fixed names. It is not likely to change much. – Mtok Dec 20 '12 at 15:49
  • 1
    @Mtok In that case I'd create an enumeration, rather than using strings, and use a `switch` in a factor method to create the form given an enumeration value. The fact that the data is persisted in a database pretty much prevents a purely polymorphic solution. – Servy Dec 20 '12 at 15:54

5 Answers5

7

Have a look at Activator.CreateInstance(String, String):

Activator.CreateInstance("Namespace.Forms", "Login");

you can also use the Assembly class (in System.Reflection namespace):

Assembly.GetExecutingAssembly().CreateInstance("Login");
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • If you do this, how do you get events like the Form Load event to trigger? From my testing, Activator.CreateInstance only executes the constructor but not the Form Load event. – Shiv Mar 14 '18 at 00:28
4

Using reflection:

//note: this assumes all your forms are located in the namespace "MyForms" in the current assembly.

string formToCall = "Login"
var type = Type.GetType("MyForms." + formtocall);
var form = Activator.CreateInstance(type) as Form;

if (form != null)
   form.Show();
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
2

For more dynamic so you can place your form in any folder :

public static void OpenForm(string FormName)
{
    var _formName = (from t in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                     where t.Name.Equals(FormName)
                     select t.FullName).Single();
    var _form = (Form)Activator.CreateInstance(Type.GetType(_formName));
    if (_form != null) 
    _form.Show();
}
1

Try this:

var form = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(formToCall);
form.Show();
snurre
  • 3,045
  • 2
  • 24
  • 31
0

(Form)Assembly.GetExecutingAssembly().CreateInstance()

    Form frm = (Form)Assembly.GetExecutingAssembly().CreateInstance("namespace.form");
    frm.Show();