.NET & Dynamically Created Controls

.NET (C# and VB.NET) doesn't really make a distinction between design-time created controls and controls created at run-time. But, it's not immediately obvious how to create ActiveX controls at run-time.

A simple trick may help. Visual Studio automatically generates all the needed code when you drop a control on a form. But, it hides it in hidden files. For example, while you are editing Form1.cs or Form1.vb, the "design-time" code is placed into Form1.Designer.cs or Form1.Designer.vb. These are great places to learn how to create controls dynamically.

We have created a sample for both VB and C#, which you can recreate by creating a new project with an empty form. Then add an ActiveX control (in this example we used SftBox/OCX) and a button control (named button1). Add the following code:


Public Class Form1

Friend WithEvents AxSftBox2 As AxSftBoxLib40.AxSftBox

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    ' Create one SftBox control based on a designed control 
    ' (named AxSftBoxDesigned)

    Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))

    Me.AxSftBox2 = New AxSftBoxLib40.AxSftBox
    CType(Me.AxSftBox2, System.ComponentModel.ISupportInitialize).BeginInit()


    Me.AxSftBox2.Location = New System.Drawing.Point(27, 27)
    Me.AxSftBox2.Name = "AxSftBox2"
    ' THIS IS IMPORTANT: We are using the properties as defined by AxSftBoxDesigned to create the dynamic control
    Me.AxSftBox2.OcxState = CType(resources.GetObject("AxSftBoxDesigned.OcxState"), System.Windows.Forms.AxHost.State)
    Me.AxSftBox2.Size = New System.Drawing.Size(249, 38)
    Me.AxSftBox2.Location = New Point(Me.AxSftBoxDesigned.Location.X + 40, Me.AxSftBoxDesigned.Location.Y + 40)
    Me.AxSftBox2.TabIndex = 0


    CType(Me.AxSftBox2, System.ComponentModel.ISupportInitialize).EndInit()

End Sub
End Class


namespace CS_WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {

        private AxSftBoxLib40.AxSftBox axSftBoxDesign;

        private AxSftBoxLib40.AxSftBox axSftBoxRuntime;

        private void button1_Click(object sender, EventArgs e) {

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

            axSftBoxRuntime = new AxSftBoxLib40.AxSftBox();
            axSftBoxRuntime.Location = new System.Drawing.Point(axSftBoxDesign.Location.X + 50, axSftBoxDesign.Location.Y + 50);
            axSftBoxRuntime.Name = "axSftBoxRuntime";
            // THIS IS IMPORTANT: We are using the properties as defined by AxSftBoxDesign to create the dynamic control
            axSftBoxRuntime.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftBoxDesign.OcxState")));
            axSftBoxRuntime.Size = new System.Drawing.Size(258, 38);
            axSftBoxRuntime.TabIndex = 0;


The key in both samples is to set the OcxState property of the dynamic control with the property settings of the "designed" control. This has the convenient benefit, that you can set all relevant properties using the property pages. These are then copied for all dynamic controls.

You will have to "design" at least one control. If you don't want to actually use the designed control, simply disable and hide it using the Enabled and Visible properties. Or, you could have multiple designed controls that you can use as templates. Then, when you create a dynamic control, select the property set (for the OcxState property) which closest matches your desired settings.