How to loop through multiple checkboxes

T

Tim

On my form I have 10 checkboxes named chkbox1,chkbox2,....chkbox10. I would
have like to set it up as an array control like in VB6 where I could have
chkbox(1),chkbox(2) but I think .net you have to have specific names for
each control. All 10 of the boxes are in a groupbox. What I'm trying to do
is when I hit a button it will loop through all the checkboxes to see which
ones are checked and add that checkbox name to a string. Something like
Names = chkbox1.text & " " chkbox2.text & " " ect... Once I got a list of
all the ones that were checked all 10 would be reset and unchecked to start
over.

Thanks,

Tim
 
A

Armin Zingler

Tim said:
On my form I have 10 checkboxes named chkbox1,chkbox2,....chkbox10.

You can use expressive names instead of numbers.
I would have like to set it up as an array control like in VB6 where
I could have chkbox(1),chkbox(2) but I think .net you have to have
specific names for each control.

You don't have to, but you can.
All 10 of the boxes are in a
groupbox. What I'm trying to do is when I hit a button it will loop
through all the checkboxes to see which ones are checked and add that
checkbox name to a string. Something like Names = chkbox1.text & " "
chkbox2.text & " " ect... Once I got a list of all the ones that
were checked all 10 would be reset and unchecked to start over.

You can still put the Checkboxes into an array:

Once (in constructor or in OnLoad):
Dim AllCheckboxes As CheckBox() = {chk1, chk2, chk3, ... chkbox10}

Later:
dim chk as checkbox
dim sb as new system.text.stringbuilder
dim names as string

for each chk in allcheckboxes
if chk.checked Then
sb.append(chk.text)
sb.append(" "c)
chk.check = false
end if
next chk

names = sb.tostring(0, sb.length - 1)


_Instead_ of the array (AllCheckboxes), you can loop through the controls in
the GroupBox:

dim chk as checkbox
for each chk in mygroupbox.controls
'same as above
next chk

If there are also other types of controls in the same groupbox:
dim o as object
for each o in mygroupbox.controls
if typeof o is checkbox then
dim chk as checkbox
chk = directcast(o, checkbox)
'same as above
end if
next chk
 
H

Herfried K. Wagner [MVP]

Tim said:
On my form I have 10 checkboxes named chkbox1,chkbox2,....chkbox10. I would
have like to set it up as an array control like in VB6 where I could have
chkbox(1),chkbox(2) but I think .net you have to have specific names for
each control. All 10 of the boxes are in a groupbox. What I'm trying to do
is when I hit a button it will loop through all the checkboxes to see which
ones are checked and add that checkbox name to a string. Something like
Names = chkbox1.text & " " chkbox2.text & " " ect... Once I got a list of
all the ones that were checked all 10 would be reset and unchecked to start
over.

Creating Control Arrays in Visual Basic .NET and Visual C# .NET
<http://msdn.microsoft.com/library/?...ngControlArraysInVisualBasicNETVisualCNET.asp>
 
J

Justin Weinberg

1) You could create an array that contains a reference to each of the
checkboxes. Just add them manually. Just because you can't have control
arrays alla VB6 doesn't mean you can't use arrays.

2) If the form has all of the checkboxes you are interested in, and you
don't expect to have more, you can loop through the controls collection of
the form (For each chk as Checkbox in me.controls)

3) You can add all the checkboxes to a panel and do the same as #2 but the
panel's controls. This gives you a little more extensibility.

I'm sure there are more ways.
 
C

Cor

Hi Tim,
On my form I have 10 checkboxes named chkbox1,chkbox2,....chkbox10. I would
have like to set it up as an array control like in VB6 where I could have
chkbox(1),chkbox(2) but I think .net you have to have specific names for
each control. All 10 of the boxes are in a groupbox. What I'm trying to do
is when I hit a button it will loop through all the checkboxes to see which
ones are checked and add that checkbox name to a string. Something like
Names = chkbox1.text & " " chkbox2.text & " " ect... Once I got a list of
all the ones that were checked all 10 would be reset and unchecked to start
over.
Something like this I think.
\\\\
dim strb as system.text.stringbuilder
Dim ctr As Control
Dim a As New System.Text.StringBuilder
For Each ctr In Me.GroupBox1.Controls
Dim ctrchk As CheckBox = DirectCast(ctr, CheckBox)
If ctrchk.Checked Then
strb.Append(ctrchk.Name.ToString)
End If
Next
dim strEnd as string = strb.tostring
////
I hope this helps a little bit
Cor
 
D

Don B

Tim said:
On my form I have 10 checkboxes named chkbox1,chkbox2,....chkbox10. I would
have like to set it up as an array control like in VB6 where I could have
chkbox(1),chkbox(2) but I think .net you have to have specific names for
each control. All 10 of the boxes are in a groupbox. What I'm trying to do
is when I hit a button it will loop through all the checkboxes to see which
ones are checked and add that checkbox name to a string. Something like
Names = chkbox1.text & " " chkbox2.text & " " ect... Once I got a list of
all the ones that were checked all 10 would be reset and unchecked to start
over.

Thanks,

Tim

Dim ctrl as Control
Dim ChkBx as CheckBox

For each ctrl in GroupBox.Controls
If ctrl.GetType is ChkBx.GetType Then
ctrl = DirectCast(ctl, CheckBox)
If ctrl.Checked Then
'do stuff
End If
End If
Next
 
O

One Handed Man

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim c As System.Windows.Forms.CheckBox

For Each c In GroupBox1.Controls()

c.Checked = True

Next

end Sub
 
A

Armin Zingler

One Handed Man said:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim c As System.Windows.Forms.CheckBox

For Each c In GroupBox1.Controls()

c.Checked = True

Next

end Sub

I'm trying to figure out the difference. Ok, I accidently wrote "chk.check =
False" (not checkED) instead, but that's all I can see.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top