Export (0) Print
Expand All

Enabling Drag-and-Drop Operations with the Windows Forms RichTextBox Control

Visual Studio .NET 2003

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.
    ' Visual Basic
    Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter
       If (e.Data.GetDataPresent(DataFormats.Text)) Then
          e.Effect = DragDropEffects.Copy
       Else
          e.Effect = DragDropEffects.None
       End If
    End Sub
    
    // C#
    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;
    }
    
    // C++
    private:
       System::Void richTextBox1_DragEnter(System::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 C++ Note   Be sure the necessary code to enable the event handler is present. In this case, it would be similar to the following:
    // C#
    this.richTextBox1.DragEnter += new
        System.Windows.Forms.DragEventHandler
        (this.richTextBox1_DragEnter);
    // C++
    this->richTextBox1->DragEnter += new
       System::Windows::Forms::DragEventHandler
       (this, richTextBox1_DragEnter);
    
  3. Write code to handle the DragDrop event. Use the GetData method to retrieve the data being dragged. For more information, see DataObject.Data Property.

    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.

    ' Visual Basic
    Private Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop
       Dim i As Int16 
       Dim s As String
    
       ' 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
    End Sub
    
    // C#
    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;
    }
    
    // C++
    private:
       System::Void richTextBox1_DragDrop(System::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.
       String *str = String::Concat(richTextBox1->Text, e->Data
       ->GetData(DataFormats->Text)->ToString()); 
       richTextBox1->Text = String::Concat(str, s);
       }
    
    Visual C# and C++ Note   Be sure the necessary code to enable the event handler is present. In this case, it would be similar to the following:
    // C#
    this.richTextBox1.DragDrop += new
        System.Windows.Forms.DragEventHandler
        (this.richTextBox1_DragDrop);
    // C++
    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.

See Also

Performing Drag-and-Drop Operations Between Applications | RichTextBox Class | RichTextBox Control (Windows Forms) | Controls You Can Use on Windows Forms

Show:
© 2014 Microsoft