比如代码:
for(int i=0;i<5;i++)
{
PictureBox pic=new PictureBox();
pic.MouseEnter += new EventHandler(pb_MouseEnter);
}
为什么只有第一个PictureBox能执行MouseEnter事件?
for(int i=0;i<5;i++)
{
PictureBox pic=new PictureBox();
pic.MouseEnter += new EventHandler(pb_MouseEnter);
}
为什么只有第一个PictureBox能执行MouseEnter事件?
还是按1楼的方法做吧。
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 2; i++)
{
Button b = new Button();
b.Text = "b" + i.ToString();
b.Location = new Point(i * 100, 0);
b.Click+=new EventHandler(test);
this.Controls.Add(b);
}
} private void test(object sernder,EventArgs e)
{
MessageBox.Show("test");
}
经过测试,我认为楼主在吹牛。
int num = 0;
Button bt = (Button)sender;
string id = bt.Name;
BLL.HRConfig.HR_StepInfo step = new HR_StepInfo();
//DataSet ds = step.GetList(" SectionID=" + id);
DataSet ds = step.GetList(int.Parse(Common.CurrentUserInfo.UserId), string.Empty, int.Parse(id));
num = ds.Tables[0].Rows.Count;
for (int i = 0; i < num; i++)
{
Button btnstep = new Button();
btnstep.Name = ds.Tables[0].Rows[i]["StepID"].ToString();
btnstep.Tag = ds.Tables[0].Rows[i]["StepWindow"].ToString();
btnstep.Width = 110;
btnstep.Height = 50;
btnstep.Text = ds.Tables[0].Rows[i]["StepDesc"].ToString();
btnstep.Click += new EventHandler(btnstep_Click);
this.tableStepList.Controls.Add(btnstep, 1, i);
}