Go4Expert (http://www.go4expert.com/)
-   Visual Basic ( VB ) (http://www.go4expert.com/forums/visual-basic/)
-   -   Hot to Drag OUT of a form in vb.net [HERLP] (http://www.go4expert.com/forums/hot-drag-form-vbnet-herlp-t23010/)

ManzZup 14Aug2010 11:10

Hot to Drag OUT of a form in vb.net [HERLP]

i'm having this problem in ma head for months till i decided to ask it here (of course i digg every search engine).
The question is simple:

I'm making an app like winzip (an extractor) and i successfully coded dragging a file in and opening. But i now need to drag the files out, like in winzip in which i can drag and drop a file on to any folder directly. But how to implement that into my program?

Please HELP!


ManzZup 22Aug2010 17:39

Re: Hot to Drag OUT of a form in vb.net [HERLP]
i figured it out (cheating really)
and i'll post it here n submit as a tut for future use :D

First i muz tell tat drag out thing can be easily done using the doDragDrop func() but the problem is it always have a custom event. Like just to copy a file which is already EXIST, or move like so.

But most of the time the need is to dodragdrop for a virtual file.
ex: in ma project which is a decompressioner i wanted to get the target path and then decompress the file to that path. Because there's no possible way of to create the file before dragging off!
So i cnt use the normal func as:
(1) As i cannot provide my own codes for that or get the target path
(2) I cannot delay the process to decompress the file

So i found a CHEATING method.
[The original idea is not mine, i saw da idea and just coded it suitably :D )


The idea behind is to add some filesystemwatchers to Watch weather a SPECIFIC file to created and WHERE.
So the theory is i'm going to make a 'fake' file drop (just tel the user and the system that i dropped a file) but instead of dropping the real file i use a predifined file. (with a remarkable ext.)
So when the file is created the fileSystemWatcher FIRES! and tell us the target dir! :D

Lets start:
First we need to setup filesystem wacthers and the form load:


Public Sub setupFSMs()
        ' This is the main sub that prepares the filesystemwatchers.
        ' DONOT edit the code.
        ' Replace .antf2 with a unique ext.
        Dim Drives() As IO.DriveInfo = IO.DriveInfo.GetDrives
        For i As Integer = 0 To Drives.Length - 1
            If Drives(i).DriveType = IO.DriveType.Fixed Then
                If Drives(i).IsReady Then
                    Dim FSM As IO.FileSystemWatcher = New IO.FileSystemWatcher(Drives(i).RootDirectory.FullName, "*.antf2")
                    AddHandler FSM.Created, AddressOf OnChanged
                    FSM.IncludeSubdirectories = True
                    FSM.EnableRaisingEvents = True
                End If
            End If
    End Sub

private sub form_load(blah)
end sub

Above initiate the fileSystemWatchers. Note the ext. i have used. its "antf2" which i felt like an uncommon one :D
remeber to use your own one as i dont want ma prog go messing around :D

Now the code to say What To Do when we find a drop of that file.


Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs)
        ' Specify what is done when a file is created.
        ' NOTE this runs on a seperate thread!
            output = IO.Path.GetDirectoryName(e.FullPath)
My.Computer.FileSystem.DeleteFile(output.Trim(quote) & "\tmp_donot_delete.antf2") 'This is to prevent the file being there too much.
          'Add your code here
    End Sub

Add the actionyou need in 'Add your code here part
PLEASE note that this action runs on a seperate thread so be creaful not to add actions with cross-thread references.

Now to add the real part, where the file is dragged out
Here i have used a lstView and i coded in the itemDrag.


If lstMain.SelectedItems.Count > 0 Then
            Dim fileas As String() = New [String](0) {}

            fileas(0) = My.Application.Info.DirectoryPath & "\tmp_donot_delete.antf2"

            dta = New DataObject(DataFormats.FileDrop, fileas)
            dta.SetData(DataFormats.StringFormat, fileas)
            DoDragDrop(dta, DragDropEffects.Copy)

        End If

See what happened?
I just copied the file "tmp_donot_delete.antf2" from the ma prog dir to the target dir.
(NOTE: tmp_donot_delete.antf2 is the file name i've given. You'll need to have this file created evrythime you run your prog. Of corse thats simple and you can add hidden + system attribs to make sure this file is hidden)

So that way, when user drag out the file, tmp_donot_delete.antf2 will copy to the target dir and the FileSystemWatcher will catch it then get the target dir in the output variable. So you can use it on your wish!

I'm sorry for the bad gramer n all mistakes. Please feel free to ask on any matter :D
[Credits goes for the one who really thought of this idea]

Final Notes: This method is not 100% good, but easy to use. If you really need it in the professional way you'll have to extend your searches on shellExts

All times are GMT +5.5. The time now is 22:44.