3

我知道这个问题之前已经回答过,但我似乎无法得到一个可以满足我想要的答案,即能够检查我的动态创建的复选框是否已被选中,我需要能够检查每个复选框分别。

这是我创建复选框的代码

Private Sub CreateCheckbox(ByVal x As Integer, ByVal y As Integer, text As String, ByVal id As Integer)
    Dim btnYes As System.Windows.Forms.CheckBox
    btnYes = New System.Windows.Forms.CheckBox
    btnYes.AutoSize = True
    btnYes.TabStop = True
    btnYes.Name = "CheckBox" + id
    btnYes.TabIndex = id
    btnYes.Parent = Panel1
    btnYes.Size = New System.Drawing.Size(81, 17)
    btnYes.Location = New System.Drawing.Point(x, y)
    btnYes.TabIndex = 1
    btnYes.Text = text
    Button1.TabIndex = Button1.TabIndex + 1
    Me.Panel1.Controls.Add(btnYes)
End Sub

我需要类似的东西

for i as integer = 0 to id step 1
 if checkbox(i).checked = true then
   'Do Something
 end if
next
4

1 回答 1

1

您应该handler为您的动态创建添加checkboxes,给定的代码将创建一个check box并且顺便添加checkedChanged事件处理程序。

Private Sub CreateCheckbox(ByVal x As Integer, ByVal y As Integer, text As String, ByVal id As Integer)
    Dim btnYes As System.Windows.Forms.CheckBox
    btnYes = New System.Windows.Forms.CheckBox
    ...............
    ...............

    Me.Panel1.Controls.Add(btnYes)

    'Now here add a common handler for your created checkbox
     AddHandler btnYes.CheckedChanged addressOf CommonCheckedChangeHandler
End Sub

以下过程将根据我们上面添加的事件(checkedchanged)被调用。我们可以使用参数访问特定的复选框sender

private sub CommonCheckedChangeHandler(ByVal sender As Object, ByVal e As System.EventArgs)
   ' This method will be invoked when your dynamically
   ' created check box's checked state got changed
End sub

编辑1:

使用下面的代码遍历里面panel来检查你的checkboxes

For Each chkBox In Panel1.Controls.OfType(Of CheckBox)()
  'Check here whether your check boxes are checked true/false
Next

编辑2:

OfType 属于LINQ,如果你不想要上面的试试这个代码

for i as integer = 0 to id step 1
 if Ctype(panel1.controls("CheckBox" & i), checkbox).checked = true then
   'Do Something
 end if
next
于 2013-03-20T06:39:36.730 回答