You are currently offline, waiting for your internet to reconnect

You cannot drop text in a RichTextBox control by using a code sample from MSDN documentation

This article has been archived. It is offered "as is" and will no longer be updated.
Beta Information
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location from which you downloaded the release.
You try to drag a RichTextBox control with the code in the "Enabling Drag-and-Drop Operations with the Windows Forms RichTextBox Control" MSDN document. The change may occur in the pointer when you drag the text from WordPad. However, you cannot drop the text from the RichTextBox.
This problem occurs because there is no code for the Drop event in MSDN documentation. Therefore, you cannot drop the text that you select from WordPad in the RichTextBox.
To work around this problem, add a DragDrop event for RichTextBox1. To do this, use the following instructions.

Visual Basic .NET or Visual Basic 2005

Add the following code in the RichTextBox1 DragDrop event:
   Dim i As Int16    Dim s As String   ' Get Start Position For the for Dropping 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# .NET or Visual C# 2005

Add the following code to the Form1 class constructor after the InitializeComponent() statement:
// Handler for DragDrop eventthis.richTextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.rtb_dragdrop);
Add the following procedure to the code after the Main() method:
private void rtb_dragdrop(object sender,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 the RichTextBox   richTextBox1.Text = richTextBox1.Text + e.Data.GetData(DataFormats.Text).ToString();   richTextBox1.Text = richTextBox1.Text + s;}
This behavior is by design.

Steps to Reproduce the Behavior

  1. In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, create a new Windows application project by using Visual Basic .NET, Visual Basic 2005, Visual C# .NET, or Visual C# 2005.

    By default, Form1 is created.
  2. From the toolbox, drag a RichTextBox control to Form1.

    RichTextBox1 is created on Form1.
  3. Right-click RichTextBox1, and then click Properties.
  4. In the Properties window, set the value for AllowDrop to True.
  5. Right-click Form1, and then click View Code.
  6. Add the following code in the RichTextBox1 DragEnter event:

    Visual Basic .NET or Visual Basic 2005 Code
       If (e.Data.GetDataPresent(DataFormats.Text)) Then      e.Effect = DragDropEffects.Copy   Else      e.Effect = DragDropEffects.None   End If
    Visual C# .NET or Visual C# 2005 Code
    private void rtb_dragenter(object sender,DragEventArgs e){   if (e.Data.GetDataPresent(DataFormats.Text))       e.Effect = DragDropEffects.Copy ;   else      e.Effect = DragDropEffects.None ;}
  7. Try to drag text from WordPad to RichTextBox1.

    You cannot drop the text.
For more information, visit the following MSDN Web site:

Enabling Drag-and-Drop Operations with the Windows Forms RichTextBox Control
For more information about the RichTextBox.DragEnter event and the RichTextBox.DragDrop event, see your .NET Framework SDK documentation or visit the following MSDN Web site:

RichTextBox.DragEnter Event

Article ID: 814309 - Last Review: 02/27/2014 21:16:57 - Revision: 2.9

Microsoft Visual Basic 2005, Microsoft Visual Basic .NET 2003 Standard Edition, Microsoft Visual Basic .NET 2002 Standard Edition, Microsoft Visual C# 2005 Express Edition, Microsoft Visual C# .NET 2003 Standard Edition, Microsoft Visual C# .NET 2002 Standard Edition

  • kbnosurvey kbarchive kbvs2005swept kbvs2005applies kbprb kbwindowsforms kbproperties kbevent kbrichedit kbdragdrop KB814309