Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

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



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

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.
C#
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.
 
Share this answer
 
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.
<lang>I've fixed the problem.


C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900