问题
I want to create an ability for the user in my app to create pictureboxs everytime the user clicks on the main picturebox (I want to keep the pictureboxs and give an infinity picturebox creating ability to the User)
The code:
PictureBox Pic = new PictureBox();
Pic = pictureBox2;
Pic.Left = e.X;
Pic.Top = e.Y;
Pic.Visible = true;
回答1:
you need to register to the event of clicking the pictureBox
and create a new pirtureBox
when clicking on it:
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox1.Location = new System.Drawing.Point(319, 32);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(100, 50);
this.pictureBox1.TabIndex = 7;
this.pictureBox1.TabStop = false;
// THE IMAGE IS UP TO YOU TO ADD.USE THIS -this.pictureBox1.Image =
this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
this.Controls.Add(pb);
and in the pictureBox1_MouseClick
event do:
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
PictureBox pb = new System.Windows.Forms.PictureBox();
pb.Location = new System.Drawing.Point(319, 32);// THE LOCATION AND CONTEXT IS UP TO YOU
pb.Name = "pictureBox1";
pb.Size = new System.Drawing.Size(100, 50);
pb.TabIndex = 7;
pb.TabStop = false;
this.Controls.Add(this.pictureBox1);
}
well, I'm adding here as rene saying in the comment, that if you are using big pictures or large amount of pictures you might run out of memory very fast. in that case you should do .Dispose()
to pictures you no longer need
来源:https://stackoverflow.com/questions/18810819/how-to-create-multiple-picture-boxes-and-keep-them