如何在下拉列表中选中/取消选中用户控件中的所有复选框

时间:2017-11-13 15:59:47

标签: c# winforms checkbox user-controls

我创建了一个用户控件来托管一些基于SQL表中的列表动态创建的复选框。我需要提供一个选项来选择/取消选中所有复选框。不动态创建时很容易做到这一点。我发现的问题是,一旦我点击(全部),是的,它将检查或取消选中所有复选框,但不允许单独选择。

生成复选框的代码是:

public void GenerateCheckboxesOnUserControl()
{
    // Create user control.
    UserControl2 flp = new UserControl2();

    UserControl2 userControl2 = new UserControl2();
    userControl2.BorderStyle = BorderStyle.None;

    this.customComboBox4.DropDownControl = userControl2.flpanel;


    //*****************************//

    List<string> ItemList = new List<string>();

    ItemList.Add("Pending");
    ItemList.Add("New");
    ItemList.Add("Started");
    ItemList.Add("Declined");
    ItemList.Add("Completed");
    ItemList.Add("Accepted");
    ItemList.Add("Close");
    ItemList.Add("(ALL)");

    int i = ItemList.Count;

    CheckBox[] box = new CheckBox[i];
    _cbStatus = box;

    for (i = 0; i < ItemList.Count; i++)
    {

        box[i] = new CheckBox();
        box[i].Name = "cb" + ItemList[i].ToString();
        box[i].Tag = ItemList[i];
        box[i].Text = ItemList[i].ToString();
        box[i].Focus();

        box[i].BringToFront();
        box[i].CheckedChanged += new System.EventHandler(this._cbStatus_CheckedChanged);

        this.customComboBox4.DropDownControl.Controls.Add(box[i]);
        count++;

    }

    //****************************//
}

用于检查/取消选中的代码是:

public void _cbStatus_CheckedChanged(object sender, EventArgs e)
{
    if (sender is CheckBox == false) return;

    UserControl2 userControl2 = new UserControl2();

    string message = string.Empty;
    string m = "";
    for (int i = 0; i < count; i++)
    {
        if (_cbStatus[i].Checked)
        {
            m += _cbStatus[i].Name + ", ";
            message += string.Format("boxes[{0}] is clicked\n ", i + " " + _cbStatus[i].Name);
        }

        foreach (Control cbStatus in customComboBox4.DropDownControl.Controls)
        {
            CheckBox cb = (CheckBox)cbStatus;
            if (cb.Name == "cb(ALL)" && cb.Checked)
            {
                _cbStatus[i].Checked = true; 
            }
            else
                if (cb.Name == "cb(ALL)" && !cb.Checked)
                {
                    _cbStatus[i].Checked = false;
                }    
        }
    }

    customComboBox4.Text = m;
    //MessageBox.Show(message);
}

显然,如果我删除foreach循环,它将允许我进行单独选择。有什么建议让它正常工作吗?

Unchecked Checked

这是我的用户控件的代码隐藏:

private void InitializeComponent()
{
    this.flpanel = new System.Windows.Forms.FlowLayoutPanel();
    this.SuspendLayout();
    // 
    // flpanel
    // 
    this.flpanel.AutoScroll = true;
    this.flpanel.Location = new System.Drawing.Point(4, 13);
    this.flpanel.Name = "flpanel";
    this.flpanel.Size = new System.Drawing.Size(215, 135);
    this.flpanel.TabIndex = 0;
    // 
    // UserControl2
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.BackColor = System.Drawing.SystemColors.ActiveCaption;
    this.Controls.Add(this.flpanel);
    this.Name = "UserControl2";
    this.Size = new System.Drawing.Size(222, 151);
    this.ResumeLayout(false);        
}

public System.Windows.Forms.FlowLayoutPanel flpanel;

1 个答案:

答案 0 :(得分:1)

如果支票的发件人是“cb(全部)”,您只能选中/取消选中所有其他复选框。你必须在这个循环中省略“cb(All)”本身。

所以你的代码应该是这样的:

var eventSendingCheckbox = sender as CheckBox
if( eventSendingCheckbox.Name == "cb(ALL)" )
{
    foreach( Control cbStatus in customComboBox4.DropDownControl.Controls )
    {
         if( cbStatus != eventSendingCheckbox )
         {
             cbStatus.Checked = eventSendingCheckbox.Checked;
         }
    }
}

真正有趣的是,如果您的目标是自动检查/取消选中“cb(ALL)”,如果用户手动选中/取消选中所有其他框。