Check all yellow checkboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David Callen
    New Member
    • Dec 2010
    • 1

    Check all yellow checkboxes

    Hey Guys

    Quick question which I'm sure is actually pretty simple but I'm pretty new to this.

    I have a piece of code for a program where when a specific checkbox is checked\uncheck ed (In this case "Allyellow" ) it checks\unchecks any checkboxes within a group of 14 with a lightyellow backcolor.

    While the piece of code below does this fine it seems to me to be very clunky and it must be possible to streamline it.

    Any help would be much appriciated
    Thanks
    Dave

    Code:
    Private Sub Allyellow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Allyellow.CheckedChanged
    
            If Allyellow.Checked = True Then
                If CheckBox1.BackColor = Color.LightYellow Then
                    CheckBox1.Checked = True
                End If
                If CheckBox2.BackColor = Color.LightYellow Then
                    CheckBox2.Checked = True
                End If
                If CheckBox3.BackColor = Color.LightYellow Then
                    CheckBox3.Checked = True
                End If
                If CheckBox4.BackColor = Color.LightYellow Then
                    CheckBox4.Checked = True
                End If
                If CheckBox5.BackColor = Color.LightYellow Then
                    CheckBox5.Checked = True
                End If
                If CheckBox6.BackColor = Color.LightYellow Then
                    CheckBox6.Checked = True
                End If
                If CheckBox7.BackColor = Color.LightYellow Then
                    CheckBox7.Checked = True
                End If
                If CheckBox8.BackColor = Color.LightYellow Then
                    CheckBox8.Checked = True
                End If
                If CheckBox9.BackColor = Color.LightYellow Then
                    CheckBox9.Checked = True
                End If
                If CheckBox10.BackColor = Color.LightYellow Then
                    CheckBox10.Checked = True
                End If
                If CheckBox11.BackColor = Color.LightYellow Then
                    CheckBox11.Checked = True
                End If
                If CheckBox12.BackColor = Color.LightYellow Then
                    CheckBox12.Checked = True
                End If
                If CheckBox13.BackColor = Color.LightYellow Then
                    CheckBox13.Checked = True
                End If
                If CheckBox14.BackColor = Color.LightYellow Then
                    CheckBox14.Checked = True
                End If
            ElseIf Allyellow.Checked = False Then
                If CheckBox1.BackColor = Color.LightYellow Then
                    CheckBox1.Checked = False
                End If
                If CheckBox2.BackColor = Color.LightYellow Then
                    CheckBox2.Checked = False
                End If
                If CheckBox3.BackColor = Color.LightYellow Then
                    CheckBox3.Checked = False
                End If
                If CheckBox4.BackColor = Color.LightYellow Then
                    CheckBox4.Checked = False
                End If
                If CheckBox5.BackColor = Color.LightYellow Then
                    CheckBox5.Checked = False
                End If
                If CheckBox6.BackColor = Color.LightYellow Then
                    CheckBox6.Checked = False
                End If
                If CheckBox7.BackColor = Color.LightYellow Then
                    CheckBox7.Checked = False
                End If
                If CheckBox8.BackColor = Color.LightYellow Then
                    CheckBox8.Checked = False
                End If
                If CheckBox9.BackColor = Color.LightYellow Then
                    CheckBox9.Checked = False
                End If
                If CheckBox10.BackColor = Color.LightYellow Then
                    CheckBox10.Checked = False
                End If
                If CheckBox11.BackColor = Color.LightYellow Then
                    CheckBox11.Checked = False
                End If
                If CheckBox12.BackColor = Color.LightYellow Then
                    CheckBox12.Checked = False
                End If
                If CheckBox13.BackColor = Color.LightYellow Then
                    CheckBox13.Checked = False
                End If
                If CheckBox14.BackColor = Color.LightYellow Then
                    CheckBox14.Checked = False
                End If
            End If
        End Sub
  • rythmic
    New Member
    • Feb 2010
    • 29

    #2
    How about putting all checkboxes in an array and then iterate through that array.

    I actually don't know VB syntax at all but i do know C# and that approach would work fine.

    Since I don't know VB syntax I can't help you there but a conceptual solution would be this:

    Code:
    Dim cbs As CheckBox[] = {All checkboxes here}
    Dim allYellowStatus As bool = Allyellow.Checked; 
    For Each checkBox As CheckBox In cbs {
        If checkBox.BackColor = Color.LightYellow Then
            checkBox.Checked = allYellowStatus
        End If
    }
    Something like that seems a bit easier to grasp.

    Comment

    • Joseph Martell
      Recognized Expert New Member
      • Jan 2010
      • 198

      #3
      There is actually a better way to do this. Your controls on your form are already in a collection. What you need is an efficient way to get all check boxes that have the light yellow background.

      IF you are using .Net framework 3.5 or greater then you have access to a couple of extension methods that can make life a little easier for you.

      Here is a complete code sample that should work:
      Code:
      Dim lightYellowCheckBoxes As List(Of CheckBox) = Nothing
      
      lightYellowCheckBoxes = Me.Controls.OfType(Of CheckBox).Where(Function(chkBox As CheckBox) chkBox.BackColor = Color.LightYellow)
      
      If (AllYellow.Checked) Then
      
          For Each chkBox In lightYellowCheckBoxes
              chkBox.Checked = True
          Next
      Else
          For Each chkBox In lightYellowCheckBoxes
              chkBox.Checked = False
          Next
      End If
      The key to this code is the following line:
      Code:
      lightYellowCheckBoxes = Me.Controls.OfType(Of CheckBox).Where(Function(chkBox As CheckBox) chkBox.BackColor = Color.LightYellow)
      This is kind of complicated, especially if you've never seen it before (and the VB syntax makes this a little bit less clear, in my opinion). Lets break it down:

      lightYellowChec kBoxes is a list(of CheckBox) so it holds a collection of checkboxes. Because of the way that this is implemented it dynamically sizes itself to hold as many check boxes as you need. This means you don't have to worry about sizing your array correctly, or manually resizing your array during processing.

      Me.Controls references the collection of controls that are currently on the window. The .Net framework 3.5 introduces the extension method OfType:

      Code:
      Me.Controls.OfType(Of CheckBox)
      This statement returns a collection of all the check box controls in the Me.Controls collection.

      Finally, the .Where extension method is also introduced in .Net framework 3.5. So, with Me.Controls.OfT ype(Of CheckBox) we accessed a collection of check boxes on the form. Using the .Where extension allows to return another collection of controls. Unlike .OfType, which just filters on the type of the control, we get to supply a predicate of the .Where extension ourselves.

      From the statement:
      Code:
      lightYellowCheckBoxes = Me.Controls.OfType(Of CheckBox).Where(Function(chkBox As CheckBox) chkBox.BackColor = Color.LightYellow)
      the predicate is provided by this part of the code:

      Code:
      Function(chkBox As CheckBox) chkBox.BackColor = Color.LightYellow
      This statement creates a Lambda function. Think of a Lambda function as a single-purpose, unnamed function that we create on-the-fly and use for a very specific reason. In this case, I am creating a function that takes a check box and returns a boolean value based on whether the background of that check box was Color.LightYell ow. The return type is implied.


      Now lightYellowChec kBoxes references a list of checkboxes on the current form with a background of light yellow.

      This code can be made more efficient if the check boxes with light yellow backgrounds do not change. You could make lightYellowChec kBoxes a private class member (instead of a local collection in the function) and put:

      Code:
      lightYellowCheckBoxes = Me.Controls.OfType(Of CheckBox).Where(Function(chkBox As CheckBox) chkBox.BackColor = Color.LightYellow)
      in your form load event. That way you are only creating your list of check boxes once.

      The if statements and loops are pretty self explanatory. I chose to nest the for loops inside the if statements because that way your code would perform one conditional test and then fall into a loop. Nesting the if statement inside the loop would mean that the conditional test occurs in each iteration of the loop. Just a side note.

      You should also know that everything that was achieved with the .OfType and .Where extension methods can be achieved with other code as well. .OfType and .Where are just meant to be syntactic sugar to make life easier for you.

      Comment

      Working...