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

Check all yellow checkboxes

P: 1
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\unchecked (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

Expand|Select|Wrap|Line Numbers
  1. Private Sub Allyellow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Allyellow.CheckedChanged
  3.         If Allyellow.Checked = True Then
  4.             If CheckBox1.BackColor = Color.LightYellow Then
  5.                 CheckBox1.Checked = True
  6.             End If
  7.             If CheckBox2.BackColor = Color.LightYellow Then
  8.                 CheckBox2.Checked = True
  9.             End If
  10.             If CheckBox3.BackColor = Color.LightYellow Then
  11.                 CheckBox3.Checked = True
  12.             End If
  13.             If CheckBox4.BackColor = Color.LightYellow Then
  14.                 CheckBox4.Checked = True
  15.             End If
  16.             If CheckBox5.BackColor = Color.LightYellow Then
  17.                 CheckBox5.Checked = True
  18.             End If
  19.             If CheckBox6.BackColor = Color.LightYellow Then
  20.                 CheckBox6.Checked = True
  21.             End If
  22.             If CheckBox7.BackColor = Color.LightYellow Then
  23.                 CheckBox7.Checked = True
  24.             End If
  25.             If CheckBox8.BackColor = Color.LightYellow Then
  26.                 CheckBox8.Checked = True
  27.             End If
  28.             If CheckBox9.BackColor = Color.LightYellow Then
  29.                 CheckBox9.Checked = True
  30.             End If
  31.             If CheckBox10.BackColor = Color.LightYellow Then
  32.                 CheckBox10.Checked = True
  33.             End If
  34.             If CheckBox11.BackColor = Color.LightYellow Then
  35.                 CheckBox11.Checked = True
  36.             End If
  37.             If CheckBox12.BackColor = Color.LightYellow Then
  38.                 CheckBox12.Checked = True
  39.             End If
  40.             If CheckBox13.BackColor = Color.LightYellow Then
  41.                 CheckBox13.Checked = True
  42.             End If
  43.             If CheckBox14.BackColor = Color.LightYellow Then
  44.                 CheckBox14.Checked = True
  45.             End If
  46.         ElseIf Allyellow.Checked = False Then
  47.             If CheckBox1.BackColor = Color.LightYellow Then
  48.                 CheckBox1.Checked = False
  49.             End If
  50.             If CheckBox2.BackColor = Color.LightYellow Then
  51.                 CheckBox2.Checked = False
  52.             End If
  53.             If CheckBox3.BackColor = Color.LightYellow Then
  54.                 CheckBox3.Checked = False
  55.             End If
  56.             If CheckBox4.BackColor = Color.LightYellow Then
  57.                 CheckBox4.Checked = False
  58.             End If
  59.             If CheckBox5.BackColor = Color.LightYellow Then
  60.                 CheckBox5.Checked = False
  61.             End If
  62.             If CheckBox6.BackColor = Color.LightYellow Then
  63.                 CheckBox6.Checked = False
  64.             End If
  65.             If CheckBox7.BackColor = Color.LightYellow Then
  66.                 CheckBox7.Checked = False
  67.             End If
  68.             If CheckBox8.BackColor = Color.LightYellow Then
  69.                 CheckBox8.Checked = False
  70.             End If
  71.             If CheckBox9.BackColor = Color.LightYellow Then
  72.                 CheckBox9.Checked = False
  73.             End If
  74.             If CheckBox10.BackColor = Color.LightYellow Then
  75.                 CheckBox10.Checked = False
  76.             End If
  77.             If CheckBox11.BackColor = Color.LightYellow Then
  78.                 CheckBox11.Checked = False
  79.             End If
  80.             If CheckBox12.BackColor = Color.LightYellow Then
  81.                 CheckBox12.Checked = False
  82.             End If
  83.             If CheckBox13.BackColor = Color.LightYellow Then
  84.                 CheckBox13.Checked = False
  85.             End If
  86.             If CheckBox14.BackColor = Color.LightYellow Then
  87.                 CheckBox14.Checked = False
  88.             End If
  89.         End If
  90.     End Sub
Dec 10 '10 #1
Share this Question
Share on Google+
2 Replies

P: 29
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:

Expand|Select|Wrap|Line Numbers
  1. Dim cbs As CheckBox[] = {All checkboxes here}
  2. Dim allYellowStatus As bool = Allyellow.Checked; 
  3. For Each checkBox As CheckBox In cbs {
  4.     If checkBox.BackColor = Color.LightYellow Then
  5.         checkBox.Checked = allYellowStatus
  6.     End If
  7. }
Something like that seems a bit easier to grasp.
Dec 11 '10 #2

Joseph Martell
Expert 100+
P: 196
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:
Expand|Select|Wrap|Line Numbers
  1. Dim lightYellowCheckBoxes As List(Of CheckBox) = Nothing
  3. lightYellowCheckBoxes = Me.Controls.OfType(Of CheckBox).Where(Function(chkBox As CheckBox) chkBox.BackColor = Color.LightYellow)
  5. If (AllYellow.Checked) Then
  7.     For Each chkBox In lightYellowCheckBoxes
  8.         chkBox.Checked = True
  9.     Next
  10. Else
  11.     For Each chkBox In lightYellowCheckBoxes
  12.         chkBox.Checked = False
  13.     Next
  14. End If
The key to this code is the following line:
Expand|Select|Wrap|Line Numbers
  1. 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:

lightYellowCheckBoxes 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:

Expand|Select|Wrap|Line Numbers
  1. 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.OfType(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:
Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. 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.LightYellow. The return type is implied.

Now lightYellowCheckBoxes 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 lightYellowCheckBoxes a private class member (instead of a local collection in the function) and put:

Expand|Select|Wrap|Line Numbers
  1. 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.
Dec 13 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.