There are 30 check box in a form, ckb0501, ckb0502...ckb0530. How to make a loop to check if any one of check boxes was checked?
Printable View
There are 30 check box in a form, ckb0501, ckb0502...ckb0530. How to make a loop to check if any one of check boxes was checked?
There are events for that.
Sorry. The event is for one by one. I want to make a loop like below:
for i = 1 to 30
if ckb050i.checked = true then
msgbox "ok"
end if
next
But, I do not know how to make ckb050i working.
Are they all in their own container? or just right on the form? Are there other checkbox's with them?
For any:
vb Code:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged For Each chk As CheckBox In Me.Controls.OfType(Of CheckBox)() If Not chk Is DirectCast(sender, CheckBox) AndAlso chk.Checked Then MsgBox("OK") Exit For End If Next End Sub
For all:
vb Code:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged For Each chk As CheckBox In Me.Controls.OfType(Of CheckBox)() If Not chk Is DirectCast(sender, CheckBox) AndAlso Not chk.Checked Then Exit Sub Next MsgBox("OK") End Sub
Thank you for help. There are another checkbox like ckb0601, ckb0701... and I do not want to check them. The cicatrix's code is for all checkbox.
My question is how change ckb050i to ckb0501, ckb0502...ckb0530 using a loop.
The easiest way is to put them (groups) in different containers (panels, groupboxes, etc).
Then you can check only the collection of the checkboxes of the needed container (use Panel1.Controls instead of Me.Controls if Panel1 is the container for them) in the examples above.