基于checkbox.checked多个复选框的值填充文本框

时间:2010-10-13 14:46:31

标签: c# winforms

我有一个包含10个以上复选框的组合框。我想构建一个字符串,连接所有选中的复选框的Checkbox.Text。

当然,只要任何复选框的选中状态发生变化,就需要重建字符串。我怎么能这样做呢?

注意:这需要在检查/取消选中复选框时动态发生。

这是我的想法,但我觉得有更好的方法可以做到这一点 - 而且我也不确定如果取消选中某个项目,我将如何移除字符串。

有什么想法吗?

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

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作,假设复选框名称的格式为“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();
}

如果您不想担心复选框名称的格式,可以使用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);

答案 1 :(得分:0)

以下代码有效:但是,如果有更好的方法,我仍然有兴趣听听吗?

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