Solved

Drag and drop from external source

Posted on 2006-07-16
2
229 Views
Last Modified: 2010-04-23
Hi Experts,

I would like to be able to drag and drop a file onto my application from the windows explorer and open that file in a rich text box using the RichTextBox.loadfile command. On the drag drop event for my form i need to know the path for the dragged file. Also if the file is a shortcut i would like to grab the target path.

How can i achieve this?

Cheers,

Tom
0
Comment
Question by:TommyTwoPints
2 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 500 total points
ID: 17116964
Hello Tom,

  Here's a quick example that will enable the dragEnter and DragDrop events to work with RichTextBox, so you can drag any "RTF" file and drop it in your application

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
       RichTextBox1.AllowDrop = True
        AddHandler Me.RichTextBox1.DragEnter, New System.Windows.Forms.DragEventHandler(AddressOf Me.RichTextBox1_DragEnter)
        AddHandler Me.RichTextBox1.DragDrop, New System.Windows.Forms.DragEventHandler(AddressOf Me.RichTextBox1_DragDrop)
    End Sub

    Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If CType(e, DragEventArgs).Data.GetDataPresent(DataFormats.FileDrop) Then
            CType(e, DragEventArgs).Effect = DragDropEffects.All
        Else
            CType(e, DragEventArgs).Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
        Dim s As String() = e.Data.GetData(DataFormats.FileDrop)
        RichTextBox1.LoadFile(s(0))
    End Sub

HTH


Regards,
Mohammed Nasman
0
 
LVL 3

Author Comment

by:TommyTwoPints
ID: 17116990
Thats excellent, thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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