Pergunta

This is kind of a stupid question... I'm trying to drag and drop a picturebox onto a panel. I followed some exemples, but it doesn't work. The DragDrop event of the panel is never raised. I searched thi site for a solution andfound two topics over a year old, but their solutions did not work. I created a new project, with only this code :

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            panel1.DragDrop +=new DragEventHandler(panel1_DragDrop);
            panel1.DragOver +=new DragEventHandler(panel1_DragOver);
        }

        private void panel1_DragOver(object sender, DragEventArgs e)
        {
            Console.WriteLine("DragOver");
        }

        private void panel1_DragDrop(object sender, DragEventArgs e)
        {
            Console.WriteLine("DragDrop");
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Console.WriteLine("Mouse");
            pictureBox1.DoDragDrop(pictureBox1.Text, DragDropEffects.All);
        }
    }

I also set the AllowDrop of the panel and the form to true. DragOver and MouseDown are raised. Also, when I drag the picturebox, the cursor become a barred circled, like it was an operation that wasn't allowed. Is there a way that the cursor becomes the image in the picture box? I don't want the picturebox to move, only to add an item to the panel.

Foi útil?

Solução

Richard, the problem is that drag and drop is not as simple operation as you have coded here. Here you haven't started the drag movement which should start with code and you can read more about it here... http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(v=VS.90).aspx

If you just want to move the PictureBox... dragging picturebox inside winform on runtime

And finally Drag and Drop between Instances of the same Windows Forms Application

Hope this helps.

Outras dicas

The problem is easy to solve.

You must just set in DragEnter appropriate Effect:

private void Form1_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.All;
}

After that DragDrop event is fired correctly.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top