9

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.

SALZZZ
  • 113
  • 1
  • 1
  • 6

5 Answers5

11

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.

sloth
  • 99,095
  • 21
  • 171
  • 219
  • hmmm, thanks for this, but actually im not sure whether the clients computer would be compatible windows framework 4.0"Excuse me, for not mentioning it" – SALZZZ Dec 12 '12 at 09:30
  • Is it mandatory to have form obj at "yourform" or can it be a panel obj too.. Coz, I mite have out of context checkboxes too in the same form – SALZZZ Dec 12 '12 at 09:41
  • hey wats this checked_boxes returning.. the no. of checked boxes or something else.. Am I supposed to keep this inside a loop – SALZZZ Dec 12 '12 at 10:14
  • I need atleast 15 rep to upvote..hey I wrote this code foreach (CheckBox cbx in checked_boxes) { str += cbx.Text; } MessageBox.Show(str); But, why is it tat if the checkboxes are in order 10 11 12 13.then its displaying in opposite direction as 13 12 11 10 – SALZZZ Dec 12 '12 at 10:53
1
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.

AkshayP
  • 188
  • 3
  • 11
0

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
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

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
    }  
  }   
JonH
  • 32,732
  • 12
  • 87
  • 145
0

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

nitin-sharma
  • 110
  • 1
  • 6