title: Drag and Drop between Instances of the same Windows Forms Application
body: <p>I have created a small Windows Forms test application to try out some drag/drop code. The form consists of three PictureBoxes. My intention was to grab a picture from one PictureBox, display it as a custom cursor during the drag operation, then drop it on another PictureBox target.</p>
  
  <p>This works fine from one PictureBox to another <em>as long as they are on the same form</em>.</p>
  
  <p>If I open two instances of the same application and attempt to drag/drop between them, I get the following cryptic error:</p>
  
  <blockquote>
    <p>This remoting proxy has no channel
    sink which means either the server has
    no registered server channels that are
    listening, or this application has no
    suitable client channel to talk to the
    server.</p>
  </blockquote>
  
  <p>For some reason, however, it does work to drag/drop to Wordpad (but not MS Word or Paintbrush).</p>
  
  <p>The three PictureBoxes get their events hooked up like this:</p>
  
  <pre><code>foreach (Control pbx in this.Controls) {
      if (pbx is PictureBox) {
          pbx.AllowDrop = true;
          pbx.MouseDown    += new MouseEventHandler(pictureBox_MouseDown);
          pbx.GiveFeedback += new GiveFeedbackEventHandler(pictureBox_GiveFeedback);
          pbx.DragEnter    += new DragEventHandler(pictureBox_DragEnter);
          pbx.DragDrop     += new DragEventHandler(pictureBox_DragDrop);
      }
  }
  </code></pre>
  
  <p>Then there are the four events like this:</p>
  
  <pre><code>void pictureBox_MouseDown(object sender, MouseEventArgs e) {
      int width = (sender as PictureBox).Image.Width;
      int height = (sender as PictureBox).Image.Height;
  
      Bitmap bmp = new Bitmap(width, height);
      Graphics g = Graphics.FromImage(bmp);
      g.DrawImage((sender as PictureBox).Image, 0, 0, width, height);
      g.Dispose();
      cursorCreatedFromControlBitmap = CustomCursors.CreateFormCursor(bmp, transparencyType);
      bmp.Dispose();
  
      Cursor.Current = this.cursorCreatedFromControlBitmap;
  
      (sender as PictureBox).DoDragDrop((sender as PictureBox).Image, DragDropEffects.All);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>void pictureBox_GiveFeedback(object sender, GiveFeedbackEventArgs gfea) {
      gfea.UseDefaultCursors = false;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>void pictureBox_DragEnter(object sender, DragEventArgs dea) {
      if ((dea.KeyState &amp; 32) == 32) { // ALT is pressed
          dea.Effect = DragDropEffects.Link;
      }
      else if ((dea.KeyState &amp; 8) == 8) { // CTRL is pressed
          dea.Effect = DragDropEffects.Copy;
      }
      else if ((dea.KeyState &amp; 4) == 4) { // SHIFT is pressed
          dea.Effect = DragDropEffects.None;
      }
      else {
          dea.Effect = DragDropEffects.Move;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>void pictureBox_DragDrop(object sender, DragEventArgs dea) {
      if (((IDataObject)dea.Data).GetDataPresent(DataFormats.Bitmap))
          (sender as PictureBox).Image = (Image)((IDataObject)dea.Data).GetData(DataFormats.Bitmap);
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>