我已动态向面板添加复选框。现在我怎样才能收到“你已经检查过1或2或3 ......”的警告信息。当复选框被选中?
CheckBox[] premiumticket = new CheckBox[50];
private void Form1_Load(object sender, EventArgs e)
{
var panel1 = new Panel()
{
Size = new Size(600, 70),
Location = new Point(20, 130),
BorderStyle = BorderStyle.FixedSingle
};
for (int i = 0; i < 20; i++)
{
premiumticket[i]=new CheckBox();
premiumticket[i].Text=(i+1).ToString();
premiumticket[i].Name=(i+1).ToString();
premiumticket[i].Location=new Point(x,y);
panel1.Controls.Add(premiumticket[i]);
x = x - 55;
if (x < 55)
{
y = y + 20;
x = 550;
}
}
x = 550; y = 10;
Controls.Add(panel1);
}
答案 0 :(得分:1)
为每个CheckBox添加一个事件处理程序:
public void Checkbox_CheckedChanged(Object sender, EventArgs e) {
CheckBox cb = (CheckBox)sender;
MessageBox.Show( cb.Name + " was clicked!");
}
for (int i = 0; i < 20; i++) {
premiumticket[i] = new CheckBox();
premiumticket[i].OnCheckChanged += new EventHandler( Checkbox_CheckedChange );
...
}
答案 1 :(得分:0)
为每个复选框动态创建事件并将警报代码放在其上
答案 2 :(得分:0)
将此添加到您的for循环:
premiumticket[i].OnCheckChanged += new EventHandler( premiumTicketChanged );
复选框切换处理程序:
public void premiumTicketChanged (Object sender, EventArgs e)
{
int ticketCount = premiumticket.Count(c => c.Checked);
MessageBox.Show( string.Format("You have checked {0} checkboxes....", ticketCount));
}
答案 3 :(得分:0)
var checkBox = new CheckBox { ID = "WCCheckBox" + item.ItemID.ToString(), ItemID = item.ItemID, Checked = item.UserValue == "1", CssClass = "wounditem" };
您的实施可能是另一个。
答案 4 :(得分:0)
动态添加控件后,您可以使用AddHandler来管理此控件的事件。 请记住将复选框autopostback属性设置为true。