简体   繁体   English

移动PictureBox C#

[英]Move PictureBox C#

Here's the sitch. 这是女巫。 I click a button and create a new PictureBox no problem. 我单击一个按钮并创建一个新的PictureBox没问题。 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. 现在,当我再次单击该按钮时,将创建同一PictureBox的新实例,当尝试移动旧的PictureBox时,最终将移动新创建的盒子。 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. * 更新 *感谢Nilotpal的回答,我设法解决了上述问题。 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;

    }
    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. 此举动摇不定,您可以根据需要进行更改。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM