简体   繁体   中英

populating textbox based on checkbox.checked value of numerous checkboxes

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
    {
    }
}

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);

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();
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM