0

I am updating code done in VB 4, where I have a RichTextBox. I need to be able to drag-and-drop an image from Windows Explorer into the RTB. Unfortunately, I am unable to get the drag-and-drop to work.

I've created a much more simple Windows Form program to try to resolve this, but have made no progress. I begin by setting AllowDrop to True.

Public Sub New()
    InitializeComponent()
    Me.DragAndDropTextBox.AllowDrop = True
End Sub

I then create handlers for the RTB. These are taken directly from MSDN.

Private Sub DragAndDropTextBox_DragEnter(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragEnter
    ' Check the format of the data being dropped.
    If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
        ' Display the copy cursor.
        e.Effect = DragDropEffects.Copy
    Else
        ' Display the no-drop cursor.
        e.Effect = DragDropEffects.None
    End If
End Sub

Private Sub DragAndDropTextBox_DragDrop(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop
    System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop
    Dim img As Image
    img = Image.FromFile(e.Data.GetData(DataFormats.FileDrop, False))
    Clipboard.SetImage(img)

    Me.DragAndDropTextBox.SelectionStart = 0
    Me.DragAndDropTextBox.Paste()
End Sub

When I grab an image in Explorer and drag it over my window, I get the circle with a slash. I have put breakpoints on the first line of each of the handlers, and they are never reached. I have looked at several pages, and they all seem to give the same process, so I must be missing something simple.

I am not worried right now about pasting the image into the text box; I know I need to work on that. I am only trying to capture the image, but the handler methods do not seem to be getting called.

UPDATE

After quite a bit of experimentation, I found that the actual issue is with my Visual Studio 2010, which I always run as administrator. When I run the program from an exe, the drag-and-drop works. When I try running from VS in debug, it does not. Has anyone experienced this before?

If anyone could shed some light on this, I would be very grateful.

Tim
  • 2,731
  • 9
  • 35
  • 72
  • 1
    Your code in DragEnter only sets it to accept for `DataFormats.Text`. Images are not Text – Ňɏssa Pøngjǣrdenlarp May 13 '15 at 14:05
  • 1
    When you drag and drop "image" from Windows Explorer in reality you drag and drop just a file containing image. So your drop source must support drops of files. And your must process dropped files and load images by yourself. – Denis Anisimov May 13 '15 at 14:17
  • This is a good point, and I should have caught that. I made the change in my code, but I am still having no luck. As I said, the breakpoint is not being reached so I assume the DragEnter and DragDrop methods are never actually being called. – Tim May 13 '15 at 14:25

2 Answers2

0

Try getting rid of the InitializeComponent() call in your Sub New function. When I did that, I was able to detect the DragEnter event. Here is the code I tested (I created a simple WinForm and put a RichTextBox on it called DragAndDropTextBox):

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DragAndDropTextBox.AllowDrop = True
End Sub

Private Sub DragAndDropTextBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragEnter

    Debug.Print("Entering text box region")

    ' Check the format of the data being dropped.
    If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
        ' Display the copy cursor.
        e.Effect = DragDropEffects.Copy
    Else
        ' Display the no-drop cursor.
        e.Effect = DragDropEffects.None
    End If

End Sub

Private Sub DragAndDropTextBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop

    Dim img As Image
    img = Image.FromFile(e.Data.GetData(DataFormats.FileDrop, False))
    Clipboard.SetImage(img)

    Me.DragAndDropTextBox.SelectionStart = 0
    Me.DragAndDropTextBox.Paste()
End Sub

End Class

The InitializeComponent() call should appear in your code (I believe) when you add your own custom controls to a form. Otherwise, I don't think you need to call it.

ChicagoMike
  • 618
  • 3
  • 11
  • I believe the InitializeComponent call sets up the UI, and needs to be called unless you manually create all controls. I did try to remove it just to be sure, and my RichTextBox could not be found. Thank you for the suggestion, though. – Tim May 15 '15 at 19:13
0

It turned out that the Drag-And-Drop was working when running the code from an exe, but not from within Visual Studio. More searching turned up this answer, which states that Drag-And-Drop does not work in Visual Studio when it is run as an Administrator. I ran it with normal permissions, and the code worked.

Community
  • 1
  • 1
Tim
  • 2,731
  • 9
  • 35
  • 72