Solved

VB.net drag and drop into RichTextBox

Posted on 2012-04-09
2
1,261 Views
Last Modified: 2012-04-09
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
Comment
Question by:murbro
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37823069
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
 

Author Closing Comment

by:murbro
ID: 37823085
Thanks very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now