By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

How to loop through multiple checkboxes

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Tim" <dg******@maine.rrdotcom> scripsit:
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/?url=/library/en-us/dv_vstechart/html/vbtchCreatingControlArraysInVisualBasicNETVisualCN ET.asp>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
"Tim" <dg******@maine.rrdotcom> schrieb
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
--
Armin

Nov 20 '05 #3

P: n/a
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.

"Tim" <dg******@maine.rrdotcom> wrote in message
news:T0*****************@twister.nyroc.rr.com...
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

Nov 20 '05 #4

P: n/a
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
Nov 20 '05 #5

P: n/a
Here is an article I submitted to PSC which may help you.

Matt

http://www.planetsourcecode.com/vb/s...1376&lngWId=10

"Tim" <dg******@maine.rrdotcom> wrote in message
news:T0*****************@twister.nyroc.rr.com...
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

Nov 20 '05 #6

P: n/a
"Tim" <dg******@maine.rrdotcom> wrote in message news:<T0*****************@twister.nyroc.rr.com>...
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
Nov 20 '05 #7

P: n/a
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
Armin Zingler wrote:
"Tim" <dg******@maine.rrdotcom> schrieb
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

Nov 20 '05 #8

P: n/a
"One Handed Man" <Bo****@Duck.net> schrieb
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.
--
Armin

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.