Drag and Drop Files Into Toolstrip

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
1 post Page 1 of 1
Contributors
User avatar
anthonygz
Member
Member
Posts: 48
Joined: Sun Feb 13, 2011 7:25 pm

Function:
Dragging in files from an outside source into a tooltip, filling in the name / icon / image (if you get creative) of the dropped file.


What you'll need:
1 Toolstrip


First things First:

You'll want to place the following before the first Sub (ex. Public Class Form1):
Code: Select all
Imports System
Imports System.IO
Next, Place this after under the first Sub, Or in my case Public Class Form1
Code: Select all
Friend Sub ModifyEnabledControls(ByVal Toolstrip1 As ToolStrip, ByVal sType As String)
This will make us able to manipulate the toolstrip a bit better, and lighten down the restrictions.

This next bit of code goes under the toolstrip's DragDrop function.
It's a long bit of code, but I'll explain what It does anyway.
Code: Select all
Dim files() As String = e.Data.GetData(DataFormats.FileDrop) ' Getting the information from a file dropped into the toolstrip or whatever source Its dropped into
        For Each path In files ' For each file dragged In, the path splits. Doesn't work well for multiple files, but works perfectly for one at a time.
            Dim NewToolStripButton As New ToolStripButton(path, Nothing, Nothing, path) ' Creating a new ToolStrip Button to place Into the Toolstrip
            ToolStrip1.Items.Add(NewToolStripButton) ' Adding In our new Toolstrip Button
            Dim WithoutName As String = "" 'Now's the fun part, breaking up the path to use later on.
            WithoutName = System.IO.Path.GetExtension(path) 'Getting the extension, this can be used later on for doing certain things If a certain file extension is dragged In.
            Dim JustName As String = "" 'Another File name split method
            JustName = System.IO.Path.GetFileNameWithoutExtension(path) 'Path without the Extension, This'll be used later on to get the File's Name
            NewToolStripButton.Text = JustName
            NewToolStripButton.Tag = path 'When clicked, you can refer to this (as shown later) to use process.start and open the path connected to the clicked button.
        Next
For Extracting Images from an .exe file, search up Vswe's tutorial.
You can then place the imagelocation for the button to the extracted Image's location.

The next piece of code will go under the ToolStrip1's DragEnter function
Code: Select all
 If e.Data.GetDataPresent(DataFormats.FileDrop) Then ' If a file is dragged In then..
            e.Effect = DragDropEffects.Copy ' Copy the data into the seperator's stated In the DragDrop function
        End If
Now the Code to make the path open for the Item we've added
Code: Select all
     On Error Resume Next ' For reasons, It'll always come out an error even if you do Try and On error goto whatever Is complete bogus, NEVER use It!
        Process.Start(e.ClickedItem.Tag) 'Opening the Process attached to the button
That should just be about It to get you started.

Q/A
Q) Is it possible to Save items and load them Later on?
A) Yes
1 post Page 1 of 1
Return to “Tutorials”