C#如何动态的生成多个pictureBox控件,生成的pictureBox能实现不同的点击事件?
其实我的想法是这样的:我想动态的生成多个pictureBox,点击pictureBox1,然后pictureBox1控件就显示“pictureBox1”,点击pictur...
其实我的想法是这样的:我想动态的生成多个pictureBox,点击pictureBox1,然后pictureBox1控件就显示“pictureBox1”,点击pictureBox2,显示“pictureBox2”,就是点击不同的picturebox,就显示该picturebox的名字
展开
3个回答
展开全部
private int _NO = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int x = 0;
int y = _NO * 100;
CreatePictureBox(_NO.ToString(), x, y);
++_NO;
}
private bool CreatePictureBox(string name, int locX, int locY)
{
try
{
PictureBox pb = new PictureBox();
pb.Name = name;
pb.Location = new Point(locX, locY);
pb.BackColor = Color.LightBlue;
this.Controls.Add(pb);
pb.Click += new EventHandler(PictureBox_Click);
return true;
}
catch
{
return false;
}
}
private void PictureBox_Click(object sender, EventArgs e)
{
PictureBox pb = (PictureBox)sender;
MessageBox.Show(pb.Name);
}
展开全部
public Form1()
{
InitializeComponent();
for (int i = 1; i <= 3; i++)
{
PictureBox p = new PictureBox();
p.Name = "pictureBox" + i.ToString();
p.Size = new System.Drawing.Size(50, 50);
p.Top = 20;
p.Left = i * 60;
p.Click += F;
p.BackColor = Color.Red;
Controls.Add(p);
}
}
private void F(object sender, EventArgs e)
{
PictureBox p = sender as PictureBox;
MessageBox.Show(p.Name);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
就别逃避 勇敢面对
追问
什么意思!能帮我解决这个问题吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询