简体   繁体   中英

C# - Drag and Drop & Keep Control

So I am trying to make a drag and drop application that drags something on a panel. I did it before and I have forgot the code that I used for it. I would also like it to have an event too. Here is an example that failed to work:

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

I wanted me to click on a flower, then it would be placed onto the panel then, if the mouse is clicked over that control duplicated onto the panel, then make the location the mouse cursors location. How would I go about doing any of this? It does not even appear to duplicate.

EDIT: Just realised that the image is underneath the panel making it not able to be seen. That's one issue, now how do I get it to drag and drop?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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