2

(第 1 部分)我想为每个动态创建的标签页添加 8 个复选框。我尝试了以下操作,但它在最后一个标签页上添加了一个复选框。如何为每个标签页添加 8 个复选框(比如 10 个标签页)

(第 2 部分)由于我们不知道创建的复选框的索引,我无法理解如何将组框与“checkbox.checked”条件链接。任何帮助将不胜感激。

Dim chkbx As New CheckBox

For j = 1 To 10 '#10 tab pages considered
Form4.TabControl1.TabPages.Add(j)
Form4.TabControl1.TabPages(j).Text = "Folder" & j + 1
Form4.TabControl1.TabPages(j).Controls.Add(chkbx)
chkbx.Text = "folder1name "
chkbx.Size = New Size(220, 17)
Next (j)

谢谢

4

2 回答 2

1

每次for循环创建 CheckBox 实例

For j = 0 To 9 '#10 tab pages considered
  Dim chkbx As New CheckBox
  Form4.TabControl1.TabPages.Add(j)
  Form4.TabControl1.TabPages(j).Text = "Folder" & j + 1
  Form4.TabControl1.TabPages(j).Controls.Add(chkbx)
  chkbx.Text = "FolderName"
  chkbx.Size = New Size(220, 17)
  chkbx.Name="A" & j  <----- set unique name/key to each control
Next (j)

并使用Controls.Find()方法获取参考control

Dim name = "A0"
Dim controlArray = Me.Controls.Find(name, True)
If controlArray.Length <> 0  Then
   Dim chk As CheckBox = CType(controlArray(0), CheckBox)
   If Not IsNothing(chk) Then
       MsgBox(chk.Checked)
   End If
End If
于 2012-09-27T03:07:20.160 回答
1

为了配合 AVD 所说的,在您分配动态 TabPages 的循环中创建您的 CheckBox。您将需要预先创建组合框或在另一个 for 循环中动态创建它们。然后,我将使用他们的 Tag 属性为每个 CheckBox 分配一个唯一 ID,并使用它来区分在 Common CheckedChanged 事件中更改了哪个。像这样的东西。

Dim index As Integer = 0
For j = 1 To 10 '#10 tab pages considered 
    Dim startLocation As Point = New Point(10, 10)
    TabControl1.TabPages.Add(New TabPage)
    TabControl1.TabPages(j).Text = "Folder" & j + 1
    For x = 0 To 7
        Dim chkbx As New CheckBox
        TabControl1.TabPages(j).Controls.Add(chkbx)
        chkbx.Tag = index
        chkbx.Text = "FolderName"
        chkbx.Location = startLocation
        chkbx.Size = New Size(220, 17)
        AddHandler chkbx.CheckedChanged, AddressOf CheckBox_CheckedChanged
        startLocation.Y = startLocation.Y + 16
        index += 1
    Next
Next

CheckChanged 事件

Private Sub CheckBox_CheckedChanged(sender As System.Object, e As System.EventArgs)
    Dim cb As CheckBox = CType(sender, CheckBox)

    Select Case CInt(cb.Tag)
        Case 0

        Case 1

        Case 2

        Case 3

        Case .... 'Up to how ever many index's that you have

        Case Else

    End Select

End Sub
于 2012-09-27T03:38:12.853 回答