Microsoft KB Archive/814309

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

Article ID: 814309

Article Last Modified on 11/13/2007

-

APPLIES TO


 * 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

-





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.



SYMPTOMS
You try to drag a RichTextBox control with the code in the &quot;Enabling Drag-and-Drop Operations with the Windows Forms RichTextBox Control&quot; 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.



CAUSE
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.



RESOLUTION
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 event this.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; }



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 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. From the toolbox, drag a RichTextBox control to Form1.

RichTextBox1 is created on Form1. Right-click RichTextBox1, and then click Properties. In the Properties window, set the value for AllowDrop to True . Right-click Form1, and then click View Code.</li>  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 ; } </li> Try to drag text from WordPad to RichTextBox1.

You cannot drop the text.</li></ol>

<div class="references_section">