|
-
May 12th, 2005, 04:01 AM
#1
Thread Starter
Hyperactive Member
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.
Last edited by fret; May 23rd, 2005 at 12:40 AM.
-
May 12th, 2005, 01:31 PM
#2
Addicted Member
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();
}
There are no stupid questions, but a whole bunch of dumb sayings !
Save time on database code, try DataLG !
-
May 13th, 2005, 03:41 AM
#3
Thread Starter
Hyperactive Member
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.
-
May 19th, 2005, 02:40 AM
#4
Lively Member
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
-
May 19th, 2005, 11:31 PM
#5
Thread Starter
Hyperactive Member
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.
-
May 20th, 2005, 12:15 AM
#6
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....
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
May 23rd, 2005, 12:39 AM
#7
Thread Starter
Hyperactive Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|