title: C# Drag and Drop From listBox
body: <p>I'm trying to build a simple interface that allows users to drop files into a <code>listBox</code> to add them to a process, and to drag them out to remove them. Everything is working fine, but I'd like to add one feature to make it just a tad more sophisticated. </p>
  
  <p>Right now, I have the removal of the item tied to the <code>DragLeave</code> event, which means that as soon as the mouse leaves the box, the item is removed. But I'd like for users to be able to change their minds. In other words, if they realize they're dragging the wrong file out, I'd like them to be able to move the mouse back into the <code>listBox</code> and release the mouse to cancel the action. I'm thinking that means I need to be able to capture the <code>MouseUp</code> event instead of the <code>DragLeave</code> event. But that hasn't been successful so far. </p>
  
  <p>Below is the code I'm currently using for removing files dragged out. How can I modify to keep the files from being removed form the list until the user lets the mouse button go?</p>
  
  <pre><code>private void listBox1_MouseDown(object sender, MouseEventArgs e)
  {
      if (listBox1.Items.Count == 0)
      { 
          return; 
      }
  
      int index = listBox1.IndexFromPoint(e.X, e.Y);
      string s = listBox1.Items[index].ToString();
      DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
  }
  
  private void listBox1_DragLeave(object sender, EventArgs e)
  {
      ListBox lb = sender as ListBox;
      lb.Items.Remove(lb.SelectedItem);
  }
  </code></pre>
  
  <p><strong>Edit 2013/05/16</strong></p>
  
  <p>The comments and answers so far have been useful, but I realize my question isn't clear enough. In this case, I'm displaying a dialog separate from the parent form that is basically as big as the <code>listBox</code>. When someone drags a file out of the list, they're dragging it off the form completely. Have I backed myself into a corner by doing this? I recognize I'm making it harder than it has to be, but I'd still like to see how it would work if it's possible.</p>