SALZZZ
SALZZZ

Reputation: 113

Selected Checkbox from a bunch of checkboxes in .c#.net

The problem is faced under c# .NET, Visual Studio, Windows Form Application

I have a bunch of checkboxes placed randomly in one form and in one panel. So, If any checkbox is selected in the form its value is supposed to be added up.

Bottomline: Instead of using plenty of "If-else loops", to evaluate whether its been checked or not. I wanna simplify it using a "for loop ".

Is there any Checkbox group name type feature, which I can use???

I wanna code something like this:

for(int i=0;i<checkboxes.length;i++)
{
    string str;
    if(chkbox.checked)
    {
        str+=chkbox.value;
    }
}

Where checkboxes is a group name.

Upvotes: 9

Views: 19565

Answers (5)

sloth
sloth

Reputation: 101052

You can use a simple LINQ query

var checked_boxes = yourControl.Controls.OfType<CheckBox>().Where(c => c.Checked);

where yourControl is the control containing your checkboxes.


checked_boxes is now an object which implements IEnumerable<CheckBox> that represents the query. Usually you want to iterate over this query with an foreach loop:

foreach(CheckBox cbx in checked_boxes) 
{
}

You also can convert this query to a list (List<Checkbox>) by calling .ToList(), either on checked_boxes or directly after the Where(...).


Since you want to concatenate the Text of the checkboxes to a single string, you could use String.Join.

var checked_texts = yourControl.Controls.OfType<CheckBox>()
                                        .Where(c => c.Checked)
                                        .OrderBy(c => c.Text)
                                        .Select(c => c.Text);

var allCheckedAsString = String.Join("", checked_texts);

I also added an OrderBy clause to ensure the checkboxes are sorted by their Text.

Upvotes: 11

nitin-sharma
nitin-sharma

Reputation: 110

I suppose other than subscribing to event CheckedChanged there is no alternative even if it is contained in some panel or form, You have to use if else,

if it would have been web base eg asp.net or php we could use jquery because it gives us the option to loop through each particular event using .each and getting its value

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

Subscribe all checkboxes to one CheckedChanged event handler and build your string when any checkbox checked or unchecked. Following query will build string, containing names of all Form's checked checkboxes:

private void Checkbox_CheckedChanged(object sender, EventArgs e)
{
    // this will use all checkboxes on Form
    string str = Controls.OfType<CheckBox>()
                         .Where(ch => ch.Checked)
                         .Aggregate(new StringBuilder(),
                                    (sb, ch) => sb.Append(ch.Name),
                                    sb => sb.ToString());
    // use string
}

Upvotes: 0

AkshayP
AkshayP

Reputation: 188

CheckBox[] box = new CheckBox[4];  

box[0] = checkBox1;  
box[1] = checkBox2;  
box[2] = checkBox3;  
box[3] = checkBox4;

for(int i=0; i<box.length; i++)
{
      string str;
      if(box[i].checked== true)
      {
           str += i.value;
      }
} 

I think this code will work with DotNet4.0. Plz let me know any error occurs. Treat 'box' as regular array.

Upvotes: 1

JonH
JonH

Reputation: 33143

If all the checkboxes are in a groupbox you can do this:

foreach(Control c in myGroupBox.Controls)
  {
   if (c is CheckBox)
    {
    //do something
     CheckBox temp = (CheckBox)c;
     if(temp.Checked)
        //its checked
    }  
  }   

Upvotes: 0

Related Questions