drag and drop text to specific location in multi-line text box

Posted on 2007-03-29
Medium Priority
Last Modified: 2008-01-09
hi there,

i have a list box that contains several types of custom tags that i would like to be able to drag on drop into a multi-line text box and have the text insert at the location of the cursor. It would also be good if the cursor position could be updated as the user moved the mouse over the textbox.

does anyone know how this might be done?
Question by:RepriseMIS
  • 2
LVL 15

Expert Comment

ID: 18821395
This isn't done using C#.  Instead, you use JavaScript for this.  Here's a tutorial on how to do it:


You can also Google "JavaScript drag and drop" -- there are a lot of helpful tutorials and how-to pages out there.

Author Comment

ID: 18822564
i probably should've mentioned that this is a .NET Forms application, not a web page.

Accepted Solution

Rytmis earned 2000 total points
ID: 18828367
I found this interesting, so I thought I'd give it a stab.

This is done on .NET 2.0, so if you're on 1.1, you'll have to combine the partial classes.


namespace DragTestApp
      partial class MainForm
            /// <summary>
            /// Designer variable used to keep track of non-visual components.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
            /// <summary>
            /// Disposes resources used by the form.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
                  if (disposing) {
                        if (components != null) {
            /// <summary>
            /// This method is required for Windows Forms designer support.
            /// Do not change the method contents inside the source code editor. The Forms designer might
            /// not be able to load this method if it was changed manually.
            /// </summary>
            private void InitializeComponent()
                  this.textBox1 = new System.Windows.Forms.TextBox();
                  this.listBox1 = new System.Windows.Forms.ListBox();
                  // textBox1
                  this.textBox1.AllowDrop = true;
                  this.textBox1.Location = new System.Drawing.Point(150, 18);
                  this.textBox1.Multiline = true;
                  this.textBox1.Name = "textBox1";
                  this.textBox1.Size = new System.Drawing.Size(277, 370);
                  this.textBox1.TabIndex = 1;
                  this.textBox1.DragOver += new System.Windows.Forms.DragEventHandler(this.TextBox1DragOver);
                  this.textBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox1DragDrop);
                  // listBox1
                  this.listBox1.FormattingEnabled = true;
                  this.listBox1.Items.AddRange(new object[] {
                                                      "More text",
                  this.listBox1.Location = new System.Drawing.Point(33, 18);
                  this.listBox1.Name = "listBox1";
                  this.listBox1.Size = new System.Drawing.Size(90, 121);
                  this.listBox1.TabIndex = 2;
                  this.listBox1.QueryContinueDrag += new System.Windows.Forms.QueryContinueDragEventHandler(this.ListBox1QueryContinueDrag);
                  this.listBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ListBox1MouseMove);
                  this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ListBox1MouseDown);
                  this.listBox1.GiveFeedback += new System.Windows.Forms.GiveFeedbackEventHandler(this.ListBox1GiveFeedback);
                  // MainForm
                  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                  this.ClientSize = new System.Drawing.Size(601, 523);
                  this.Name = "MainForm";
                  this.Text = "S";
            private System.Windows.Forms.ListBox listBox1;
            private System.Windows.Forms.TextBox textBox1;

MainForm.cs: this is where the interesting stuff happens. Drag'n'drop is fairly straightforward, but getting the position for the caret involves some P/Invoke magic.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DragTestApp
      /// <summary>
      /// Description of MainForm.
      /// </summary>
      public partial class MainForm : Form
            private static extern Int32 SendMessage(IntPtr hwnd, Int32 wMsg, Int32 wParam, Int32 lParam);
            private const Int32 EM_CHARFROMPOS = 215;
            private static int GetCharFromPos(IntPtr hwnd, Point pos) {
                  DWORD param = new DWORD(pos.X, pos.Y);
                  return DWORD.MakeLong(SendMessage(hwnd, EM_CHARFROMPOS, 0, param.LongValue)).LoWord;
            private struct DWORD {
                  [FieldOffset(0)] public int LongValue;
                  [FieldOffset(0)] public short LoWord;
                  [FieldOffset(2)] public short HiWord;
                  public DWORD(int lowValue, int highValue) {
                        this.LongValue = 0;
                        this.LoWord = (short) lowValue;
                        this.HiWord = (short) highValue;
                  public static DWORD MakeLong(int longValue) {
                        DWORD result = new DWORD();
                        result.LongValue = longValue;
                        return result;
            public static void Main(string[] args)
                  Application.Run(new MainForm());
            private Rectangle dragThreshold = Rectangle.Empty;
            private int listDragIndex = ListBox.NoMatches;
            public MainForm()
                  // The InitializeComponent() call is required for Windows Forms designer support.
                  // TODO: Add constructor code after the InitializeComponent() call.
            void ListBox1GiveFeedback(object sender, GiveFeedbackEventArgs e)
            void ListBox1MouseDown(object sender, MouseEventArgs e)
                  int dragIndex = listBox1.IndexFromPoint(e.X, e.Y);
                  if (dragIndex != ListBox.NoMatches) {
                        dragThreshold = GetDragThreshold(e.X, e.Y);
                        listDragIndex = dragIndex;
                  else {
                        dragThreshold = Rectangle.Empty;
            private static Rectangle GetDragThreshold(int x, int y) {
                  return new Rectangle(new Point(x - (SystemInformation.DragSize.Width / 2),
                                                 y - (SystemInformation.DragSize.Height / 2)),
            void ListBox1MouseMove(object sender, MouseEventArgs e)
                  if (LeftMouseDown(e) && OverThreshold(e.X, e.Y)) {
                        DragDropEffects drag =
                              listBox1.DoDragDrop(listBox1.Items[listDragIndex], DragDropEffects.All);
                        if (drag == DragDropEffects.Copy) {
            private static bool LeftMouseDown(MouseEventArgs e) {
                  return (e.Button & MouseButtons.Left) == MouseButtons.Left;
            private bool OverThreshold(int x, int y) {
                  return dragThreshold != Rectangle.Empty
                        && !dragThreshold.Contains(x, y);
            void TextBox1DragOver(object sender, DragEventArgs e)
                  e.Effect = DragDropEffects.Copy;
                  Point p = textBox1.PointToClient(new Point(e.X, e.Y));
                  textBox1.SelectionLength = 0;
                  textBox1.SelectionStart = GetCharFromPos(textBox1.Handle, p);

            void TextBox1DragDrop(object sender, DragEventArgs e)
                  textBox1.Paste((string) e.Data.GetData(typeof(System.String)));

            void ListBox1QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
                  if (!this.DesktopBounds.Contains(GetOffsetPosition(Control.MousePosition)))
                        e.Action = DragAction.Cancel;
            private static Point GetOffsetPosition(Point p) {
                  Point screenOffset = SystemInformation.WorkingArea.Location;
                  return new Point(p.X - screenOffset.X, p.Y - screenOffset.Y);


I hacked this together based on a couple of different tutorials and some MSDN searching. It's not perfect, but it should get you started.



Expert Comment

ID: 18828371
Also, please ignore the couple of bits of leftover code I neglected to remove. :)

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question