I created a user control to host some checkboxes that are created dynamically based on a list from a SQL table. I need to provide an option to select/unselect all checkboxes. This is easy to do when not created dynamically. The problem I find is that once I click on (ALL), yes, it will check or uncheck all checkboxes, but will not allow individual selection.
The code that generates the checkboxes is this:
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++;
}
//****************************//
}
The code that is used to check/uncheck is this:
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);
}
Obviously, if I remove the foreach loop, it will allow me to do individual selections. Any suggestion to make it work properly?
This is the codebehind for my user control:
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);
}
#endregion
public System.Windows.Forms.FlowLayoutPanel flpanel;
}
Aucun commentaire:
Enregistrer un commentaire