17,740
社区成员
发帖
与我相关
我的任务
分享
private void UserInfo_Load(object sender, EventArgs e)
{
LoadButton();
}
#region 循环按钮事件
private void LoadButton()
{
Button[] btns = new Button[15];
for (int i = 0; i < 15; i++)
{
btns[i] = new Button();
btns[i].Location = new Point(3, 0 + i * 29);//你把所有按钮放在同一位置吗?-> btns[i].Location = new System.Drawing.Point(100,100+ i*60);//
btns[i].Name = "btn" + i;
btns[i].Size = new System.Drawing.Size(80, 30);
btns[i].Text = i.ToString();
btns[i].Click += new EventHandler(Button_Click);
this.panel1.Controls.Add(btns[i]);
}
}
void Button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
MessageBox.Show(b.Text);
LinkLabel[] link = new LinkLabel[5];
for (int i = 0; i < 5; i++)
{
link[i] = new LinkLabel();
link[i].Location = new Point(71 + i * 60, 10);//你把所有按钮放在同一位置吗?-> btns[i].Location = new System.Drawing.Point(100,100+ i*60);//
link[i].Name = b.Text + "link" + i.ToString();
link[i].Size = new System.Drawing.Size(50, 16);
link[i].Text = b.Text + "link"+i.ToString();
link[i].Click += new EventHandler(LinkLabel_Click);
this.panel2.Controls.Add(link[i]);
}
}
void LinkLabel_Click(object sender, EventArgs e)
{
LinkLabel link = (LinkLabel)sender;
MessageBox.Show(link.Text);
}
#endregion
void Button_Click(object sender, EventArgs e)
{
panel2.SuspendLayout();
// this.SuspendLayout();
panel2.Controls.Clear();
Button b = (Button)sender;
MessageBox.Show(b.Text);
LinkLabel[] link = new LinkLabel[5];
for (int i = 0; i < 5; i++)
{
link[i] = new LinkLabel();
link[i].Location = new Point(71 + i * 60, 10);//你把所有按钮放在同一位置吗?-> btns[i].Location = new System.Drawing.Point(100,100+ i*60);//
link[i].Name = b.Text + "link" + i.ToString();
link[i].Size = new System.Drawing.Size(50, 16);
link[i].Text = b.Text + "link"+i.ToString();
link[i].Click += new EventHandler(LinkLabel_Click);
this.panel2.Controls.Add(link[i]);
}
panel2.ResumeLayout(false);//别漏掉 要是这句还是出不来效果 那就加上下面这句
// this.PerformLayout();
}
void Button_Click(object sender, EventArgs e)
{
panel2.Controls.Clear();
Button b = (Button)sender;
MessageBox.Show(b.Text);
LinkLabel[] link = new LinkLabel[5];
for (int i = 0; i < 5; i++)
{
link[i] = new LinkLabel();
link[i].Location = new Point(71 + i * 60, 10);//你把所有按钮放在同一位置吗?-> btns[i].Location = new System.Drawing.Point(100,100+ i*60);//
link[i].Name = b.Text + "link" + i.ToString();
link[i].Size = new System.Drawing.Size(50, 16);
link[i].Text = b.Text + "link"+i.ToString();
link[i].Click += new EventHandler(LinkLabel_Click);
this.panel2.Controls.Add(link[i]);
}
panel2.ResumeLayout(false);//这两句话别漏掉 要是这两句还是出不来效果
panel2.PerformLayout();//就把下面的那两句注释给去掉
// this.ResumeLayout(false);
// this.PerformLayout();
}