Monday, November 26, 2012

Add Control in your form dynamically


Add Control in your form dynamically



The form designer is very fun to use and useful to create form and usercontrol. Each time you add a control in your form, you have to think about the position of each control in that form. So is a pain in the butt.


I’ll make a little sample on adding control dynamically in a form. I’m too lazy to make something complicated. I’ll make a simple table.


  'adding controls to make a table
        Dim oTextbox As TextBox
        For index1 As Integer = 0 To 3 Step 1
            For index2 As Integer = 0 To 3 Step 1
                oTextbox = New TextBox()
                With oTextbox
                    .Name = "TextBox" & index1.ToString & index2.ToString
                    .Text = index1.ToString & index2.ToString
                    .Width = Me.Width / 4
                    .Left = index1 * (Me.Width / 4)
                    '.Height
                    .Top = index2 * .Height
                End With
                oForm.Controls.Add(oTextbox)
            Next index2
        Next index1



If you use the show function to display your dynamically created form, you could simply put it before you call the show function or after. If you use ShowDialog, your control will not be added in your form because your form is modal.



    Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click

        Dim oForm As Form
        oForm = New Form
        oForm.Show()

        'adding controls to make a table
        Dim oTextbox As TextBox
        For index1 As Integer = 0 To 3 Step 1
            For index2 As Integer = 0 To 3 Step 1
                oTextbox = New TextBox()
                With oTextbox
                    .Name = "TextBox" & index1.ToString & index2.ToString
                    .Text = index1.ToString & index2.ToString
                    .Width = Me.Width / 4
                    .Left = index1 * (Me.Width / 4)
                    '.Height
                    .Top = index2 * .Height
                End With
                oForm.Controls.Add(oTextbox)
            Next index2
        Next index1
    End Sub




When you run the program, your new form will look like this:


Table created dynamically



I don’t want to make big post. That is the reason I’ll stop here. There are many things we could do here.



You could “improve” your code using a try and catch to control any program crash. This make you program better in quality. I also put the dim outside the try-catch. If something goes wrong in the loop, at least, it will display the index1 that cause the problem.




    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim oForm As Form
        Dim oTextbox As TextBox
        Dim index1 As Integer
        Dim index2 As Integer
        Try
            oForm = New Form
            oForm.Text = "TABLE crfeated dynamically"
            oForm.Show()

            'adding controls to make a table

            For index1 = 0 To 3 Step 1
                For index2 = 0 To 3 Step 1
                    oTextbox = New TextBox()
                    With oTextbox
                        .Name = "TextBox" & index1.ToString & index2.ToString
                        .Text = index1.ToString & index2.ToString
                        .Width = Me.Width / 4
                        .Left = index1 * (Me.Width / 4)
                        '.Height
                        .Top = index2 * .Height
                    End With
                    oForm.Controls.Add(oTextbox)
                Next index2
            Next index1
        Catch ex As Exception
            MsgBox(ex.StackTrace & vbCrLf & "index1= " & index1 & vbCrLf & "index2= " & index2)
        End Try
    End Sub



If you like this post, leave a comment or share it.

You could also visit my web site at http://checktechno.ca



2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Great, Let us assume that you have created a picturebox instead of a textbox. May I ask you how you would do in order to select (on the fly) that new picturebox and change its properties ?
    Thank you

    ReplyDelete