Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1356
  • Last Modified:

VB.net drag and drop into RichTextBox

Hi. I was given code to drag and drop from a ListBox into a TextBox asnd it worked well.
I also need to be able to drop into a RichTextBox. The following code doesn't achieve that.
I set AllowDrop = True in the form load event.


    Private Sub ListBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
        ' Drag and Drop STEP 1
        If e.Button = Windows.Forms.MouseButtons.Left AndAlso Me.ListBox1.SelectedItem IsNot Nothing Then
            Me.ListBox1.DoDragDrop(Me.ListBox1.SelectedItem.ToString(), DragDropEffects.Copy)
        End If

    End Sub

    Private Sub RichTextBox1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter
        ' Drag and Drop STEP 2
        If e.Data.GetDataPresent(System.Windows.Forms.DataFormats.StringFormat) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub


    Private Sub RichTextBox1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop
        ' Drag and Drop STEP 3
        If e.Data.GetDataPresent(System.Windows.Forms.DataFormats.StringFormat) Then
            Me.RichTextBox1.Text = e.Data.GetData(System.Windows.Forms.DataFormats.StringFormat).ToString()
        End If
    End Sub
0
Murray Brown
Asked:
Murray Brown
1 Solution
 
käµfm³d 👽Commented:
DragEnter doesn't seem to function in the same way for RTB as it does for TB. I switched to DragOver, and it appears to work.

Private Sub RichTextBox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragOver
    ' Drag and Drop STEP 2
    If e.Data.GetDataPresent(System.Windows.Forms.DataFormats.StringFormat) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub

Open in new window

0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now