Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple checkBox selection 2

Status
Not open for further replies.

rstum2005

Programmer
Jun 9, 2005
117
US
I would like to Check multiple checkBoxs and do functions based on what series of checkBoxes were Checked. What would be the best way to do this insted of a bunch of if/else statements?

Thanks

if/else Example:

if (checkBox1.Checked==true&&checkBox2.Checked==true)
{

}

 
put all the checkboxes into a panel. In this case, my panel is called pnlCheckBoxes. Then loop through and get the check state.

for (int i = 0; i < pnlCheckBoxes.Controls.Count; i++)
{
if (pnlCheckBoxes.Controls.GetType() == checkBox1.GetType())
{
CheckBox box = (CheckBox)pnlCheckBoxes.Controls;
if (box.Checked)
{
//Do your stuff here.
}
}
}


This code was written from my head so it may have syntax errors.
 
Dear JurkMonkey,
Your code reflects your high programming experience :) . I guess rstum wishes to check more than one checkBox to get a certain condition. like
[ ] Married
[ ] Have Children
[ ] widdowed
[ ] Single

So he may need to know who's married and have children, who is widdowed and have children, how many are single...

Please correct me rstum if i'm wrong.
If i'm right, I guess u'll need an array of boolean where each item represents a checkbox, on check_event u can set each boolean item to true/false then elsewhere in code u can check the array of bools to get the required values. According to my small knowledge i guess no other way than if/else bunches. the array will just save effort of calling the controls(checkboxes) all the time.
 
Yes thats exactly what im talking about. I have about eight checkboxes and lets say I want a condition done for each.

[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]

I need to do this sort of thing.

if 1 and 2 are checked
if 1 and 3 are checked
if 1 is checked and 3 is checked and 4 is checked
if 2 is checked and 4 is checked and 6 is checked and 3 is checked.


this means id be writing alot of if/else statements if i did it this way.
 
Just a thought ....

create a variable (Integer should be OK)

set it to 0
using a series of If statements OR a bit value into it (a separate bit value for each checkbox)
ie checkbox1 could be 1
checkbox2 could be 2
checkbox3 could be 4
.
.


you can then work out the integer values for each condition you need to test and then using the C# equivalent to the VB Select Case statement

Select Case IntVar
Case x

Case y

etc.


Hope this helps.

 
Since the Checked property of a Checkbox is already a boolean, there's no need to do a comparison. This should simplify your code quite a bit. From this:
Code:
if (checkBox1.Checked==true&&checkBox2.Checked==true)
to this:
Code:
if (checkBox1.Checked && checkBox2.Checked)
If you need to test for a checkbox not being checked, use the negation operator (!):
Code:
if (checkBox1.Checked && !checkBox2.Checked)
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chip with 8 or more CheckBoxes that could entail a lot of nested Ifs or have every required combination checked my suggestion was designed to be easy to manage (more or less conditions required in future) and hopefully easily readable. Its something I've used many times in Delphi and VB (.Net, 6 and VBA)

I wouldn't attempt to write this in C# but this is what I meant:

Code:
This assumes 4 checkboxes


'Obviously only declare the constants that will need to be checked and give them descriptive names
Const condition1 As Integer = 1
Const condition2 As Integer  = 2
Const condition3 As Integer  = 3
Const condition4 As Integer  = 4
Const condition5 As Integer  = 5
Const condition6 As Integer  = 6
Const condition7 As Integer  = 7
Const condition8 As Integer = 8
Const condition9 As Integer = 9
Const condition10 As Integer  = 10
Const condition11 As Integer  = 11
Const condition12 As Integer  = 12
Const condition13 As Integer  = 13
Const condition14 As Integer  = 14
Const condition15 As Integer  = 15


Dim IntVar As Integer = 0

If cb1.Checked Then IntVar = 1
If cb2.Checked Then IntVar += 2  'equivalent in thiscase to ORing
If cb3.Checked Then IntVar += 4  'equivalent in thiscase to ORing
If cb4.Checked Then IntVar += 8  'equivalent in thiscase to ORing


'Obviously only check the conditions that are of interest
Select Case IntVar
  Case 1

  Case 2

  .
  
  .

  Case 14

  Case 15

  Case Else  'If all possible conditions are not being individually checked

End Select

Hope this helps.
 
8 CheckBoxes?

You're going to think this is crazy but:

BINARY is your answer.

--------------------------------------------------------
int finalValue =0;

for (int i = 0; i < pnlCheckBoxes.Controls.Count; i++)
{
if (pnlCheckBoxes.Controls.GetType() == checkBox1.GetType())
{
CheckBox box = (CheckBox)pnlCheckBoxes.Controls;
if (box.Checked)
{
int temp = 1;
for (int j = 0; j < i; i++)
{
temp = temp * 2;
}

finalValue += temp;
}
}
}
----------------------------------------------------------

[0][0][0][0] [0 ][0 ][0 ][0]
[1][2][4][8] [16][32][64][128]

so if only checkbox1 is checked then finalvalue = 1
if checkbox 2 and 5 are checked then finalvalue = 18

now you can just use a switch.

switch(finalvalue)
{
case 1:
case 2:
case 4:
case 8:
...
}

if you want to take it a step further, create an enumeration:

public enum PersonStatus {Single=1, Married=2, Children=4, MarriedWithChildren = 8, ... }

then use

switch(finalvalue)
{
case (int)PersonStatus.Single:
case (int)PersonStatus.Married:
...
}

Hope that helps you out.


 
Thanks JurkMonkey, I new converting my VB suggestion into C# would be easy, but I played with it for a bit and gave up - and decided to post in VB instead.
 
Thanks for all of your responses. I have an idea of the direction I will take but I wont be working on this project untill later this weekend. Thanks again.

Ron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top