Results 1 to 14 of 14

Thread: [RESOLVED] Check checkbox in a loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2022
    Posts
    2

    Resolved [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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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:
    1. For Each cb In myContainer.Controls.OfType(Of CheckBox)()
    2.     'Use cb here.
    3. Next
    You can take that a step further:
    vb.net Code:
    1. For Each cb In myContainer.Controls.OfType(Of CheckBox)().Where(Function(cbx) cbx.Checked)
    2.     'Use cb here.
    3. Next
    Of course, if you take it that far, you may as well take it all the way:
    vb.net Code:
    1. 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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2022
    Posts
    2

    Re: Check checkbox in a loop

    Thanks jmcilhinney!
    you have solved my problems, i used For each / next with groupbox and now its perfect

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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:
    1. For Each ctl In myContainer.Controls
    2.     Dim cb = TryCast(ctl, CheckBox)
    3.  
    4.     If cb IsNot Nothing Then
    5.         'Use cb here.
    6.     End If
    7. 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:
    1. For Each ctl In myContainer.Controls
    2.     Dim cb = DirectCast(ctl, CheckBox)
    3.  
    4.     'Use cb here.
    5. Next

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    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.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: [RESOLVED] Check checkbox in a loop

    Quote Originally Posted by wes4dbt View Post
    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

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: [RESOLVED] Check checkbox in a loop

    Quote Originally Posted by wes4dbt View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: [RESOLVED] Check checkbox in a loop

    Quote Originally Posted by dbasnett View Post
    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???

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: [RESOLVED] Check checkbox in a loop

    I know. See edit.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: [RESOLVED] Check checkbox in a loop

    Quote Originally Posted by dbasnett View Post
    I know. See edit.

  11. #11
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: [RESOLVED] Check checkbox in a loop

    Quote Originally Posted by dbasnett View Post
    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

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: [RESOLVED] Check checkbox in a loop

    Quote Originally Posted by wes4dbt View Post
    tried that, tried several ways
    And... what happened?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: [RESOLVED] Check checkbox in a loop

    Quote Originally Posted by wes4dbt View Post
    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.

  14. #14
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    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
  •  



Click Here to Expand Forum to Full Width