Click here to Skip to main content
15,881,594 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
VB
Dim frm As New Form
        Dim formName As String = "Form1"
        formName = [Assembly].GetEntryAssembly.GetName.Name & "." & formName
        frm = DirectCast([Assembly].GetEntryAssembly.CreateInstance(formName), Form)
        frm.Show()

//how to use this in c#
Posted
Comments
sahar j 30-Dec-11 16:00pm    
Dear developer
Hi,
When I use this lines in my code, there is a problem with [Assembly]. what should I do? thanks.

Do you mean convert it to C#?

C#
Form frm = new Form();
string formName = "Form1";
formName = string.Format("{0}.{1}", [Assembly].GetEntryAssembly.GetName.Name, formName);
frm = [Assembly].GetEntryAssembly.CreateInstance(formName) as Form;
frm.Show();


Or more succinctly:

C#
string name = string.Format("{0}.Form1", [Assembly].GetEntryAssembly.GetName.Name);
Form frm = [Assembly].GetEntryAssembly.CreateInstance(formName) as Form;
frm.Show();
 
Share this answer
 
v2
C#
string formName = "frmTestForm";
formName = Assembly.GetEntryAssembly().GetName().Name + "." + formName;
               
Type type = Type.GetType(formName);
Form form = (Form)Activator.CreateInstance(type);

form.StartPosition = FormStartPosition.CenterParent;
form.ShowDialog();
 
Share this answer
 
v3
Comments
fjdiewornncalwe 24-Jul-12 9:47am    
Please don't answer questions that are this old. It is unlikely that the OP is going to still be having issues with it. As well, there is an existing answer that is appropriate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900