Microsoft KB Archive/308433

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

Article ID: 308433

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2

-



This article was previously published under Q308433







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.



SUMMARY
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.

Requirements
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
 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. Imports System.Drawing.Color   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 

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.   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 = &quot;Run-time Controls&quot; .FormBorderStyle = FormBorderStyle.FixedDialog .StartPosition = FormStartPosition.CenterScreen End With </li>  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 = &quot;Add&quot; .Location = New System.Drawing.Point(90, 25) .Size = New System.Drawing.Size(50, 25) End With </li>  Add the following code to customize the appearance of the TextBox control. With Me.txtBox .Text = &quot;Text&quot; .Location = New System.Drawing.Point(10, 25) .Size = New System.Drawing.Size(70, 20) End With </li>  Add the following code to customize the appearance of the ListBox control. With Me.lstBox .Items.Add(&quot;One&quot;) .Items.Add(&quot;Two&quot;) .Items.Add(&quot;Three&quot;) .Items.Add(&quot;Four&quot;) .Sorted = True .Location = New System.Drawing.Point(10, 55) .Size = New System.Drawing.Size(130, 95) End With </li>  Add the following code to customize the appearance of the CheckBox control. With Me.chkBox .Text = &quot;Disable&quot; .Location = New System.Drawing.Point(15, 190) .Size = New System.Drawing.Size(110, 30) End With </li>  Add the following code to customize the appearance of the Label control. With Me.lblCount .Text = lstBox.Items.Count &amp; &quot; items&quot; .Location = New System.Drawing.Point(55, 160) .Size = New System.Drawing.Size(65, 15) End With </li></ol>

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. With Me.Controls .Add(btnAdd) .Add(txtBox) .Add(lstBox) .Add(chkBox) .Add(lblCount) End With </li> Save the project.</li></ol>

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.

<div class="references_section">