将文本添加到自动标签和文本框vb.net
问题描述:
以下代码创建/格式化标签和文本框到一个groupbox。将文本添加到自动标签和文本框vb.net
我的问题是,我想改变文本框文本到1
和0
定期(如夹层),我不知道如何。
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"
...等等。
谢谢!
答
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
答
也许使用布尔值将工作。没有清洁的方式,但它应该工作
当你暗淡的一切:
Dim even as boolean
而且txtData的所有属性更改后:
if even then
txtData.Text=1
else
txtData.Text=0
end if
even= not even
+0
nope。没有工作。它只会改变最后一个文本框的值。 – noidea
我测试过的代码和它的工作..与标签!我想要在文本框中的文本(txtData.text)。谢谢 ! – noidea
上面的代码已更新:标签增量,文本框之间1和0 – Veeke
相当不错!谢谢 ! – noidea