RichTextBox with AutoComplete and DragAndDrop

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
I'd like to add AutoComplete and DragAndDrop to a Richtextbox, but a RichTextBox lacks Autocomplete and AllowDrop properties. If I use a Textbox I loose the ability to use colours and fonts. Is there a third party Richtextbox control that I could use, or are there ways to add these properties when they're missing?

Thank you,
Robert
 
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
 
Thank you for this. I find that I can set this property statically, so that I don't need to set it in code and I can remove the statement "RichTextBox1.Allowdrop=true".

The definitions are: -
EnableAutoDragDrop: Gets or sets a value that enables drag-and-drop operations on text, pictures, and other data.
AllowDrop: Gets or sets a value indicating whether the control will enable drag-and-drop operations.

These seem to be saying the same thing in different words. Is there any actual difference?
 
EnableAutoDragDrop is an on/off switch to support DnD without further coding.
The regular DnD where you have to write code (AllowDrop etc) has been hidden from designer for RichTextBox class.
 
Back
Top