我如何 check/uncheck 下拉列表中用户控件中的所有复选框
How can I check/uncheck all checkboxes that are in a user control in a dropdownlist
我创建了一个用户控件来承载一些基于 SQL table 中的列表动态创建的复选框。我需要为 select/unselect 所有复选框提供一个选项。如果不是动态创建,这很容易做到。我发现的问题是,一旦我点击(全部),是的,它会选中或取消选中所有复选框,但不允许单独选择。
生成复选框的代码是这样的:
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++;
}
//****************************//
}
用于check/uncheck的代码是这样的:
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
循环,它将允许我进行单独的选择。有什么建议可以让它正常工作吗?
这是我的用户控件的代码隐藏:
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;
如果支票的发件人是 "cb(All)",则您只能 check/uncheck 所有其他复选框。并且您必须在此循环中省略 "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;
}
}
}
真正的乐趣开始了,如果你有目标也 check/uncheck "cb(ALL)" 自动,如果用户手动 checked/unchecked 所有其他框。
我创建了一个用户控件来承载一些基于 SQL table 中的列表动态创建的复选框。我需要为 select/unselect 所有复选框提供一个选项。如果不是动态创建,这很容易做到。我发现的问题是,一旦我点击(全部),是的,它会选中或取消选中所有复选框,但不允许单独选择。
生成复选框的代码是这样的:
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++;
}
//****************************//
}
用于check/uncheck的代码是这样的:
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
循环,它将允许我进行单独的选择。有什么建议可以让它正常工作吗?
这是我的用户控件的代码隐藏:
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;
如果支票的发件人是 "cb(All)",则您只能 check/uncheck 所有其他复选框。并且您必须在此循环中省略 "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;
}
}
}
真正的乐趣开始了,如果你有目标也 check/uncheck "cb(ALL)" 自动,如果用户手动 checked/unchecked 所有其他框。