Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have A group of checkboxs(Name of Months) and i want to select the multiple select and check that how many checkbox is checked e.g.If one Checkbox(one month)Selected the the value assign to label 1 and if 5 checkbox is checked then label value is 5


I am implement this in C# windows form applications
Thanks

What I have tried:

I don't try on this. Because i wont understand this case this strange for me.
i have search alot on google and youtube but not get valuable information
Posted
Updated 18-Jan-19 20:33pm
v2

It's going to depend on the environment your code is running under, but basically: find all the checkboxes in the group, and count them in a loop!

For Winforms, let's assume you have your group in a Panel (to hold them together) and it becomes pretty simple:
C#
int count = 0;
foreach (Control c in myPanel.Controls)
   {
   Checkbox cb = c as Checkbox;
   if (cb != null && cb.Checked) count++;
   }
myLabel.Text = count.ToString();
It'll be similar for other environments, but that's up to you!
 
Share this answer
 
Comments
Fahim ullah 18-Jan-19 8:27am    
Not work please give other example
OriginalGriff 18-Jan-19 8:37am    
"Not work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Fahim ullah 18-Jan-19 8:58am    
Please See This And tell me how i do this
https://drive.google.com/file/d/10HoBdr1wi3A_1OkKo8sVTkT8YYkjIIuo/view?usp=sharing
OriginalGriff 18-Jan-19 9:59am    
No, I'm not watching a video, even when Google has finished "processing it".

If you want to talk, use a keyboard!
Fahim ullah 18-Jan-19 10:23am    
OK i have QUantity Label and i want to assign a value of salected checkBox.
i mean first check that how many checkbox is selected and then assign this value to QUantity Label

Example :
if 2 checkbox is selected then QUantityLabel.text = 2 and so on.

i hope you got my idea and give best answer.
You might think about whether your best strategy is to do something immediately whenever a CheckBox's 'CheckState Property is changed compared to taking deferred action based on reading the current state of a group of CheckBoxes. You often find the "deferred action" strategy used in configuration user-interfaces in combination with an "Apply" button.

A slight variation using 'OfType<> and Linq:
C#
public int HowManyChecked(Control container)
{
    return container.Controls.OfType<CheckBox>().Where(cbx => cbx.Checked).Count();
}
Note: this will not throw an error if there are no CheckBoxes. 'OfType<> requires NET FrameWork 4.5, or later.

However, it seems logical to me that you would want to do something based on which CheckBoxes are checked; so:
public IEnumerable<CheckBox> GetCbxChecked(Control container)
{
    return container.Controls.OfType<CheckBox>().Where(cbx => cbx.Checked);
}
A use-case example:
private void button1_Click(object sender, EventArgs e)
{
    var chkd = GetCbxChecked(panel1);

    if(chkd.Count() > 0)
    {
        foreach (var cbx in chkd)
        {
            switch (cbx.Name)
            {
                case "checkBox1":
                    break;
                case "checkBox2":
                    break;
                case "checkBox3":
                    break;
            }
        }
    }
}
 
Share this answer
 
Comments
Maciej Los 11-Feb-19 5:46am    
This:
return container.Controls.OfType<CheckBox>().Where(cbx => cbx.Checked).Count();

can be simplified to:
return container.Controls.OfType<CheckBox>().Count(cbx => cbx.Checked);


Cheers!
Maciej
BillWoodruff 11-Feb-19 7:08am    
cool ! there's some interesting discussion of where + count compared to using count with a predicate function here:

https://stackoverflow.com/questions/25787555/linq-performance-count-vs-where-and-count/25789631#25789631

Depending on the application, I might just maintain a list of checked controls by using a CheckChanged Event Handler assigned to all CheckBoxes, and updating a List.

thanks, Bill

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