Microsoft Knowledge Base Email Alertz

KBAlertz.com: Visual Basic .NET provides many visual controls that you can use to build a Windows Forms application. You can add and configure controls at design time within Microsoft Visual Studio .NET, or you can add and configure controls programmatic

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 308433 - Last Review: December 6, 2006 - Revision: 3.3

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

This article was previously published under Q308433
For a Microsoft Visual C# 2005 and Microsoft Visual C# .NET version of this article, see 319266  (http://kbalertz.com/Feedback.aspx?kbNumber=319266/ ) .
For a Microsoft Visual Basic 6.0 version of this article, see 190670  (http://kbalertz.com/Feedback.aspx?kbNumber=190670/ ) .

On This Page

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

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

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.

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.

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.

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
Keywords: 
kbvs2005applies kbvs2005swept kbhowtomaster KB308433
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please