title: C# Moving own controls around and make them droppable
body: <p>A good morning to you all!
  Yesterday I ran into a problem while trying to implement a custom DragDrop for my own controls in a <code>WinForms</code> application.</p>
  
  <p>I have a form which can dynamically create instances of two of my own controls. These controls consist of some controls themselves, such as buttons, labels and listboxes/treeviews. The controls serve as a representation for a certain dataset. Now, we all know the class diagrams in VS. There you have these boxes representing classes. You can move the boxes around on the canvas by doing - what I would call - dragging them around, much like you would drag around files. To accomplish this with my own controls I have done the following:</p>
  
  <pre><code>public partial class MyControl: UserControl
  {
  private Control activeControl;
  
  private void GeneralMouseDown(MouseEventArgs e)
  {
      activeControl = this;
      previousLocation = e.Location;
      Cursor = Cursors.Hand;   
  }
  
  private void GeneralMouseMove(Control sender, MouseEventArgs e)
  {
      if (activeControl == null || activeControl != sender)
          return;
      var location = activeControl.Location;
      location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);
      activeControl.Location = location;
  }
  
  private void GeneralMouseUp()
  {
      activeControl = null;
      Cursor = Cursors.Default;
  }
  }
  </code></pre>
  
  <p>The controls on my control which I want to "grab" for dragging <code>MyControl</code> have their MouseDown-, MouseMove- and MouseUp-events pointing to these three methods. As a result I can move my control about on the form freely, just as I want to.</p>
  
  <p>Here comes the tricky bit:
  The datasets I have controls for can be in hierarchical dependencies, which means, one control represents detailling of a component of the other, which is why my controls have Listboxes or TreeViews. To establish such a hierarchical dependency I would very much like to DragDrop the lower-order-control on the listbox of my higher-order-control, causing data to be transfered.</p>
  
  <p>I know how to set up my <code>DragEnter</code> and <code>DragDrop</code> methods for the listbox, as I have done so previously with files. Just for completeness:</p>
  
  <pre><code>private void lst_MyControl_DragEnter(object sender, DragEventArgs e)
  {
      if (e.Data.GetDataPresent(typeof(MyControl)))
          e.Effect = DragDropEffects.Move;
      else e.Effect = DragDropEffects.None;
  }
  </code></pre>
  
  <p>Here's the problem: As I am moving my control about (which gets repainted at every position, giving a very much wanted effect!), when I "drag" it over the target-listbox, the <code>DragEnter</code>-event does not get fired. I thought I could work around this problem by telling Windows "Hey, I'm, Dragging'n'Dropping here!", thus adding to my <code>GeneralMouseDown</code>-method:</p>
  
  <pre><code>this.DoDragDrop(this, DragDropEffects.Move);
  </code></pre>
  
  <p>This, on the one hand, gets the <code>DragEnter</code>-event to fire => Yeah! On the other hand is the moving-around-part only working after I release the mouse, causing the control to hang on the mousepointer forever => Anti-Yeah!</p>
  
  <p>Here's the question: Is there a way, to have both actions at the same time? So that I can move my control around, seing it at every position as I do now and fire the <code>DragEnter</code>-event when I get to that area of the other control?</p>