noidea
noidea

Reputation: 184

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!

Upvotes: 0

Views: 380

Answers (2)

Veeke
Veeke

Reputation: 239

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

To give only the first 2 textboxes a value '1', use the same code above with these 3 lines modified:

 Dim txtData As New TextBox() With {.Name = "txt" & a}
 txtData.Text = 0
 If txtData.Name = "txt1" Or txtData.Name = "txt2" Then txtData.Text = 1 'add this line just below the above one

Upvotes: 1

Aimnox
Aimnox

Reputation: 899

Maybe using a boolean would work. Not the cleaner way but it should work

Where you dim everything:

Dim even as boolean

And after all the property changes of the txtData:

if even then
    txtData.Text=1
else
    txtData.Text=0
end if
even= not even

Upvotes: 0

Related Questions