Alen
Alen

Reputation: 145

Move PictureBox C#

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;

    }

Upvotes: 1

Views: 10322

Answers (3)

Ryan Williams
Ryan Williams

Reputation: 1620

You may find this helpful.

static class ExtensionMethods
{
    public static Point Add(this Point original, Point value)
    {
        return new Point(original.X + value.X, original.Y + value.Y);
    }

    public static Point Subtract(this Point original, Point value)
    {
        return new Point(original.X - value.X, original.Y - value.Y);
    }
}

Upvotes: 1

Alen
Alen

Reputation: 145

Fixed it!

Old Code:

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.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;
    }

Working Code:

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(thisPB.Location.X - (oldX - e.X), thisPB.Location.Y - (oldY - e.Y));

                this.Refresh();                
        }            
    }


    private void pic_MouseUp(object sender, MouseEventArgs e)
    {
        mouseDown = false;           
    }

Upvotes: 2

nilobarp
nilobarp

Reputation: 4084

    private void button1_Click(object sender, EventArgs e)
    {
        PictureBox pb = new PictureBox();
        pb.Top = 200;
        pb.Left = 200;
        pb.BackColor = Color.Gray;
        pb.MouseMove += new MouseEventHandler(pb_MouseMove);
        this.Controls.Add(pb);
    }

    void pb_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            PictureBox thisPB = (PictureBox)sender;
            thisPB.Left = e.X;
            thisPB.Top = e.Y;
        }
    }

The move will be shaky, you can change that according to your need.

Upvotes: 2

Related Questions