Welcome to ADO.NET Access 2003—your ultimate hub for VB.NET and ADO.NET programming excellence. Discover in-depth tutorials, practical code samples, and expert troubleshooting guides covering a broad range of topics—from building robust WinForms applications and seamless MS Access integration to working with SQL Server, MySQL, and advanced tools like WebView2 and Crystal Reports. Whether you're a beginner or a seasoned developer, our step-by-step articles are designed to empower you to optimize.

Looking for MS Access Developer❓❓

Application developer

Post Page Advertisement [Top]

How to Create Controls at Run-Time in 
Visual Basic

VB.NET OOP Create controls at run-time
Visual Basic Online Course

👨‍🏫Scenario

The example will show you how to create a control (TextBox) at the Application Runtime

This TextBox control will allow the user to input Numbers only

We will create a new TextBox control with event TextChanged() when the Form is clicked.

🔬WinForms Design

Create new VB.NET WinForms Application Project (MyPro), don't forget to always Save your project

⁉️How does it work

RUN your project (F5) then click on the Form (Form1) and you will notice that a TextBox control was created with the same properties that we provided in our example in the code below.

👂TextBox Properties

  • Location: The cursor location when click on theForm1
  • Size: 120,300
  • Parent:Form1
  • TextAlign: HorizontalAlignment.Left
  • ForeColor: Color.White
  • Name: MyNewText
  • Font: Times New Roman, 10, Regular, Point

👨‍💻Code

      'Visual Basic Online Course
'2014
'VB 2010 Create TextBox Control at Run-Time with events

Public Class Form1
   Public MyNewTXT As TextBox = New TextBox
   Const FHEIGHT As Integer = 120
   Const FWIDTH As Integer = 300
   Dim PointX, PointY As Integer
   
   Private Sub Form1_Click(sender As Object, _
   e As System.EventArgs) _
   Handles Me.Click
   
      Me.AutoScroll = True
      MyNewTXT.Name = "MyNewTXT"
      MyNewTXT.Parent = Me
      MyNewTXT.Size = New Size(FWIDTH, FHEIGHT)
      MyNewTXT.Location = New Point(PointX, PointY)
      MyNewTXT.TextAlign = HorizontalAlignment.Left
      MyNewTXT.ForeColor = Color.White
      MyNewTXT.ReadOnly = False
      MyNewTXT.Multiline = False
      MyNewTXT.Font = New System.Drawing.Font("times new roman", _
                                              10, _
                                              FontStyle.Regular, _
                                              GraphicsUnit.Point)
      MyNewTXT.BackColor = Color.Blue
      MyNewTXT.Visible = True
      'Event Txt_Changed : Triggers on our new TextBox Changed
      AddHandler MyNewTXT.TextChanged, AddressOf Txt_Changed
   End Sub
   
   Private Sub Txt_Changed(Thissender As Object, _
   e As System.EventArgs)
   
      Dim ThisToChange As String = Thissender.text
      'Allow only Numbers
      If Not IsNumeric(ThisToChange) Then
         SendKeys.Send("{BackSpace}")
      End If
   End Sub
   
   Private Sub Form1_MouseMove(sender As Object, _
   e As System.Windows.Forms.MouseEventArgs) _
   Handles Me.MouseMove
      PointX = e.Location.X
      PointY = e.Location.Y
   End Sub
End Class

🔗 GitHub Sample (Optional)

Visit GitHub Repository

🛜VB.NET OOP: Creating TextBox Controls at Runtime

In VB.NET, dynamically adding controls like TextBox at runtime is a common requirement, especially when the number of inputs isn't known at design time. This tutorial demonstrates how to create TextBox controls dynamically using object-oriented programming (OOP) principles.

🫧Understanding the Scenario

Imagine an application where users can add multiple entries dynamically. Instead of predefining a fixed number of TextBox controls, we can create them on-the-fly based on user interactions.

🐍Step-by-Step Implementation

  1. Design the Form:
    • Add a Button control named btnAddTextBox with the text "Add TextBox".
    • Add a Panel control named pnlContainer to host the dynamic TextBox controls.
  2. Write the Code:

    In the code-behind, implement the logic to add new TextBox controls dynamically.

    
    Public Class Form1
        Private textBoxCount As Integer = 0
    
        Private Sub btnAddTextBox_Click(sender As Object, e As EventArgs) Handles btnAddTextBox.Click
            ' Create a new TextBox instance
            Dim txtBox As New TextBox()
            txtBox.Name = "txtBox" & textBoxCount.ToString()
            txtBox.Width = 200
            txtBox.Location = New Point(10, 10 + (30 * textBoxCount))
    
            ' Optionally, add an event handler
            AddHandler txtBox.TextChanged, AddressOf DynamicTextBox_TextChanged
    
            ' Add the TextBox to the panel
            pnlContainer.Controls.Add(txtBox)
    
            ' Increment the counter
            textBoxCount += 1
        End Sub
    
        Private Sub DynamicTextBox_TextChanged(sender As Object, e As EventArgs)
            Dim txtBox As TextBox = CType(sender, TextBox)
            ' Handle the TextChanged event
            Console.WriteLine($"TextBox {txtBox.Name} text changed to: {txtBox.Text}")
        End Sub
    End Class
        

🎁Explanation

  • textBoxCount keeps track of the number of TextBox controls added.
  • Each new TextBox is positioned 30 pixels below the previous one to avoid overlap.
  • An optional TextChanged event handler is attached to each TextBox to handle text changes.

⬇️Conclusion

By leveraging OOP principles in VB.NET, we can dynamically create and manage controls at runtime, providing flexibility in UI design and user interactions.

Thank you for reading ....

 Here are some online Visual Basic lessons and courses:

Bottom Ad [Post Page]