2 Questions: Drag and Drop File Path AND SQL Server Update Database  
Author Message
xRuntime





PostPosted: Visual Basic Language, 2 Questions: Drag and Drop File Path AND SQL Server Update Database Top

1)What code would I use to get the file path of a file that is dragged and dropped into a form

2) I have a certain table represented in an application as a listbox. On the form opening, the listbox is filled with the contents of the table. How can I have it that the table is saved with the listbox items at the form closing (what code , i'll place it on Form_Closing)





Visual Basic20  
 
 
scottwis_MS





PostPosted: Visual Basic Language, 2 Questions: Drag and Drop File Path AND SQL Server Update Database Top

The following code should do the trick for you.

public Class Form1

Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
Dim formats As ICollection(Of String) = e.Data.GetFormats(True)

If (formats.Contains("FileDrop")) Then
Dim files() As String = CType(e.Data.GetData("FileDrop", True), String())
End If

End Sub

Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
e.Effect = DragDropEffects.All
End Sub

End Class

Inside For1m_DragDrop the files() array contains the names of all the files that were dropped on the form in the current drop operation.

You can see an example of how to update data in a database by looking at the VB 2005 101 Code Samples, which you can access here: http://msdn.microsoft.com/vbasic/downloads/code/101samples/

-Scott Wisniewski


 
 
Geek Squad





PostPosted: Visual Basic Language, 2 Questions: Drag and Drop File Path AND SQL Server Update Database Top

Thanks. I doubt that the user would drag multiple files (the program isnt intended to work like that), only one file, so the file path would be accessed by files(0)



 
 
scottwis_MS





PostPosted: Visual Basic Language, 2 Questions: Drag and Drop File Path AND SQL Server Update Database Top

Yes.

You may also want to modify the Drag_Enter event handler to check the data (similar to the code in Drag_Drop), and not set the effect value if the length of the array is not 1. That way you will only see a drag_drop event if they drop 1 file, and if they try to drag multiple files they will get a visual indication that they can't do this.

-Scott