-
Jan 10th, 2022, 03:13 AM
#1
Thread Starter
New Member
[RESOLVED] Check checkbox in a loop
Good Morning everyone
i have a problem with my program, i have to check all my checkbox(200) to know how many of them are checked, but i can't find the solution...
I tried to index the checkboxes but it still doesn't working.
can you help me?
thaks!
-
Jan 10th, 2022, 04:34 AM
#2
Re: Check checkbox in a loop
For future reference, expressions like this are likely to cause confusion:
i have to check all my checkbox(200) to know how many of them are checked
You should say "test" or the like rather than "check", given that "check" has another obvious meaning in that context that you even use in the same sentence.
As for the issue, it's actually quite simple. Just make sure that all those CheckBoxes are in the same container and no other CheckBoxes are in that same container. That's probably already the case but, if it's not, just add a Panel and then move those CheckBoxes and no others into it. You can then do this:
vb.net Code:
For Each cb In myContainer.Controls.OfType(Of CheckBox)()
'Use cb here.
Next
You can take that a step further:
vb.net Code:
For Each cb In myContainer.Controls.OfType(Of CheckBox)().Where(Function(cbx) cbx.Checked)
'Use cb here.
Next
Of course, if you take it that far, you may as well take it all the way:
vb.net Code:
Dim checkedCheckBoxCount = myContainer.Controls.OfType(Of CheckBox)().Count(Function(cbx) cbx.Checked)
Note that, in this context, myContainer might be the form itself or a Panel or whatever container control the CheckBoxes are in.
Last edited by jmcilhinney; Jan 10th, 2022 at 04:41 AM.
-
Jan 10th, 2022, 04:46 AM
#3
Thread Starter
New Member
Re: Check checkbox in a loop
Thanks jmcilhinney!
you have solved my problems, i used For each / next with groupbox and now its perfect
-
Jan 10th, 2022, 04:50 AM
#4
Re: [RESOLVED] Check checkbox in a loop
If you wanted to do it without any LINQ at all (OfType, Where and Count are all LINQ extension methods) then you could do it like this:
vb.net Code:
For Each ctl In myContainer.Controls
Dim cb = TryCast(ctl, CheckBox)
If cb IsNot Nothing Then
'Use cb here.
End If
Next
This allows for controls of other types in the same container. If there aren't any other controls then you can use this:
vb.net Code:
For Each ctl In myContainer.Controls
Dim cb = DirectCast(ctl, CheckBox)
'Use cb here.
Next
Last edited by jmcilhinney; Jan 10th, 2022 at 05:20 AM.
-
Jan 10th, 2022, 01:36 PM
#5
Re: [RESOLVED] Check checkbox in a loop
jmc,
Code:
For Each cntrl In Panel2.Controls.OfType(Of CheckBox)()
Dim ctl = DirectCast(cntrl, CheckBox)
If ctl.checked Then
End If
Next
I was playing with this and I keep getting the Option Strict doesn't allow late binding error on this ctl.Checked.
-
Jan 10th, 2022, 02:35 PM
#6
Re: [RESOLVED] Check checkbox in a loop
Originally Posted by wes4dbt
jmc,
Code:
For Each cntrl In Panel2.Controls.OfType(Of CheckBox)()
Dim ctl = DirectCast(cntrl, CheckBox)
If ctl.checked Then
End If
Next
I was playing with this and I keep getting the Option Strict doesn't allow late binding error on this ctl.Checked.
Code:
For Each cntrl as CheckBox In Panel2.Controls.OfType(Of CheckBox)
If cntrl.checked Then
End If
Next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 10th, 2022, 02:35 PM
#7
Re: [RESOLVED] Check checkbox in a loop
Originally Posted by wes4dbt
jmc,
Code:
For Each cntrl In Panel2.Controls.OfType(Of CheckBox)()
Dim ctl = DirectCast(cntrl, CheckBox)
If ctl.checked Then
End If
Next
I was playing with this and I keep getting the Option Strict doesn't allow late binding error on this ctl.Checked.
Try this
Code:
For Each cntrl As CheckBox In Panel2.Controls.OfType(Of CheckBox)()
If cntrl.Checked Then
End If
Next
-
Jan 10th, 2022, 02:36 PM
#8
Re: [RESOLVED] Check checkbox in a loop
Originally Posted by dbasnett
Try this
Code:
For Each cntrl As CheckBox In Panel2.Controls.OfType(Of CheckBox)()
Dim ctl As CheckBox = DirectCast(cntrl, CheckBox)
If ctl.checked Then
End If
Next
Why cast a CheckBox to a CheckBox???
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 10th, 2022, 02:37 PM
#9
Re: [RESOLVED] Check checkbox in a loop
I know. See edit.
-
Jan 10th, 2022, 02:38 PM
#10
Re: [RESOLVED] Check checkbox in a loop
Originally Posted by dbasnett
I know. See edit.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 10th, 2022, 02:49 PM
#11
Re: [RESOLVED] Check checkbox in a loop
Originally Posted by dbasnett
Try this
Code:
For Each cntrl As CheckBox In Panel2.Controls.OfType(Of CheckBox)()
If cntrl.Checked Then
End If
Next
tried that, tried several ways
-
Jan 10th, 2022, 02:52 PM
#12
Re: [RESOLVED] Check checkbox in a loop
Originally Posted by wes4dbt
tried that, tried several ways
And... what happened?
-
Jan 10th, 2022, 07:52 PM
#13
Re: [RESOLVED] Check checkbox in a loop
Originally Posted by wes4dbt
jmc,
Code:
For Each cntrl In Panel2.Controls.OfType(Of CheckBox)()
Dim ctl = DirectCast(cntrl, CheckBox)
If ctl.checked Then
End If
Next
I was playing with this and I keep getting the Option Strict doesn't allow late binding error on this ctl.Checked.
I'm not really sure why you would play with that in the first place. The OfType method already returns items of the specified type as that type, so your cntrl variable is already type CheckBox. That means that your cast is pointless. It still won't hurt though, as DirectCast will return a reference of type CheckBox. That means that the ctl variable should be inferred as type CheckBox, unless you have Option Infer Off. If that was the case though, and you had Option Strict On, I wouldn't think that you should be able to declare that variable without specifying a type. When I pasted your code into a project with both options On it was fine. With Option Infer Off, I got the late-binding error but also errors on the declarations of both variables. If you're not seeing that, I don't know what's going on.
Regardless, go back and look at the code combinations I posted. If you use OfType then an explicit cast is pointless. Use one or the other.
-
Jan 10th, 2022, 08:54 PM
#14
Re: [RESOLVED] Check checkbox in a loop
Yeah, I didn't have the directcast originally. I was just trying different things to solve the late binding issue.
I checked and Option Infer is On.
Edit:
Well I solved the problem. I turned Option Strict Off and I was getting an error but it was a different error. It mentioned Office.
Reference required to assembly 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' containing the type 'IAccessible'. Add one to your project.
so I commented out this import
Code:
Imports Microsoft.Office.Interop.Access
And the error went away. This isn't a probablem because it's just a junk form I use for playing with code.
Last edited by wes4dbt; Jan 10th, 2022 at 09:08 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|