473,503 Members | 1,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check all yellow checkboxes

1 New Member
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
Thanks
Dave

Expand|Select|Wrap|Line Numbers
  1. Private Sub Allyellow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Allyellow.CheckedChanged
  2.  
  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
2 2088
rythmic
29 New Member
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. }
  8.  
Something like that seems a bit easier to grasp.
Dec 11 '10 #2
Joseph Martell
198 Recognized Expert New Member
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
  2.  
  3. lightYellowCheckBoxes = Me.Controls.OfType(Of CheckBox).Where(Function(chkBox As CheckBox) chkBox.BackColor = Color.LightYellow)
  4.  
  5. If (AllYellow.Checked) Then
  6.  
  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
  15.  
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)
  2.  
the predicate is provided by this part of the code:

Expand|Select|Wrap|Line Numbers
  1. Function(chkBox As CheckBox) chkBox.BackColor = Color.LightYellow
  2.  
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)
  2.  
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

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

Similar topics

6
2836
by: terence.parker | last post by:
I currently have the following JS in my header: function checkall(thestate) { var checkboxes=eval("document.forms.EssayList.file_id") for (i=0;i<checkboxes.length;i++)...
7
2888
by: Jaime Stuardo | last post by:
Hi all.. I have a DataGrid with checkboxes. In the header I have a "check all" checkbox. I'm wondering if there is an easy way to check all checkboxes using that checkbox. I could do it using...
1
1740
by: shankwheat | last post by:
I need to create to create a function that checks all checkboxes within pnlSearchValues. This isn't working - no errors. Can someone help me out? Thanks. <script type="text/javascript">...
1
5853
by: shanthsp2002 | last post by:
i have an xml file, i have to enable or disable checkboxes based on these valuse <?xml version="1.0" encoding="utf-8" ?> <ModulesAvalable> <Module name="calculator" hasdatabase="False"...
6
14747
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'...
10
5180
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
2
2241
by: birwin | last post by:
I found a Javascript snippet that very effectively checks all boxes on a page, even on a page on which the input tags are in tables and on which I use a lot of other javascript. Unfortunately it is...
3
1524
by: hpjack433 | last post by:
I need a requirement here, i want to use JavaScript so that i don't want users to check both the check boxes. They should check only one and in case if they check both, an alert message should pop up...
5
2376
by: sasimca007 | last post by:
Hi friends, I have a doubt about checkboxes, i.e if i am creating checkboxes with the same name, in a loop and i have another checkbox down to the above checkboxes, My problem is...
1
4501
Frinavale
by: Frinavale | last post by:
I'm working on an ASP.NET application using VB.NET for server side code. I have a GridView listing a bunch of stuff. Above the GridView I have a checkbox named "ChkBx_SelectAll". If this...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.