Manish Mishra Manish Mishra - 3 months ago 24
C# Question

Drag and Drop one control to another control in winform

am doing something very very simple.

I have a listbox whose events are set like this :

public Form1()
{
InitializeComponent();
this.listBox1.AllowDrop = true;
this.listBox1.DragEnter += new DragEventHandler(listBox1_DragEnter);
this.listBox1.DragDrop += new DragEventHandler(listBox1_DragDrop);
}

void listBox1_DragDrop(object sender, DragEventArgs e)
{
//code to add labelText to Items of ListBox
}

void listBox1_DragEnter(object sender, DragEventArgs e)
{
//set DragDropEffects;
}


now I have a label, code for which is as follows:

private void label1_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop((sender as Label).Text, DragDropEffects.Copy);
//this.label1.DoDragDrop((sender as Label).Text, DragDropEffects.Copy);
//used one of them at a time.

}


but nothing happens. listbox DragEnter event never fires up. in fact, drag never happens.
whenever i try to drag label (text), not allowed windows cursor appears, instead of '
DragDropEffects.Copy
's cursor

Drag and Drop doesn't take place..

when I modify the listbox (and the associated code) to accept files to be dropped on it from any other window, that works perfectly.

so..am unable to perform drag from a control kept on the form to another control kept on the same form.

am I missing something? am running windows XP.

I went through this and through this

please help...

Answer

Your code does work actually. You just have to set the right drag effects in your event handlers.

void listBox1_DragDrop(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Copy;
}

void listBox1_DragEnter(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Copy;
}
Comments