2016-05-17 145 views
0

以下代碼創建/格式化標籤和文本框到一個groupbox。將文本添加到自動標籤和文本框vb.net

我的問題是,我想改變文本框文本到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" 

...等等。

謝謝!

回答

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文本框值爲'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 
+0

我測試過的代碼和它的工作..與標籤!我想要在文本框中的文本(txtData.text)。謝謝 ! – noidea

+0

上面的代碼已更新:標籤增量,文本框之間1和0 – Veeke

+0

相當不錯!謝謝 ! – noidea

0

也許使用布爾值將工作。沒有清潔的方式,但它應該工作

當你暗淡的一切:

Dim even as boolean 

而且txtData的所有屬性更改後:

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

nope。沒有工作。它只會改變最後一個文本框的值。 – noidea