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 ?
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");
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With