Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

2. Windows Forms Controls

2.29 How do I dynamically load a control from a DLL?


You use System Reflection to dynamically load a control. If the DLL is named "SpecControls.DLL" and the class you want is "SpecControls.ColorControl", then use this code.

[C#]
     // load the assembly
     System.Reflection.Assembly assembly = Assembly.LoadFrom("SpecControls.DLL");

     // get the type
     Type t = assembly.GetType("MyControls.MyControl");

     // create an instance and add it.
     //
     Control c = (Control)Activator.CreateInstance(t);
     parent.Controls.Add(c);

[VB.NET]
      ' load the assembly
     Dim assembly1 As System.Reflection.Assembly = Assembly.LoadFrom("SpecControls.DLL")

     ' get the type
     Dim t As Type = assembly1.GetType("MyControls.MyControl")

     ' create an instance and add it.
     '
     Dim c As Control = CType(Activator.CreateInstance(t), Control)
     parent.Controls.Add(c)