title: Dropping text at mouse position in textbox
body: <p>Despite many similar questions I haven't been able to find the answer. What I would like to do is this: </p>
  
  <p>I have a text box with some text and a couple of pictureboxes with a picture each. If I perform a drag-and-drop from a picture to the textbox, some text should be inserted into the textbox at the position where my cursor is when I make the drop (i.e. at the point where the mouseup event occurs).</p>
  
  <p>The first part is fine:</p>
  
  <pre><code>private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
      // Create custom text ...
      pictureBox1.DoDragDrop("Some custom text", DragDropEffects.Copy);
  }
  
  private void textBox1_DragEnter(object sender, DragEventArgs e) {
      if (e.Data.GetDataPresent(DataFormats.Text))
          e.Effect = DragDropEffects.Copy;
      else
          e.Effect = DragDropEffects.None;
  }
  </code></pre>
  
  <p>My problem is how to define where to drop the text:</p>
  
  <pre><code>private void textBox1_DragDrop(object sender, DragEventArgs e) {
          textBox1.Text.Insert(CORRECT_POSITION, e.Data.GetData(DataFormats.Text).ToString());
      }
  </code></pre>
  
  <p>Any suggestions?</p>
  
  <p>Edit: I have tried to get the right position with GetCharIndexFromPosition(), but it doesn't seem to return the correct position. The following code does indeed return a character position, but I have no idea where it gets it from. It's clear that it doesn't represent the position of the cursor, though.</p>
  
  <pre><code>private void textBox1_DragDrop(object sender, DragEventArgs e) {
      TextBox textBox1 = (TextBox)sender;
      System.Drawing.Point position = new System.Drawing.Point(e.X, e.Y);
      int index = textBox1.GetCharIndexFromPosition(position);
      MessageBox.Show(index.ToString());
  }
  </code></pre>