Click here to Skip to main content
Sign Up to vote bad
good
See more: C#VisualStudio2010
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:50
Edited 13 Nov '12 - 1:41

Comments
Alan N - 13 Nov '12 - 7:20
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.

2 solutions

I've fixed the problem.
 

private void ltbCamposLinhas_MouseDown(object sender, MouseEventArgs e)
        {
            int indice = ltbCamposLinhas.IndexFromPoint(e.X, e.Y);
            if (ltbCamposLinhas.Items.Count == 0 || indice < 0) return;
            string s = ltbCamposLinhas.Items[indice].ToString();
            DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
 
            if (dde1 == DragDropEffects.All)
            {
      ltbCamposLinhas.Items.RemoveAt(ltbCamposLinhas.IndexFromPoint(e.X, e.Y));
            }
        }
 
Ty all.
  Permalink  
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  
Comments
werfog - 13 Nov '12 - 11:01
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.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 315
1 Sergey Alexandrovich Kryukov 259
2 Aarti Meswania 250
3 Slacker007 240
4 Ron Beyer 190
0 Sergey Alexandrovich Kryukov 8,743
1 OriginalGriff 7,124
2 CPallini 3,678
3 Rohan Leuva 3,011
4 Maciej Los 2,403


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 13 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid