--- title: Enable Drag-and-Drop Operations with RichTextBox Control ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" - "cpp" helpviewer_keywords: - "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" ms.assetid: 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 control are done by handling the and events. Thus, drag-and-drop operations are extremely simple with the control. ### To enable drag operations in a RichTextBox control 1. Set the property of the control to `true`. 2. Write code in the event handler of the event. Use an `if` statement to ensure that the data being dragged is of an acceptable type (in this case, text). The property can be set to any value of the enumeration. ```vb 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 ``` ```csharp 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; } ``` ```cpp 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. ```csharp this.richTextBox1.DragEnter += new System.Windows.Forms.DragEventHandler (this.richTextBox1_DragEnter); ``` ```cpp this->richTextBox1->DragEnter += gcnew System::Windows::Forms::DragEventHandler (this, &Form1::richTextBox1_DragEnter); ``` 3. Write code to handle the event. Use the method to retrieve the data being dragged. In the example below, the code sets the property of the control equal to the data being dragged. If there is already text in the control, the dragged text is inserted at the insertion point. ```vb 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 ``` ```csharp 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; } ``` ```cpp 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. ```csharp this.richTextBox1.DragDrop += new System.Windows.Forms.DragEventHandler (this.richTextBox1_DragDrop); ``` ```cpp 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 control in your Windows application. Notice that when you point the mouse at the control (and, consequently, raise the event), the mouse pointer changes and you can drop the selected text into the control. When you release the mouse button, the selected text is dropped (that is, the event is raised) and is inserted within the control. ## See also - - [How to: Perform Drag-and-Drop Operations Between Applications](../advanced/how-to-perform-drag-and-drop-operations-between-applications.md) - [RichTextBox Control](richtextbox-control-windows-forms.md) - [Controls to Use on Windows Forms](controls-to-use-on-windows-forms.md)