noidea noidea - 6 months ago 22
Vb.net Question

Add text to automatic labels and textboxes vb.net

The following code creates/formats labels and textboxes to a groupbox.

My problem is that i want to change the textbox text to

1
and
0
periodically ( like intercalated ), and i have no idea how to.

Private Sub ResizeData()
' Create as many textboxes as fit into window
grpData.Controls.Clear()
Dim x As Integer = 0
Dim y As Integer = 10
Dim z As Integer = 20
While y < grpData.Size.Width - 100
labData = New Label()
grpData.Controls.Add(labData)
labData.Size = New System.Drawing.Size(30, 20)
labData.Location = New System.Drawing.Point(y, z)


labData.Text = Convert.ToString(x + 1)

txtData = New TextBox()
grpData.Controls.Add(txtData)
txtData.Size = New System.Drawing.Size(50, 20)
txtData.Location = New System.Drawing.Point(y + 30, z)
txtData.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
txtData.Tag = x


x += 1
z = z + txtData.Size.Height + 5
If z > grpData.Size.Height - 40 Then
y = y + 100
z = 20
End If
End While

End Sub


i need something like this:

txtData1.text="1"
txtData2.text="0"
txtData3.text="1"
txtData4.text="0"


...and so on.

Thank you!

Answer
Private Sub ResizeData()
    ' Create as many textboxes as fit into window
    grpData.Controls.Clear()
    Dim a As Integer = 1
    Dim x As Integer = 1
    Dim y As Integer = 10
    Dim z As Integer = 20
    While y < grpData.Size.Width - 100

        Dim labData As New Label()
        grpData.Controls.Add(labData)
        labData.Size = New System.Drawing.Size(30, 20)
        labData.Location = New System.Drawing.Point(y, z)


        labData.Text = Convert.ToString(a)


        Dim txtData As New TextBox()
        grpData.Controls.Add(txtData)
        txtData.Size = New System.Drawing.Size(50, 20)
        txtData.Location = New System.Drawing.Point(y + 30, z)
        txtData.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        txtData.Tag = x
        txtData.Text = x

        a += 1
        If x = 1 Then
            x = 0
        ElseIf x = 0 Then
            x = 1
        End If

        z = z + txtData.Size.Height + 5
        If z > grpData.Size.Height - 40 Then
            y = y + 100
            z = 20
        End If
    End While

End Sub