PDA

Click to See Complete Forum and Search --> : how can i instantiate a form through its name[Resolved]


fret
May 12th, 2005, 04:01 AM
here...

'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:

sixfeetsix
May 12th, 2005, 01:31 PM
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 :
public void Show(System.Windows.Forms.Form formInit)
{
System.Windows.Forms.Form f;
f = formInit;
f.Show();
}

fret
May 13th, 2005, 03:41 AM
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.

kkc
May 19th, 2005, 02:40 AM
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

fret
May 19th, 2005, 11:31 PM
thanks for your info kkc, maybe your right i just wondering if there such way. hehehe.

<ABX
May 20th, 2005, 12:15 AM
Yes There is a way... it involves reflection.


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....

fret
May 23rd, 2005, 12:39 AM
thanks there <ABX, i have now the idea on it through the code you'd given me. :bigyello: