Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to get selected Check boxes count in windows Can u guide or send any snippets
Posted
Comments
Abhinav S 2-May-12 4:46am    
Which countrol are you using? ListView?
tanweer 2-May-12 4:47am    
it depend on how you added CheckBoxes to your Form, did you add CheckBoxList or all these are checkbox control?
Ravinder_Verma 2-May-12 5:28am    
You need to let us know, what are the control hierarchy in your window form? The solution depends upon the control hierarchy.

if the checkboxes are added directly to the form then you can use this.
C#
int counter = 0;
           foreach (Control c in this.Controls)
               if (c is CheckBox)
                   if (((CheckBox)c).Checked)
                       counter++;

           MessageBox.Show(counter.ToString());
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 2-May-12 7:56am    
+5 because It works
hitech_s 2-May-12 23:57pm    
Thank you 'shahin khorshidnia'
If the requirement is to count the CheckBoxes which are Checked, then the following LINQ query can be used

C#
for(int i=0; i< 10; i++){
    CheckBox chkBox = new CheckBox();
    if (i % 2==0)
        chkBox.Checked = true;
    Controls.Add(chkBox);
}
Controls.Add(new TextBox());
Controls.Add(new Button());

int selectedCheckBoxesCount = Controls.OfType<CheckBox>()
                            .Count (cb => cb.Checked);

//selectedCheckBoxesCount
//5
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 2-May-12 7:30am    
+5 But I think int "selectedCheckBoxesCount = Controls.OfTy..." is enough
VJ Reddy 2-May-12 7:44am    
Thank you, Shahin.
But here in selectedCheckBoxesCount = Controls, selectedCheckBoxesCount is of type int and Controls is a control collection. I could not get the point. Can you please elaborate.
Shahin Khorshidnia 2-May-12 17:49pm    
Sorry typing mistake :D I meant the last lines (11 and 12) of your code ("int selectedCheckBoxesCount = Controls.OfType<checkbox>().Count (cb => cb.Checked);") was good enough and I didn't get the intention of lines 1 to 9. You created some check boxes then you added them to the current form. (why?)
VJ Reddy 2-May-12 20:14pm    
Thank you for answering.
I created CheckBoxes etc. so that one can easily check the above statement
by creating a windows forms application and by pasting the above code in the Load event of Form.
As you have rightly pointed out the last statement before comment is the one which does the work.

Thank you.
Shahin Khorshidnia 2-May-12 20:22pm    
Thank you.
Loop through all controls on form, find each check box, if its checked then increment counter by 1.

Refer this[^] link.
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 2-May-12 7:59am    
The link doesn't consider the CheckBox is Checked.
My vote of 4
hi..
suppose you have ChekedListBox control (name as clbCount) then use the following statement to have a count of checked items in list

clbCount.CheckedItems.Count.
hope you have got the answer.
mark as solved if u've got your answer
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900