HOW TO: Programmatically Add Controls to Windows Forms at Run Time by Using Visual Basic .NET
The information in this article applies to:
* Microsoft Visual Basic .NET (2002)
* Microsoft .NET Framework SDK 1.0
This article was previously published under Q308433
For a Microsoft Visual C# .NET version of this article, see 319266.
For a Microsoft Visual Basic 6.0 version of this article, see 190670.
IN THIS TASK
* SUMMARY
*
o Requirements
o Create a Windows Forms Application
o Customize Form and Control Properties
o Add Controls to the Form
o Verify that it Works
* REFERENCES
SUMMARY
This step-by-step article demonstrates how to programmatically add and configure a few, commonly used controls within a Windows 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 .NET, or you can add and configure controls programmatically at run time.
back to the top
Requirements
This article assumes that you are familiar with the following topics:
* Visual Basic syntax
* Visual Studio .NET environment
* Purpose of common Visual Basic controls
back to the top
Create a Windows Forms Application
1. Start 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
'Controls
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()
back to the top
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
.Items.Add("One")
.Items.Add("Two")
.Items.Add("Three")
.Items.Add("Four")
.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
back to the top
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
.Add(btnAdd)
.Add(txtBox)
.Add(lstBox)
.Add(chkBox)
.Add(lblCount)
End With
2. Save the project.
back to the top
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.
back to the top
REFERENCES
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.
back to the top
Last Reviewed: 1/30/2003
Keywords: kbHOWTOmaster KB308433 kbAudDeveloper