简体   繁体   English

根据多个复选框的checkbox.checked值填充文本框

[英]populating textbox based on checkbox.checked value of numerous checkboxes

I have a groupbox which contains 10+ checkboxes. 我有一个包含10个以上复选框的组框。 I would like to build a string that concatenates the Checkbox.Text of all the checkboxes which are checked. 我想构建一个字符串,该字符串将所有选中的Checkbox.Text连接起来。

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#': 您可以执行以下操作,假设复选框名称的格式为“ 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 ,尽管您必须对源进行一些小的更改才能使其通用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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