Home

Dynamic Control Creation

 

Introduction

The objects used in a Windows application are defined in various assemblies. To add one of these controls to your application, you must first know the name of its class. With this information, you can declare a variable of its class. For example, a command button is an object of type Button that is based on the Button class. The Button class is defined in the System.Windows.Forms namespace of the System.Windows.Forms.dll assembly. Based on this, to create a button, you can create a variable of type Button. Here is an example:

Imports System
Imports System.Windows.Forms

Module Exercise

    Public Class Exercise
        Inherits Form

        Private btnSubmit As Button

        Public Sub New()

        End Sub

        Public Shared Function Main() As Integer

            Application.Run(New Exercise())
            Return 0

        End Function
    End Class
End Module

After declaring the variable, you can use the New operator to allocate memory for it:

Public Sub New()
            btnSubmit = New Button()

End Sub

This is also referred to as dynamically creating a control. After declaring the variable and allocating memory for it, the control is available but does not have a host, which makes it invisible. A control must be positioned on a container, like a form. The Form class itself contains a member variable named Controls. This member holds a list of the objects that are placed on the form. To specify that a control you have instantiated must be positioned on a form, the Controls member has a method named Add. Therefore, to make an object part of the form, pass its variable to the Add() method. Here is an example:

Imports System
Imports System.Windows.Forms

Module Exercise

    Public Class Exercise
        Inherits Form

        Private btnSubmit As Button

        Public Sub New()
            btnSubmit = New Button()
            Controls.Add(btnSubmit)
        End Sub

        Public Shared Function Main() As Integer

            Application.Run(New Exercise())
            Return 0

        End Function
    End Class
End Module

This makes it possible for  the control to appear on the form when the form displays to the user:

Dynamic Control

The two techniques of visual addition of objects and dynamic creation are the most used to add Windows controls to an application. The Windows controls are also called components.

Initializing the Components

Because there can be many controls used in a program, instead of using the constructor to initialize them, the Visual Studio standards recommend that you create a sub procedure called InitializeComponent to initialize the various objects used in your application. Then simply call that method from the constructor of your form. This would be done as follows:

Imports System
Imports System.Windows.Forms

Module Exercise

    Public Class Exercise
        Inherits Form

        Private btnSubmit As Button

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

            btnSubmit = New Button()
            Controls.Add(btnSubmit)

        End Sub

        Public Shared Function Main() As Integer

            Application.Run(New Exercise())
            Return 0

        End Function
    End Class
End Module

Notice that the control is created in the InitializeComponent() method.

Using a Partial Class

Starting in Microsoft Visual Basic 2005, and probably getting close to C++, you can use two files to create and use a form. Each file would hold a partial definition of the class. As done in a header file of a C++ application, the first file in VBasic would hold the variable  or control declarations. While in C++ a header file holds the same name (but different extensions) as its corresponding source file, because VBasic does not have the concepts of header and source file, each file must have a different name. In Microsoft Visual Basic, the name of the first file of a form starts with the name of the form, followed by a period, followed by Designer, followed by a period, and followed by the vb extension.

Components Tracking on an Application

As you add and remove components on an application, you need a way to count them to keep track of what components, and how many of them, your application is using. To assist you with this, the .NET Framework provides a class named Container. This class is defined in the ComponentModel namespace that is itself part of the System namespace. To use a variable of this class in your application, declare a variable of type Container. Because no other part of the application is interested in this variable, you should declare it private. This can be done as follows:

Imports System
Imports System.Windows.Forms

Module Exercise

    Public Class Exercise
        Partial Public Class Exercise
            Inherits Form

            Private btnSubmit As Button

            Dim components As System.ComponentModel.Container

            Public Sub New()
                InitializeComponent()
            End Sub

            Public Sub InitializeComponent()
                btnSubmit = New Button()
                
                Controls.Add(btnSubmit)
            End Sub


        End Class

        Public Shared Function Main() As Integer

            Application.Run(New Exercise())
            Return 0

        End Function

    End Class
End Module

After this declaration, the compiler can keep track of the components that are part of the form.

Control Derivation

If you are using a .NET Framework control, you must know the name of the class on which the control is based (and each control is based on a particular class). If you have examined the types of classes available but none implements the behavior you need, you can first locate one that is close to the behavior you are looking for, then use it as a base to derive a new class.

To derive a class from an existing control, you can use your knowledge of inheritance. Here is an example:

Public Class Numeric
        Inherits System.Windows.Forms.TextBox

End Class

If you want to perform some early initialization to customize your new control, you can declare a constructor. Here is an example:

Public Class Numeric
        Inherits System.Windows.Forms.TextBox

        Public Sub New()

        End Sub
End Class

Besides the constructor, in your class, you can add the fields and methods as you see fit. You can also use it to globally set a value for a variable of the parent class. Once the control is ready, you can dynamically use it like any other control. Here is an example:

Imports System
Imports System.Windows.Forms

Module Exercise

    Public Class Numeric
        Inherits System.Windows.Forms.TextBox

        Public Sub New()

        End Sub
    End Class

    Public Class Exercise
        Partial Public Class Exercise
            Inherits Form

            Private btnSubmit As Numeric

            Dim components As System.ComponentModel.Container

            Public Sub New()
                InitializeComponent()
            End Sub

            Public Sub InitializeComponent()
                btnSubmit = New Numeric()
                
                Controls.Add(btnSubmit)
            End Sub


        End Class

        Public Shared Function Main() As Integer

            Application.Run(New Exercise())
            Return 0

        End Function

    End Class
End Module

This produce:

Dynamic Control

 

Home Copyright © 2008 FunctionX, Inc. Home