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.
RequirementsThis 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
- 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.
- Double-click Form1 to create and view the Form1_Load event procedure.
- In the first line of Form1.vb, add a reference to the color namespace before the definition of the Form1 class as follows.
- Add private instance variables to the Form1 class to work with common Windows controls. The Form1 class starts as follows.
Public Class Form1
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 propertiesTip You can use the With command to perform a series of statements on a specified object without requalifying the object's name.
- 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.
.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
- 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.
.BackColor = Gray
.Text = "Add"
.Location = New System.Drawing.Point(90, 25)
.Size() = New System.Drawing.Size(50, 25)
- Add the following code to customize the appearance of the TextBox control.
.Text = "Text"
.Location = New System.Drawing.Point(10, 25)
.Size() = New System.Drawing.Size(70, 20)
- Add the following code to customize the appearance of the ListBox control.
.Sorted = True
.Location = New System.Drawing.Point(10, 55)
.Size() = New System.Drawing.Size(130, 95)
- Add the following code to customize the appearance of the CheckBox control.
.Text = "Disable"
.Location = New System.Drawing.Point(15, 190)
.Size() = New System.Drawing.Size(110, 30)
- Add the following code to customize the appearance of the Label control.
.Text = lstBox.Items.Count & " items"
.Location = New System.Drawing.Point(55, 160)
.Size() = New System.Drawing.Size(65, 15)
Add controls to the form
- Add the following code to add each object to the Controls array of the form.
'Add controls to the form.
- Save the project.
Verify that it WorksTo 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.
Article ID: 308433 - Last Review: Dec 6, 2006 - Revision: 1