Partager via


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 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
    
    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;
    }
    
    private void richTextBox1_DragEnter(Object sender,
    System.Windows.Forms.DragEventArgs e)
    {
       if (e.get_Data().GetDataPresent(DataFormats.Text))
          e.set_Effect(DragDropEffects.Copy);
       else
          e.set_Effect(DragDropEffects.None);
    }
    
    private:
       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#, Visual J#, 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);
    
    this.richTextBox1.add_DragEnter(new
       System.Windows.Forms.DragEventHandler(
          this.richTextBox1_DragEnter));
    
    this->richTextBox1->DragEnter += gcnew
       System::Windows::Forms::DragEventHandler
       (this, &Form1::richTextBox1_DragEnter);
    
  3. Write code to handle the DragDrop event. Use the System.Windows.Forms.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 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
    
    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;
    }
    
    private void richTextBox1_DragDrop(Object sender,
    System.Windows.Forms.DragEventArgs e)
    {
       int i;
       String s;
    
       // Get start position to drop the text.
       i = richTextBox1.get_SelectionStart();
       s = richTextBox1.get_Text().Substring(i);
       richTextBox1.set_Text(richTextBox1.get_Text().Substring(0, i));
    
       // Drop the text on to the RichTextBox.
       richTextBox1.set_Text(richTextBox1.get_Text() +
          e.get_Data().GetData(DataFormats.Text).ToString());
       richTextBox1.set_Text(richTextBox1.get_Text() + s);
    }
    
    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#, Visual J#, 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);
    
    this.richTextBox1.add_DragDrop(new System.Windows.Forms.DragEventHandler(this.richTextBox1_DragDrop));
    
    this->richTextBox1->DragDrop += gcnew 
       System::Windows::Forms::DragEventHandler
       (this, &Form1::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

Tasks

How to: Perform Drag-and-Drop Operations Between Applications

Reference

RichTextBox Class

Other Resources

RichTextBox Control (Windows Forms)
Controls to Use on Windows Forms