Drag and Drop not working on shortcut or exefile - vb.net

Drag&Drop is working correctly when application is running. But when a file is dropped on application shortcut or exe.file, no drag&drop event is triggered just aaplication starts.
I created simple application in Visual Studio 2019, only Form1 with following adjustments
Form1.AllowDrop = True
Private Sub Form1_DragEnter(sender As Object, e As DragEventArgs) Handles MyBase.DragEnter
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub Form1_DragDrop(sender As Object, e As DragEventArgs) Handles MyBase.DragDrop
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
Me.Text = files(0)
End Sub
Can you help me how to open my application with correct filename which is dropped on application icon/shortcut?
Thanks, Martin

Your code only handles the drag and drop on the form itself (which can be done with any other control with AllowDrop = True). Dropping a file onto the application executable file (or shortcut) is a totally different thing; what it does is simply open the application normally but with a command-line argument passed to it (i.e., the file/folder path).
To retrieve that file/folder path, you can use Environment.GetCommandLineArgs, to read the command-line arguments, make sure it returns at least two elements (the first one is your application's executable path), and then display the second (or second to last) elements.
This should work:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim args = Environment.GetCommandLineArgs()
If args.Length > 1 Then Me.Text = args(1)
End Sub
If you drop multiple files onto your program's icon and you want to display them all, you can adjust the above code to something like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim args = Environment.GetCommandLineArgs()
For Each arg In args.Skip(1)
' Do something with `arg`.
Next
End Sub

Related

Visual Basic FolderBrowser returning no value

I was looking to memorizing a folder path to my.settings indefinitely.
I tried
Private Sub CartellaTessutiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CartellaTessutiToolStripMenuItem.Click
FolderBrowserDialog1.ShowDialog()
End Sub
Private Sub FolderBrowserDialog1_Disposed(sender As Object, e As EventArgs) Handles FolderBrowserDialog1.Disposed
My.Settings.CartellaTex = FolderBrowserDialog1.SelectedPath
My.Settings.Save()
MsgBox(My.Settings.CartellaTex.ToString)
End Sub
My.settings.CartellaTex setting is set to String.
I am sure no value is returned as by setting up a msg box at the end of the text such message does not show.
All the functions in my app that rely on My.settings.CartellaTex go in error.
I have read somewhere that FolderBrowser struggles when something called "threads" are involved, but this code ran perfectly yesterday night. Keep in mind I am using Visual Studio on Parallels for Mac but again this used to work hours ago. I am really new to vb.net and this is as much knowledge I have. Please be patient.
Try this:
Private Sub CartellaTessutiToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CartellaTessutiToolStripMenuItem.Click
If FolderBrowserDialog1.ShowDialog = DailogResult.OK Then
My.Settings.CartellaTex = FolderBrowserDialog1.SelectedPath
My.Settings.Save()
MsgBox(My.Settings.CartellaTex.ToString)
End If
End Sub

Control Properties of LinkLabel when used for HelpProvider

I am working with Visual Studio 2017, working in VB. I am linking to a .CHM file from a LinkLabel which works fine with following code:
Private Sub LinkLabel2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkLabel2.Click
' Create link to help file
System.Windows.Forms.Help.ShowHelp(Me, "RPM_Help.chm", HelpNavigator.AssociateIndex)
End Sub
I use a simple image to indicate the link to the .chm file that is 32x32 pixels in size, I have been playing with the properties of LinkLabel2 but I just can't figure out how to make the entire label a link space. I did find that unless there is a Text property on the label a MouseOver cursor will not show up, but how can I make the entire label image a cursor link?
As shown in the code and image below this can be solved by using LinkLabel2.AutoSize = False and adding a ToolTip from Common Controls to the form in design mode.
Each control, such as Buttons and TextBoxes, will acquire a ToolTip on toolTip1 property when you add a toolTip1 to your designer view. You can access this property in the Properties pane to set the tool tips.
Public Class frmMain
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LinkLabel2.BackColor = Color.CornflowerBlue
LinkLabel2.AutoSize = False
LinkLabel2.Width = 168
LinkLabel2.Height = 40
LinkLabel2.Text = ""
End Sub
Private Sub LinkLabel1_Click(sender As Object, e As EventArgs) Handles LinkLabel1.Click
' --- Open help file - Table of contents
System.Windows.Forms.Help.ShowHelp(Me, "hlp/CHM-example.chm", HelpNavigator.TableOfContents)
End Sub
Private Sub LinkLabel2_Click(sender As Object, e As EventArgs) Handles LinkLabel2.Click
' --- Open help file - Index
System.Windows.Forms.Help.ShowHelp(Me, "hlp/CHM-example.chm", HelpNavigator.Index)
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
' --- Open help file - Search
System.Windows.Forms.Help.ShowHelp(Me, "hlp/CHM-example.chm", HelpNavigator.Find, "")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
The code corresponds to the image shown. The values can of course also be set via the properties, but are included here in the FormLoad for documentation.
You may want to use a simple PictureBox1_Click event for your needs as shown below (third item in the "Show help" group box).

New form "is never disposed" message

I've recently moved over to Visual Studio 2019 V16.2 and it's now showing me a new "message" whenever I move between forms in my Windows Forms App.
IDE0067 Disposable object created by 'New FindFile' is never disposed
I've always moved to the "next" form in my project with code snippets like:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim frmFindFile As New FindFile
frmFindFile.Show()
Me.Close()
End Sub
What am I doing wrong? Should I be disposing of the form variable once the new one is showing? The below gets rid of the warning, but my second form never shows up!
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim frmFindFile As New FindFile
frmFindFile.Show()
Me.Close()
frmFindFile.Dispose()
End Sub
VB.NET has default instances of forms, so if you just use FindFile.Show() it will not give a warning.
For more information, please see the answers at Why is there a default instance of every form in VB.Net but not in C#?
I haven't seen the proper answer to this question yet, but there is one.
Original code as listed above is:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim frmFindFile As New FindFile
frmFindFile.Show()
Me.Close()
End Sub
Yet, the proper way to do this is:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Using frmFindFile As FindFile = New FindFile()
frmFindFile.Show()
End Using
Me.Close()
End Sub
This will automatically dispose of anything within the Using structure that requires disposal.
Pretty much anything that can be instantiated using the Dim/As class structure can be placed within a Using structure.
Another example would be using streamreader/writer. Instantiate it with the Using, then instead of using instance.Close(), just use End Using to close it out and dispose of it.

Copying multiple sources to single destination

I am new to VB.net and am trying to create a backup utility to copy a set of local folders to a single backup destination (on usb for example) with a progress bar. After googling for the last few hours I can only find examples of single folder to single destination.
Can anyone point me in the direction of an example?
UPDATE:
Based of #Werdna example, I have created a simple FOR EACH NEXT loop. However the next issue is that only the files within the source directories are being copied to the target directory, rather than the folders and all their contents. Can anyone see where I am going wrong?
Public Class Form1
Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
Dim destination = "E:\Backup Folder"
Dim sources As New List(Of String)
sources.Add("D:\Profiles\Users\Desktop")
sources.Add("D:\Profiles\Users\Mail")
sources.Add("D:\Profiles\Users\Downloads")
For Each source As String In sources
My.Computer.FileSystem.CopyDirectory(source, destination)
Next
MessageBox.Show("Copy Completed")
End Sub
End Class
Also, what is the best method to use the FOR EACH NEXT loop to count the number of files to be copied? I would like to output the amount to a label as well as use it for a progressbar as the utility evolves.
here is something that i have quickly written up for you to give you some idea on how i went about completing your task. You will need to add 2 listboxes to your application, a folder open dialog and a few buttons, this might not be what you are looking for, unfortunately you cant select multiable folders with the other dialogs, however nevertheless, take a look below, the code isn't completely as there is always something to do, however this should lead you in the right direction hopefully!
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In IO.Directory.GetDirectories("C:\")
ListBox1.Items.Add(item)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each item In ListBox1.SelectedItems
ListBox2.Items.Add(item & "\")
Next
ListBox1.SelectedItem = Nothing
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ListBox2.Items.Remove(ListBox2.SelectedItem)
ListBox2.SelectedItem = Nothing
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each item In IO.Directory.GetDirectories(ListBox1.SelectedItem)
ListBox1.Items.Clear()
ListBox1.Items.Add(item)
Next
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
My.Computer.FileSystem.CreateDirectory(FolderBrowserDialog1.SelectedPath)
For Each item In ListBox2.Items
My.Computer.FileSystem.CopyDirectory(item, FolderBrowserDialog1.SelectedPath)
Next
MessageBox.Show("Copy Completed.")
End If
End Sub
End Class
I wrote this in about 10 minutes, so i didnt get a change to do a progessbar, however if this is what you are looking for, then i am happy to help you add one to your program. Happy Coding!
UPDATE - BASED OFF YOUR NEW QUESTION
Public Class Form1
Dim NumberofFILEs As Integer = 0
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim Location1 = "C:\Backup Folder\TESTING\FOLDER"
Dim source As String = "C:\TESTING\FOLDER"
Dim source1 As String = "C:\TESTING1\FOLDER"
Dim Location2 = "C:\Backup Folder\TESTING1\FOLDER"
IO.Directory.CreateDirectory("C:\Backup Folder\TESTING\FOLDER")
IO.Directory.CreateDirectory("C:\Backup Folder\TESTING1\FOLDER")
For Each item In source
My.Computer.FileSystem.CopyDirectory(source, Location1, True)
Next
For Each item In source1
My.Computer.FileSystem.CopyDirectory(source1, Location2, True)
Next
MessageBox.Show("Completed")
For Each file In source1.Count & source.Count
NumberofFILEs += 1
Next
Label1.Text = NumberofFILEs
End Sub
End Class
Edit the locations and destinations to what you need. Also the True at the end of the copydirectory means that it will overwrite any files with the same name, eg it will update them pretty much
You can copy each folder to the destination, one at a time. Loop through the source folders and copy each one to the target folder.

Drag & drop and get file path in VB.NET

I'd like to be able to drag a file/executable/shortcut into a Windows Forms application and have the application determine the original path of the dropped file then return it as a string.
E.g. drag an image from the desktop into the application and messagebox up the local path of the image.
Is that possible? Could someone provide me with an example maybe?
It's quite easy. Just enable drap-and-drop by setting the AllowDrop property to True and handle the DragEnter and DragDrop events.
In the DragEnter event handler, you can check if the data is of the type you want using the DataFormats class.
In the DragDrop event handler, use the Data property of the DragEventArgs to receive the actual data and the GetData method
Example:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
End Sub
Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each path In files
MsgBox(path)
Next
End Sub
Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
This is just a note to state that if drag and drop does not work, it could be because you are running Visual Studio in Administrator Mode (Windows 7 and up I believe).
This also has to do with the UAC level currently set on your Windows installation.