tags:

views:

28

answers:

2

I have a groupbox which contains 10+ checkboxes. I would like to build a string that concatenates the Checkbox.Text of all the checkboxes which are checked.

Of course anytime the checked state changes for any of the checkboxes it will need to rebuild the string. How can I go about doing this?

Note: This needs to happen on the fly as checkboxes are checked/unchecked.

This is the idea I had going, but I feel like that there is a better way to do this - and also I'm not sure how I am going to remove the strings when an item is unchecked.

Any thoughts?

private void CheckBox_CheckedChanged(System.Object sender, System.EventArgs e)
{
    if (((CheckBox)sender).Checked)
    {
        switch (((CheckBox)sender).Name)
        {
            case "CheckBox1":
                sb = sb + "This is checkbox 1." + "\n";
                break;
            case "CheckBox2":
                sb = sb + "This is checkbox 2." + "\n";
                break;
            case "CheckBox3":
                sb = sb + "This is checkbox 3." + "\n";
                break;
            case "CheckBox4":
                sb = sb + "This is checkbox 4." + "\n";
                break;
            case "CheckBox5":
                sb = sb + "This is checkbox 5." + "\n";
                break;
            case "CheckBox6":
                sb = sb + "This is checkbox 6." + "\n";
                break;
            case "CheckBox7":
                sb = sb + "This is checkbox 7." + "\n";
                break;
            case "CheckBox8":
                sb = sb + "This is checkbox 8." + "\n";
                break;
            case "CheckBox9":
                sb = sb + "This is checkbox 9." + "\n";
                break;
            case "CheckBox10":
                sb = sb + "This is checkbox 10." + "\n";
                break;
        }
    }
    else
    {
    }
}
A: 

The code below works: however, I'm still interested in hearing if there is a better way to do this?

 private void CheckBox_CheckedChanged(System.Object sender, System.EventArgs e)
        {
            if (((CheckBox)sender).Checked)
            {
                switch (((CheckBox)sender).Name)
                {
                    case "CheckBox1":
                        sb.Append("This is checkbox 1.\r\n");
                        break;
                    case "CheckBox2":
                        sb.Append("This is checkbox 2.\r\n");
                        break;
                    case "CheckBox3":
                        sb.Append("This is checkbox 3.\r\n");
                        break;
                    case "CheckBox4":
                        sb.Append("This is checkbox 4.\r\n");
                        break;
                    case "CheckBox5":
                        sb.Append("This is checkbox 5.\r\n");
                        break;
                    case "CheckBox6":
                        sb.Append("This is checkbox 6.\r\n");
                        break;
                    case "CheckBox7":
                        sb.Append("This is checkbox 7.\r\n");
                        break;
                    case "CheckBox8":
                        sb.Append("This is checkbox 8.\r\n");
                        break;
                    case "CheckBox9":
                        sb.Append("This is checkbox 9.\r\n");
                        break;
                    case "CheckBox10":
                        sb.Append("This is checkbox 10.\r\n");
                        break;
                }

                txtBox.Text = sb.ToString();
            }
            else
            {
                switch (((CheckBox)sender).Name)
                {
                    case "CheckBox1":
                        sb.Replace("This is checkbox 1.\r\n", "");
                        break;
                    case "CheckBox2":
                        sb.Replace("This is checkbox 2.\r\n", "");
                        break;
                    case "CheckBox3":
                        sb.Replace("This is checkbox 3.\r\n", "");
                        break;
                    case "CheckBox4":
                        sb.Replace("This is checkbox 4.\r\n", "");
                        break;
                    case "CheckBox5":
                        sb.Replace("This is checkbox 5.\r\n", "");
                        break;
                    case "CheckBox6":
                        sb.Replace("This is checkbox 6.\r\n", "");
                        break;
                    case "CheckBox7":
                        sb.Replace("This is checkbox 7.\r\n", "");
                        break;
                    case "CheckBox8":
                        sb.Replace("This is checkbox 8.\r\n", "");
                        break;
                    case "CheckBox9":
                        sb.Replace("This is checkbox 9.\r\n", "");
                        break;
                    case "CheckBox10":
                        sb.Replace("This is checkbox 10.\r\n", "");
                        break;
                }

                txtBox.Text = sb.ToString();
            }
        }
+1  A: 

You can do something like this, assuming the check box name has the format 'CheckBox#':

private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    var checkboxes = from c in groupBox1.Controls.OfType<CheckBox>()
                        where c.Checked
                        orderby int.Parse(c.Name.Substring(8))
                        select c;

    sb.Clear();

    foreach (var cb in  checkboxes)
    {
        sb.Append(string.Format("This is checkbox {0}", cb.Name.Substring(8)));
        sb.Append(Environment.NewLine);
    }

    textBox1.Text = sb.ToString();
}

If you don't want to worry about the format of the checkbox names, you can use AlphanumComparator, though you'll have to make some minor changes to the source to make it generic:

AlphanumComparator<string> comparator = new AlphanumComparator<string>();

var checkboxes = (from c in groupBox1.Controls.OfType<CheckBox>()
                  where c.Checked
                  select c).OrderBy(c => c.Name, comparator);
adrift
Works, much easier to follow, + I've just started learning Linq, so bonus for actually getting to apply it. Thanks!