小编Pau*_*ran的帖子

通过在C#中使用鼠标拖动控件来移动控件

我试图通过拖动它来移动名为pictureBox1的控件.问题是,当它移动时,它会一直从一个位置移动到鼠标周围的另一个位置,但它确实跟着它...这是我的代码.如果你能帮助我,我将非常感激

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    bool selected = false;
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        selected = true;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (selected == true)
        {
            pictureBox1.Location = e.Location;
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        selected = false;
    }

}
Run Code Online (Sandbox Code Playgroud)

c# location picturebox movable mousedown

16
推荐指数
3
解决办法
5万
查看次数

标签 统计

c# ×1

location ×1

mousedown ×1

movable ×1

picturebox ×1