How to create Dynamic Controls in VB.NET ?

How to create Control Arrays in VB .NET ?

In VB6 there is a feature called Control Arrays, where you name controls the same name and provide them an index value. This allows you to set a value by looping through the controls and setting each value.


How to create Dynamic Controls in VB.NET ?

One of the VB6 features that are no longer supported in VB.NET is the automated creation of control arrays by simply copying an existing control as many times as required into an array. The good news is that you can still set things up to do similar things.

The following program shows how to create a dynamic TextBox control in VB.NET and setting the properties dynamically for each TextBox control. Drag a Button control in the form and copy and paste the following source code . Here each Button click the program create a new TextBox control dyanmically.

Public Class Form1
    Dim cLeft As Integer = 1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    End Sub
    Public Function AddNewTextBox() As System.Windows.Forms.TextBox
        Dim txt As New System.Windows.Forms.TextBox()
        txt.Top = cLeft * 25
        txt.Left = 100
        txt.Text = "TextBox " & Me.cLeft.ToString
        cLeft = cLeft + 1
        Return txt
    End Function
End Class (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About