Problem sorted. Contrary to the documentation, you CAN drag and drop to a RichTextBox. However there is no AllowDrop in the control's property window, you have to set AllowDrop=True in code, and also you should use DragDropEffects.Move, not .Copy: -
1. I set up an Experiment project with TextBox1 and RichTextbox1, plus TextBox2 with text to drag.
2. I put this in the form's New event: -
RichTextBox1.AllowDrop = True
3. I changed all of the references to DragDropEffects from DragDropEffects.Copy to DragDropEffects.Move.
(as an aside: I found that with
TextBox2.DoDragDrop(TextBox2.Text, DragDropEffects.Move)
in TextBox2_MouseMove but the references in TextBox1_DragEnter and RichTextBox1_DragEnter left unchanged as DragDropEffects.Copy the Drag&Drop worked to RichTextBox1 but NOT to TextBox1. I have no idea why)
Here is my complete Experiment code: -
Public Class Form1
Dim MouseIsDown As Boolean = False
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
RichTextBox1.AllowDrop = True
End Sub
Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Move
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
' Paste the text.
TextBox1.Text = e.Data.GetData(DataFormats.Text)
End Sub
Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Move
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop
' Paste the text.
RichTextBox1.Text = e.Data.GetData(DataFormats.Text)
End Sub
Private Sub TextBox2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseDown
RichTextBox1.AllowDrop = True
MouseIsDown = True
End Sub
Private Sub TextBox2_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseMove
If MouseIsDown Then
' Initiate dragging.
TextBox2.DoDragDrop(TextBox2.Text, DragDropEffects.Move)
End If
MouseIsDown = False
End Sub
End Class