Create controls at run time with events

Updated Reverend Jim 3 Tallied Votes 658 Views Share

This code demonstrates how to add controls to a form at run time. The number of rows and columns, the spacing between the controls and the size of the controls are all determined by Consts, but this could easily be changed so that the parameters are user entered. My example adds two handlers to each textbox. I will be posting a refinement to this that uses a TableLayoutPanel to position the controls. Suggestions on how to improve this code are always welcome.

G_Waddell commented: useful +4
Stuugie commented: Nice code snippet for a noob like me. Thanks for taking the time. +1
'                                                                                               
'  Name:                                                                                        
'                                                                                               
'    DynamicTextBoxes                                                                           
'                                                                                               
'  Description:                                                                                 
'                                                                                               
'    Simple example of how to create and use textboxes at run time. The number of rows and      
'    columns of textboxes are defined by ROWS and COLS. The spacing between controls is defined 
'    by XPAD and YPAD. The control size is defined by TBWIDTH and TBHEIGHT and the location of  
'    the control at the top left is defined by XSTART and YSTART.                               
'                                                                                               
'  Audit:                                                                                       
'                                                                                               
'    2012-05-20  Reverend Jim                                                                   
'                                                                                               

Public Class Form1

    Const XPAD = 5          'horizontal spacing between controls
    Const YPAD = 5          'vertical spacing between controls  
    Const ROWS = 5          'number of rows of textboxes        
    Const COLS = 5          'number of columns of textboxes     

    Const TBWIDTH = 100     'text box width                     
    Const TBHEIGHT = 20     'text box height                    
    Const XSTART = 10       'x position of first text box       
    Const YSTART = 10       'y position of first text box       

    Private boxes(ROWS, COLS) As TextBox

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim newbox As TextBox

        'create a grid of textboxes

        For row As Integer = 0 To ROWS - 1
            For col As Integer = 0 To COLS - 1

                'calculate the position of the nex textbox

                Dim xpos As Integer = XSTART + row * (XPAD + TBWIDTH)
                Dim ypos As Integer = YSTART + col * (YPAD + TBHEIGHT)

                'create a new textbox and set its properties

                newbox = New TextBox
                newbox.Size = New Drawing.Size(TBWIDTH, TBHEIGHT)
                newbox.Location = New Point(xpos, ypos)
                newbox.Name = "TextBox" & row & "_" & col
                newbox.Text = newbox.Name

                'connect it to handlers, save a reference to the array and add it to the form controls

                AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
                AddHandler newbox.Enter, AddressOf TextBox_Enter

                boxes(row, col) = newbox
                Me.Controls.Add(newbox)

            Next
        Next

    End Sub

    Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs)

        'when you modify the contents of any textbox, the name of that textbox and
        'its current contents will be displayed in the title bar

        Dim box As TextBox = DirectCast(sender, TextBox)
        Me.Text = box.Name & ": " & box.Text

    End Sub

    Private Sub TextBox_Enter(sender As System.Object, e As System.EventArgs)

        'when you enter a textbox control, its current contents will be displayed
        'in the title bar

        Dim box As TextBox = DirectCast(sender, TextBox)
        Me.Text = box.Name & ": " & box.Text

    End Sub

End Class