How to programmatically add controls to Windows Forms at run time by using Visual Basic 2005 or Visual Basic .NET

For a Microsoft Visual C# 2005 and Microsoft Visual C# .NET version of this article, see 319266 .

For a Microsoft Visual Basic 6.0 version of this article, see 190670 .


This step-by-step article demonstrates how to programmatically add and configure a few, commonly used controls within a Microsoft Windows Forms application. Event handling has been omitted from the sample code.

The Microsoft .NET Framework Software Development Kit (SDK) provides many visual controls that you can use to build a Windows Forms application. You can add and configure controls at design time in Microsoft Visual Studio 2005 or in Microsoft Visual Studio .NET, or you can add and configure controls programmatically at run time.


This article assumes that you are familiar with the following topics:
  • Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET syntax
  • The Visual Studio 2005 or Visual Studio .NET environment
  • The purpose of common Visual Basic controls

Create a Windows Forms application

  1. Start Visual Studio 2005 or Visual Studio .NET, and create a new Visual Basic Windows Application project named WinControls. Form1 is added to the project by default.
  2. Double-click Form1 to create and view the Form1_Load event procedure.
  3. In the first line of Form1.vb, add a reference to the color namespace before the definition of the Form1 class as follows.
    Imports System.Drawing.Color
  4. Add private instance variables to the Form1 class to work with common Windows controls. The Form1 class starts as follows.
    Imports System.Drawing.Color
    Public Class Form1
    Inherits System.Windows.Forms.Form

    Private txtBox As New TextBox()
    Private btnAdd As New Button()
    Private lstBox As New ListBox()
    Private chkBox As New CheckBox()
    Private lblCount As New Label()

Customize form and control properties

Tip You can use the With command to perform a series of statements on a specified object without requalifying the object's name.
  1. Locate to the Form1_Load event procedure, and add the following code to the procedure to customize the appearance of the Form control.
    'Set up the form.
    With Me
    .MaximizeBox = False
    .MinimizeBox = False
    .BackColor = White
    .ForeColor = Black
    .Size = New System.Drawing.Size(155, 265)
    .Text = "Run-time Controls"
    .FormBorderStyle = FormBorderStyle.FixedDialog
    .StartPosition = FormStartPosition.CenterScreen
    End With
  2. Add the following code to the Form1_Load event procedure to customize the appearance of the Button control.
    'Format controls. Note: Controls inherit color from parent form.
    With Me.btnAdd
    .BackColor = Gray
    .Text = "Add"
    .Location = New System.Drawing.Point(90, 25)
    .Size() = New System.Drawing.Size(50, 25)
    End With
  3. Add the following code to customize the appearance of the TextBox control.
    With Me.txtBox
    .Text = "Text"
    .Location = New System.Drawing.Point(10, 25)
    .Size() = New System.Drawing.Size(70, 20)
    End With
  4. Add the following code to customize the appearance of the ListBox control.
    With Me.lstBox
    .Sorted = True
    .Location = New System.Drawing.Point(10, 55)
    .Size() = New System.Drawing.Size(130, 95)
    End With
  5. Add the following code to customize the appearance of the CheckBox control.
    With Me.chkBox
    .Text = "Disable"
    .Location = New System.Drawing.Point(15, 190)
    .Size() = New System.Drawing.Size(110, 30)
    End With
  6. Add the following code to customize the appearance of the Label control.
    With Me.lblCount
    .Text = lstBox.Items.Count & " items"
    .Location = New System.Drawing.Point(55, 160)
    .Size() = New System.Drawing.Size(65, 15)
    End With

Add controls to the form

  1. Add the following code to add each object to the Controls array of the form.
    'Add controls to the form.
    With Me.Controls
    End With
  2. Save the project.

Verify that it Works

To verify that the sample works, click Start on the Debug menu. Note that although the form and the controls appear, they currently do nothing because you have not written any event handlers.


For more information about using controls programmatically, see the Windows Applications topic in the Visual Basic section of the Visual Studio .NET Online Help documentation.

ID do Artigo: 308433 - Última Revisão: 6 de dez de 2006 - Revisão: 1