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



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

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

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.

MORE INFORMATION

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.

REFERENCES

For more information, visit the following MSDN Web site:

Enabling Drag-and-Drop Operations with the Windows Forms RichTextBox Control
http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskperformingdragdropoperationswithwindowsformsrichtextboxcontrol.asp
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 sites:

RichTextBox.DragEnter Event
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsRichTextBoxClassDragEnterTopic.asp
RichTextBox.DragDrop Event
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsRichTextBoxClassDragDropTopic.asp

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbprb kbWindowsForms kbProperties kbEvent kbRichEdit kbDragDrop KB814309 kbAudDeveloper