vb.net example please

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

Could someone please provide an example of opening a file browser, selecting
several files and adding them to a lst box?

Thanks

Regards
 
John,
You don't need it when you have VB.net 2003 it is in the toolbox.
Just drag to your form
Cor
 
Hi John,

You're in luck! I did this answering another query so here it is on a
plate!!

It starts Explorer as the file browser with a given file path. Then the
ListBox waits for files to be dropped onto it. The routine that actually puts
the files into the ListBox is separate in case you want to allow other
Controls to accept the files but divert them to the ListBox.

Regards,
Fergus

<code>
'===================================================================
'If the FilePath is bad, Explorer will show a dialogue box.
'The Exception Catch below is for other errors.
Public Sub CallExplorer (sFilePath As String)
Try
Process.Start ("Explorer.exe", "/e, " & sFilePath)
Catch e As Exception
MsgBox ("Couldn't start Explorer in " & sFilePath & vbCrLf &
e.Message)
'or
Throw New Exception ("Couldn't start Explorer in " & sFilePath, e)
End Try
End Sub

'===================================================================
Private Sub ListBox1_DragEnter(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListBox1.DragEnter

'Only allow drag-n-drop if it's from Explorer.
If e.Data.GetDataPresent (DataFormats.FileDrop) Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.None
End If
End Sub

'===================================================================
Private Sub ListBox1_DragDrop(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) _
Handles ListBox1.DragDrop

'Only allow graphics files to be accepted.
'Note the '.' at each end.
Dim sAllowedExtensions As String = ".jpg.bmp.gif."

'Put the list of files that have been dropped into a ListBox.
Dim asFiles() As String
asFiles = CType (e.Data.GetData ("FileDrop"), String())
DisplayFiles (asFiles, ListBox1, sAllowedExtensions)

End Sub

'===================================================================
'Files must match one of the given extensions.
'Files without an extension will never be matched.
Sub DisplayFiles (asFiles() As String, oListBox As ListBox, _
sAllowedExtensions As String)

oListBox.Items.Clear

Dim sFileName As String
For Each sFileName In asFiles
'Extract the extension.
Dim PosOfExt As Integer = ("." & sFileName).LastIndexOf (".")
Dim sExt As String = sFileName.Substring (PosOfExt)

'Only allow files with the correct extension.
If sAllowedExtensions.IndexOf ("." & sExt & ".") >= 0 Then
oListBox.Items.Add (sFileName)
End If
Next
End Sub
</code>
 
Back
Top