1

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...

Community
  • 1
  • 1
Manish Mishra
  • 12,163
  • 5
  • 35
  • 59

3 Answers3

3

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;
}
Wolfgang Ziegler
  • 1,675
  • 11
  • 23
2

Check if ListBox.AllowDrop is set to TRUE or not

Arif Eqbal
  • 3,068
  • 1
  • 18
  • 10
0

The following is an example of what you need, with all the code (adding it here for whoever finds this post).

#region Initial Values
//Constructor:
public Form1() {
   InitializeComponent();
}

private void Form1_Load( object sender, EventArgs e ) {
   InitialValues();
}

private void InitialValues() {
   PrepareDragAndDrop();
}
#endregion Initial Values

#region Drag & Drop

private void PrepareDragAndDrop() {
   //For the object that receives the other dragged element:
   TheSamplListBox.AllowDrop = true;
   TheSamplListBox.DragEnter += TheSamplListBox_DragEnter;
   TheSamplListBox.DragLeave += TheSamplListBox_DragLeave;
   TheSamplListBox.DragDrop  += TheSamplListBox_DragDrop;

   //For the object that will be dragged:
   TheSampleLabel.MouseDown += ( sender, args ) => DoDragDrop( TheSampleLabel.Text, DragDropEffects.Copy );
}

private void TheSamplListBox_DragEnter( object theReceiver, DragEventArgs theEventData ) {
   theEventData.Effect = DragDropEffects.Copy;

   //Only the code above is strictly for the Drag & Drop. The following is for user feedback:
   //You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
   var theReceiverListBox = (ListBox) theReceiver;

   theReceiverListBox.BackColor = Color.LightSteelBlue;
}

private void TheSamplListBox_DragLeave( object theReceiver, EventArgs theEventData ) {
   //No code here for the Drag & Drop. The following is for user feedback:
   //You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
   var theReceiverListBox = (ListBox) theReceiver;

   theReceiverListBox.BackColor = Color.White;
}

private void TheSamplListBox_DragDrop( object theReceiver, DragEventArgs theEventData ) {
   //You can use [TheSamplListBox] but this approach allows for multiple receivers of the same type:
   var theReceiverListBox = (ListBox) theReceiver;

   //Get the data being dropped. In this case, a string:
   var theStringBeingDropped = theEventData.Data.GetData( "System.String" );

   //Add the string to the ListBox:
   theReceiverListBox.Items.Add( theStringBeingDropped );

   //Only the code above is strictly for the Drag & Drop. The following is for user feedback:
   theReceiverListBox.BackColor = Color.White;
}
#endregion Drag & Drop

.

Jelgab
  • 1,840
  • 19
  • 12