Moving a control by dragging it with the mouse in C#

后端 未结 3 1970
南旧
南旧 2020-12-08 16:47

I\'m trying to move the control named pictureBox1 by dragging it around. The problem is, when it moves, it keeps moving from a location to another location around the mouse,

3条回答
  •  萌比男神i
    2020-12-08 17:17

    try this to move pictureBox control at runtime using mouse

     private void pictureBox7_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    xPos = e.X;
                    yPos = e.Y;
                }
            }
    
            private void pictureBox7_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                PictureBox p = sender as PictureBox;
    
                if (p != null)
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        p.Top += (e.Y - yPos);
                        p.Left += (e.X - xPos);
                    }
                }
    
            }
    

提交回复
热议问题