how can i instantiate a form through its name[Resolved]
here...
VB Code:
'frmname="Form1"
public void Show(string frmname)
{
System.Windows.Forms.Form f;
f.Name=frmname;
'....here, how can i identify when i instantiate that
'it is pointing to Form1
' supposed to be it looks like this...Form1 f=new Form1();
'then Show();
}
is this possible? or maybe i'm in the wrong way.
for now i have no idea on it. :bigyello:
Re: how can i instantiate a form through its name
You don't need to instantiate a new form if you only want to point to another form. All you have to do is to assign your other form to your new variable :
Code:
public void Show(System.Windows.Forms.Form formInit)
{
System.Windows.Forms.Form f;
f = formInit;
f.Show();
}
Re: how can i instantiate a form through its name
thanks for the info you give sixfeet but i need to pass a string and through that string name it reflect to the name of a form.
Re: how can i instantiate a form through its name
i dont think you can instantiate a form through its name, however you need to instantiate a form first, then you can assign a form name to it.
anyway, you cannot show a form before you instantiate a form object.
correct me if i am wrong
Re: how can i instantiate a form through its name
thanks for your info kkc, maybe your right i just wondering if there such way. hehehe.
Re: how can i instantiate a form through its name
Yes There is a way... it involves reflection.
Code:
static void Main()
{
//Application.Run(new Form1());
Application.Run((System.Windows.Forms.Form) CreateInstance("Form2"));
}
private static System.Object CreateInstance(System.String TypeName)
{
System.Reflection.Assembly ThisAssembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Type[] MyTypes = ThisAssembly.GetTypes();
for (int i=0; i<MyTypes.Length; i++)
{
if(String.Compare(MyTypes[i].Name, TypeName) == 0)
return System.Activator.CreateInstance(MyTypes[i]);
}
return null; // Failed
}
My C# is poor but you should get the Idea....
Re: how can i instantiate a form through its name
thanks there <ABX, i have now the idea on it through the code you'd given me. :bigyello: