我知道这个问题之前已经得到了回答,但我似乎无法得到一个符合我想要的答案,可以检查我的动态创建的复选框是否已经过检查,我需要能够检查每个复选框单独。
以下是我创建复选框的代码
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
答案 0 :(得分:0)
您应该为动态创建的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