Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/enable-drag-and-drop-operations-with-wf-richtextbox-control.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

7.2 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Enable Drag-and-Drop Operations with RichTextBox Control 03/30/2017
csharp
vb
cpp
examples [Windows Forms], text boxes
drag and drop [Windows Forms], richTextBox control
text boxes [Windows Forms], drag-and-drop operations
RichTextBox control [Windows Forms], drag-and-drop operations
ca167d1c-2014-4cf0-96a0-20598470be3b

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

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

To enable drag operations in a RichTextBox control

  1. Set the xref:System.Windows.Forms.RichTextBox.AllowDrop%2A property of the xref:System.Windows.Forms.RichTextBox control to true.

  2. Write code in the event handler of the xref:System.Windows.Forms.RichTextBox.DragEnter event. Use an if statement to ensure that the data being dragged is of an acceptable type (in this case, text). The xref:System.Windows.Forms.DragEventArgs.Effect%2A?displayProperty=nameWithType property can be set to any value of the xref:System.Windows.Forms.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(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 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->DragEnter += gcnew  
       System::Windows::Forms::DragEventHandler  
       (this, &Form1::richTextBox1_DragEnter);  
    
  3. Write code to handle the xref:System.Windows.Forms.RichTextBox.DragDrop event. Use the xref:System.Windows.Forms.DataObject.GetData%2A?displayProperty=nameWithType method to retrieve the data being dragged.

    In the example below, the code sets the xref:System.Windows.Forms.RichTextBox.Text%2A property of the xref:System.Windows.Forms.RichTextBox control equal to the data being dragged. If there is already text in the xref:System.Windows.Forms.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:  
       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 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->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 xref:System.Windows.Forms.RichTextBox control in your Windows application.

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

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

See also