Results 1 to 3 of 3

Thread: create an instance of a Form class just from knowing its name in a string

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474

    create an instance of a Form class just from knowing its name in a string

    I have seen this question here and there and didnt find a solution for it until recently (in SyncFusion )

    So for those who may want to know:
    You can use the System.Reflection.Assembly.CreateInstance method to create a form from its name. Below is a code snippet. The name in the textbox has to be the full name including its namespace. So if there is a class named Form2 in namespace MyCompanyName, then the textbox would contain MyCompanyName.Form2. This snippet also assumes that the class is defined in the current executing assembly. If not, you would have to create an instance of the assembly that contains the class instead of calling the static method GetExecutingAssembly. As noted on this board, using reflection in this manner might affect performance.
    VB Code:
    1. [C#]
    2.  
    3.      try
    4.  
    5.      {
    6.            Assembly tempAssembly = Assembly.GetExecutingAssembly();
    7.            // if class is located in another DLL or EXE, use something like
    8.            // Assembly tempAssembly = Assembly.LoadFrom("myDLL.DLL");
    9.            // or
    10.            // Assembly tempAssembly = Assembly.LoadFrom("myEXE.exe");
    11.            Form frm1 = (Form) tempAssembly.CreateInstance(textBox1.Text);// as Form;
    12.            frm1.Show();
    13.       }
    14.       catch(Exception ex)
    15.       {
    16.            MessageBox.Show("Error creating: "+ textBox1.Text);
    17.       }
    18.  
    19. [VB.NET]
    20.  
    21.      textBox1.Text = "MyNameSpace.Form2"
    22.  
    23.      ......
    24.       Try
    25.       Dim tempAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
    26.       ' if class is located in another DLL or EXE, use something like
    27.       ' tempAssembly = Assembly.LoadFrom("myDLL.DLL")
    28.      ' or  
    29.      ' tempAssembly = Assembly.LoadFrom("myEXE.exe")  
    30.      Dim frm1 As Form = CType(tempAssembly.CreateInstance(textBox1.Text), Form) ' as Form;
    31.       frm1.Show()
    32.      Catch ex As Exception
    33.           MessageBox.Show("Error creating: " + ex.ToString())
    34.      End Try
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can also do it like this:
    VB Code:
    1. Dim frm As Form = Activator.CreateInstance(Type.GetType("Namespace.ObjectName"))
    2.         frm.Show()

    Which is basically the samething it just uses the Activator object instead of getting a reference to the current assembly.
    Last edited by Edneeis; Apr 11th, 2003 at 04:17 AM.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Thanks
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width