Note that there are some explanatory texts on larger screens.

plurals
  1. POMove PictureBox C#
    primarykey
    data
    text
    <p>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:</p> <pre><code>PictureBox pic = new PictureBox(); </code></pre> <p>How can I switch between the two pictureboxes by clicking? </p> <p>*<strong><em>UPDATE</em>*</strong> 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?</p> <p>*<strong><em>UPDATE</em>*</strong> The code I have:</p> <pre><code> 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload