Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void SubMenuClicked(object sender, EventArgs e)
            {
                  Form _form;
                  string name = "frm";      
                  name+=((MenuItem)sender).Text;
                  _form = (Form)(name);
                  _form.ShowDialog();
            }

I am getting sum text and want to open that form on the basis of that text.
Posted
Updated 25-Aug-11 1:19am
v2
Comments
Prerak Patel 25-Aug-11 7:19am    
Use code block for code segments.

Replace
C#
_form = (Form)(name);
_form.ShowDialog();

with
C#
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
Form f = null;
foreach (Assembly a in assemblies)
{
    Type[] types = a.GetTypes();
    foreach (Type t in types)
    {
        if (t.IsPublic && t.BaseType == typeof(Form))
        {
            if (t.FullName.Contains(name))
            {
                f = (Form)a.CreateInstance(t.FullName);
            }
        }
    }
}

if (f != null)
    f.ShowDialog();
 
Share this answer
 
Comments
#realJSOP 25-Aug-11 7:27am    
5 - propose as answer.
Abhijit Ghosh (Subho) 21-Oct-14 2:56am    
vote of 5
VB
' with reflection

Dim myAssembly As Assembly = Assembly.GetExecutingAssembly()

For Each t As Type In myAssembly.GetTypes()
  If t.IsPublic And t.BaseType.Name = "Form" Then
    If t.Name = NomeForm Then
      _obj = t.InvokeMember("", BindingFlags.CreateInstance, System.Type.DefaultBinder, New Object, Nothing)
      frm = CType(_obj, Form)
      frm.MdiParent = Me
      frm.Show()
      Exit For
    End If
  End If
Next
 
Share this answer
 
You code is invalid, you cannot instantiate forms like this. If what you are trying to do is create and open instances of different forms in your project based on different menu item selections, you can use some code like this:
C#
switch(((MenuItem)sender).Text) {
    case "Item1":
        Form1 f1 = new Form1();
        f1.Show();
    case "Item2":
        Form2 f2 = new Form2();
        f2.Show();
}
 
Share this answer
 
Comments
#realJSOP 25-Aug-11 7:27am    
Ummm, no. The most correct way (and surely what the user was after) was Prerak Patel's solution.

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