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

时间:2016-05-17 10:16:18

标签: vb.net winforms textbox

以下代码为组框创建/格式化标签和文本框。

我的问题是,我想定期将文本框文本更改为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"

......等等。

谢谢!

2 个答案:

答案 0 :(得分:1)

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个文本框提供值&#39; 1&#39;,请使用上述相同的代码修改这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

答案 1 :(得分:0)

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

你把一切都变暗的地方:

Dim even as boolean

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

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