C# - 拖放&保持控制

时间:2013-08-29 16:27:52

标签: c# drag-and-drop

所以我试图制作一个拖放应用程序来拖动面板上的东西。我以前做过,我忘记了我用它的代码。我也希望它也有一个活动。这是一个无法工作的例子:

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        PictureBox flower1 = new PictureBox();
        flower1.Image = pictureBox1.Image;
        flower1.Location = new Point(panel1.Location.X, panel1.Location.Y);
        flower1.Width = 100;
        this.Controls.Add(flower1);
        flower1.MouseDown += new MouseEventHandler(flower1_MouseDown);
    }

    void flower1_MouseDown(object sender, MouseEventArgs e)
    {
        //flower1.Location = new Point(MousePosition.X, MousePosition.Y);
    }

我希望我点击一朵花,然后将它放在面板上,然后,如果鼠标单击该控件复制到面板上,则将该位置设置为鼠标光标所在的位置。我该怎么做呢?它甚至没有重复。

编辑:刚刚意识到图像位于面板下方,使其无法被人看到。这是一个问题,现在如何让它拖放?

1 个答案:

答案 0 :(得分:0)

private void pictureBox1_Click(object sender, EventArgs e)
{
    PictureBox flower1 = new PictureBox();
    flower1.Image = pictureBox1.Image;
    flower1.Location = Point.Empty;
    flower1.Width = 100;
    flower1.Parent = panel1;
    flower1.MouseDown += new MouseEventHandler(flower1_MouseDown);
}