473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to loop through multiple checkboxes

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
8 42001
"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
"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
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
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
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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jeffsnox | last post by:
Hi, I have multiple checkboxes on the same form as follows: <input type='checkbox' name='cbtype' value='1'> <input type='checkbox' name='cbtype' value='2'> <input type='checkbox'...
4
by: ramapv | last post by:
can i highlight a checkbox from a group of checkbox with particular name which is given as a search key. I am having a list of checkboxes and i have to select some of them and form a group.but i'm...
4
by: favor08 | last post by:
I have a continious subform that 9000 + records and I have serveral check marks on the subform. so it ties to that record. Does anyone have any examples were they use multiple checkboxes in a ...
6
by: dream2rule | last post by:
Hello All, I am trying to validate multiple checkboxes whose values are stored in an array using php. I have been trying from a really long time but nothing's working out. Can anyone help? ...
0
by: serghei | last post by:
I have already the insert form and the database. It's working to insert multiple data with multiple checkboxes. But I can't retrieve multiple data.
0
by: Ned Balzer | last post by:
Hi, Can anyone point me in the direction of a solution for validating multiple checkboxes in an asp.net 2.0 page? 1) This is not a checkboxlist, it is a number of separate checkboxes 2) I do...
3
by: santoshjsh | last post by:
hello everyone, i have a gridview in which i have multiple checkboxes in a single column. means for every row in the gridview i have four checkboxes in one column e.g Add, Delete, Print,...
3
by: raamay | last post by:
hey experts, please advise me what is the best way to save multiple checkboxes value in a database. I have 6 checkboxes and i came across storing the values in a single column of a table which i dont...
0
by: kimmelsd33 | last post by:
I am adding to my software. I have placed about 30 checkboxes on a form. Based on which checkboxes are selected, I want to print the values to a tab delimited text file. For instance, the first...
12
by: BabyLucifer666 | last post by:
Hello All, (using Access2000) I have a form with multiple unbound checkboxes. What I would like to do is have the user check whoever needs to take a specific training course. My database is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.