将文本添加到自动标签和文本框 vb.net

Add text to automatic labels and textboxes vb.net

下面的代码creates/formats 将标签和文本框添加到组框。

我的问题是我想定期将文本框文本更改为 10(如插页),但我不知道该怎么做。

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

我需要这样的东西:

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

...等等。

谢谢!

也许使用布尔值会起作用。不是更干净的方法,但它应该有效

你在哪里调暗一切:

Dim even as boolean

在 txtData 的所有 属性 更改之后:

if even then
    txtData.Text=1
else
    txtData.Text=0
end if
even= not even
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

要仅给前 2 个文本框赋值“1”,请使用上面相同的代码并修改以下 3 行:

 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