我在表单上动态添加了 checkboxex。在选中复选框时,我想执行一些操作。下面是我正在使用的代码
private void GenerateSidePanelControls()
{
try
{
int topPosition = 15;
foreach (SideMenuItem sItem in _sideMenuItemList)
{
CheckBox objLabel = new CheckBox();
objLabel.Name = sItem.TagName;
objLabel.Text = sItem.TagName;
objLabel.Left = 15;
objLabel.Top = topPosition;
objLabel.Font = _normalFont;
sidePanel.Controls.Add(objLabel);
topPosition += 35;
objLabel.CheckedChanged += new System.EventHandler(this.SideLabel_Click);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void SideLabel_Click(object sender, EventArgs e)
{
int topPosition = 20;
try
{
Cursor = Cursors.Default;
CheckBox objLabel = (CheckBox)sender;
objLabel.Name = objLabel.ToString();
objLabel.Font = _boldFont;
foreach (Control ctrl in sidePanel.Controls)
{
if (ctrl is CheckBox)
{
if (((CheckBox)objLabel).Checked == true)
{
TextBox txt = new TextBox();
txt.Text = "Checked";
sidePanel.Controls.Add(objLabel);
topPosition += 25;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
在我在运行时检查复选框之前得到结果。我需要在运行时选中复选框后执行该操作。