Click here to Skip to main content
12,501,082 members (43,517 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# VS2010
Hi everyone, i've a question.

I'm trying to do one code that the user use the drop and drag, but my problem is when i pressing two times on listBox. If the listBox is null then nothing happen, but if have some text then he stop and go to this part of code:

string s = ltbCamposLinhas.Items[ltbCamposLinhas.IndexFromPoint(e.X, e.Y)].ToString();


private void ltbCamposLinhas_MouseDown(object sender, MouseEventArgs e)
        {
            if (ltbCamposLinhas.Items.Count == 0) return;
            string s = ltbCamposLinhas.Items[ltbCamposLinhas.IndexFromPoint(e.X, e.Y)].ToString();
            DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
 
            if (dde1 == DragDropEffects.All)
            {
                ltbCamposLinhas.Items.RemoveAt(ltbCamposLinhas.IndexFromPoint(e.X, e.Y));
            }
        }


If you guys could help would be awesome.

Well, first i want to drag items from listBox1 to listBox2.

Second i want to drag items from listBox2 to out and then they will delete.

But my problem is none of them, because, if i press 2 times with mouse in listBox1 with items(text) he show one error:

InvalidArgument=Value '-1
Posted 13-Nov-12 0:50am
Updated 13-Nov-12 1:41am
v2
Comments
Alan N 13-Nov-12 7:20am
   
Are you trying to rearrange listbox items OR drag items out of the listbox and drop them on a different control.
Edit your question using the "Improve Question" button to clarify what you want to do.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I think your code in the original question and your solution is far too simple.

You should use separate events to select an object (MouseDown) and to initiate the drag drop operation (MouseMove).

In my code example DragDropEffects is set to 'Move' if the item was dropped onto listbox2. In that case it's ok to delete the item from listbox1.

In the other situation where the item has been dragged and the mouse button released when not over listbox2 the effect will be 'None'. Now we delete only if the cursor is outside the bounds of the form.
private Object itemToDrag;
 
private void listBox1_MouseDown(object sender, MouseEventArgs e) {
  itemToDrag = null;
  if (e.Button == MouseButtons.Left) {
    Int32 idx = listBox1.IndexFromPoint(e.Location);
    if (idx != ListBox.NoMatches) {  // NoMatches = -1
      itemToDrag = listBox1.Items[idx];
      Debug.Print("Item selected for drag: {0}", itemToDrag);
    }
  }
}
 
private void listBox1_MouseMove(object sender, MouseEventArgs e) {
  if (e.Button == MouseButtons.Left && itemToDrag != null) {
    Debug.Print("DragDrop starting");
    DragDropEffects effect = listBox1.DoDragDrop(itemToDrag, DragDropEffects.Move);
    Debug.Print("DragDrop ended : Effect {0}", effect);
    if (effect == DragDropEffects.Move) {
      Debug.Print("Item moved");
      listBox1.Items.Remove(itemToDrag);
    } else if (effect == DragDropEffects.None) {
      // Is mouse currently outside the form
      if (!this.DesktopBounds.Contains(Cursor.Position)) {
        Debug.Print("Item deleted");
        listBox1.Items.Remove(itemToDrag);
      }
    }
    itemToDrag = null;
  }
}
 
private void listBox2_DragDrop(object sender, DragEventArgs e) {
  String droppedItem = (String)e.Data.GetData(DataFormats.StringFormat);
  Debug.Print("listbox2 DragDrop  {0}", droppedItem);
  listBox2.Items.Add(droppedItem);
}
 
private void listBox2_DragEnter(object sender, DragEventArgs e) {
  e.Effect = DragDropEffects.Move;
  Debug.Print("DragEnter listbox2");
}

Alan.
  Permalink  
v2
Comments
werfog 13-Nov-12 11:01am
   
I voted your answer 4 because your code looked more classical then author's one.
Just 4 because you seem to be a professional in programing.
Best wishes.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100