Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I dynamically load a control from an assembly (or DLL)?

You use System.Reflection to dynamically load a control. This sample loads the SpecControls.ColorControl from SpecControls.dll.

[C#]

using System.Reflection;

Assembly assembly = Assembly.LoadFrom( "SpecControls.dll" ); 
Type t = assembly.GetType( "SpecControls.ColorControl" ); 

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

[Visual Basic]

Imports System.Reflection

Dim assembly1 As Assembly = Assembly.LoadFrom("SpecControls.DLL") 
Dim t As Type = assembly1.GetType("SpecControls.ColorControl") 

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

Contributed from George Shepherd's Windows Forms FAQ



Page view counter