Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a series of TextBoxes and corresponding Labels at runtime in Visual Basic .NET
DescriptionThis example shows how to make a series of TextBoxes and corresponding Labels at runtime in Visual Basic .NET.
KeywordsTextBox, Label, runtime, create controls, make controls, example, example program, Windows Forms programming, Visual Basic .NET, VB.NET
CategoriesControls, Controls
 

When the program starts, it uses the following code to create a series of TextBoxes with corresponding Labels.

 
' Make some Labels and TextBoxes.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Coordinates for the controls.
    Dim y As Integer = 8

    ' The labels to display.
    Dim captions() As String = {"Name:", "Street:", "City:", _
        "State:", "ZIP:"}
    For Each caption As String In captions
        ' Make the Label.
        Dim new_label As New Label()
        new_label.Parent = Me
        new_label.Text = caption
        new_label.Location = New Point(8, y)
        new_label.AutoSize = True

        ' Make the TextBox.
        Dim new_textbox As New TextBox()
        new_textbox.Parent = Me
        new_textbox.Location = New Point(50, y)
        new_textbox.Name = "txt" & caption.Replace(":", "")

        ' Move down.
        y += new_textbox.Height + 4
    Next caption
End Sub
 
The code defines the captions that the Labels should display. It then loops through the captions creating Labels and TextBoxes. About the only non-obvious thing to remember here is that you must set the controls' Parent property to the form.
 
 
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated