Export (0) Print
Expand All

How to: Enable Drag-and-Drop Operations with the Windows Forms RichTextBox Control

Drag-and-drop operations with the Windows Forms RichTextBox control are done by handling the DragEnter and DragDrop events. Thus, drag-and-drop operations are extremely simple with the RichTextBox control.

To enable drag operations in a RichTextBox control

  1. Set the AllowDrop property of the RichTextBox control to true.

  2. Write code in the event handler of the DragEnter event. Use an if statement to ensure that the data being dragged is of an acceptable type (in this case, text). The DragEventArgs.Effect property can be set to any value of the DragDropEffects enumeration.

    private void richTextBox1_DragEnter(object sender, 
    System.Windows.Forms.DragEventArgs e)
    {
       if (e.Data.GetDataPresent(DataFormats.Text)) 
          e.Effect = DragDropEffects.Copy;
       else
          e.Effect = DragDropEffects.None;
    }
    

    (Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.

    this.richTextBox1.DragEnter += new
        System.Windows.Forms.DragEventHandler
        (this.richTextBox1_DragEnter);
    
  3. Write code to handle the DragDrop event. Use the DataObject.GetData method to retrieve the data being dragged.

    In the example below, the code sets the Text property of the RichTextBox control equal to the data being dragged. If there is already text in the RichTextBox control, the dragged text is inserted at the insertion point.

    private void richTextBox1_DragDrop(object sender, 
    System.Windows.Forms.DragEventArgs e)
    {
       int i;
       String s;
    
       // Get start position to drop the text.
       i = richTextBox1.SelectionStart;
       s = richTextBox1.Text.Substring(i);
       richTextBox1.Text = richTextBox1.Text.Substring(0,i);
    
       // Drop the text on to the RichTextBox.
       richTextBox1.Text = richTextBox1.Text + 
          e.Data.GetData(DataFormats.Text).ToString();
       richTextBox1.Text = richTextBox1.Text + s;
    }
    

    (Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.

    this.richTextBox1.DragDrop += new
        System.Windows.Forms.DragEventHandler
        (this.richTextBox1_DragDrop);
    

To test the drag-and-drop functionality in your application

  1. Save and build your application. While it is running, run WordPad.

    WordPad is a text editor installed by Windows that allows drag-and-drop operations. It is accessible by clicking the Start button, selecting Run, typing WordPad in the text box of the Run dialog box, and then clicking OK.

  2. Once WordPad is open, type a string of text in it. Using the mouse, select the text, and then drag the selected text over to the RichTextBox control in your Windows application.

    Notice that when you point the mouse at the RichTextBox control (and, consequently, raise the DragEnter event), the mouse pointer changes and you can drop the selected text into the RichTextBox control.

    When you release the mouse button, the selected text is dropped (that is, the DragDrop event is raised) and is inserted within the RichTextBox control.

Show:
© 2015 Microsoft