Here's the sitch. I click a button and create a new PictureBox no problem. When I click and drag, I move the picture to it's new location. Now, when I click the button again, I create a new instance of the same PictureBox and when I try to move the old one, I end up moving the newly created box. I take it this is because they both have the same name:
PictureBox pic = new PictureBox();
How can I switch between the two pictureboxes by clicking?
*UPDATE* Thanks to Nilotpal's answer I've managed to solve the above problem. Only thing is the picturebox now seems to shake, or switch locations back and fourth between the other instance and the one I'm dragging. Either way, I'm really unsure about how to solve this. Any ideas?
*UPDATE* The code I have:
private void code128ToolStripMenuItem_Click(object sender, EventArgs e)
{
bNum++;
Barcode barcode = new Barcode();
pic = new PictureBox();
pic.Name = "bCode" + bNum;
pic.SizeMode = PictureBoxSizeMode.AutoSize;
pic.Image = barcode.createBarcode(BarcodeLib.TYPE.CODE128, 300, 100, "123456789");
pic.Show();
labelHolder.Controls.Add(pic);
pic.BringToFront();
pic.MouseDown += pic_MouseDown;
pic.MouseMove +=pic_MouseMove;
pic.MouseUp += pic_MouseUp;
}
PictureBox thisPB;
private void pic_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
oldX = e.X;
oldY = e.Y;
}
private void pic_MouseMove(object sender, MouseEventArgs e)
{
if(mouseDown)
{
thisPB = (PictureBox)sender;
thisPB.Location = new Point(pic.Location.X - (oldX - e.X), pic.Location.Y - (oldY - e.Y));
this.Refresh();
}
}
private void pic_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}