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);
}
就别逃避 勇敢面对