Adding File Names to a ListBox
I needed to develop a utility in VB which takes a few filenames stores them to a listbox and processes each of them on click of a button. The process here is not important as getting the names of the files to be processed.
The classic approach would be to use a CommonDialog control for getting each filename and add to the list. I have used this approach in a number of apps. Let’s start buiding with this approach first.
Create a new project in VB. Create a ListBox, a “Browse…” button and a “Process” button. Name then lstFileNames, cmdBrowse, and cmdProcess resp. Now add a CommonDialog control. By default it’s not available in the toolbox. So right-click the toolbox, click on “Components…”. Under “Controls Tab browse teh list to find “Microsoft Common Dialog Control 6.0″. Check it and press “OK”. An icon for CommonDialog control would be visible on the toolbox. Add the control on the form and name it dlgBrowse.

Open the code window and paste the below code.
Private Sub cmdBrowse_Click()
dlgBrowse.ShowOpen
AddToList dlgBrowse.FileName
End Sub
Private Sub AddToList(ByVal txtFileName As String)
Dim i As Integer
Dim blnFileAlreadyexists As Boolean
txtFileName = Trim(txtFileName)
If txtFileName <> "" Then
blnFileAlreadyexists = False
For i = 0 To lstFileNames.ListCount - 1
If Trim(lstFileNames.List(i)) = txtFileName Then
blnFileAlreadyexists = True
End If
Next
If Not blnFileAlreadyexists Then
lstFileNames.AddItem txtFileName
End If
End If
End Sub
On clicking the “Browse…” button ShowOpen method of CommonDialog Control is called which shows the File-Open Dialog Box. The FileName property of the CommonDialog hold the name of the file that was selected, which is sent to AddToList procedure.
The AddToList procedure takes the filename parameter, validates it for empty string, checks if the file was already selected by matching it with existing names and adds the filename (with path) to the listbox.
That’s all with the classical approach. I have not added any code for “Process” button, as I said, that’s not of much importance.
Let’s add support for another way to add files… What about making the list add filenames by dragging and dropping the files over it?
Just open the Properties Window for lstFileNames, find property “OLEDropMode” and change it to “1 - Manual” and add the code for OLEDragDrop Event as below.
Private Sub lstFileNames_OLEDragDrop(Data As DataObject, Effect As Long, _
Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim i As Integer
For i = 1 To Data.Files.Count
AddToList Data.Files(i)
Next
End Sub
While anything can be dragged & dropped on the listbox, we have assumed here and handled only filedrops which will populate the Files Property of the DataObject with FileCount and the FileNames, which will be passed to AddToList procedure for adding filenames to the list. Handling of other types of data is out of scope of this blog entry and would cause runtime error.
That’s all again… but…
This time I wanted to make use of another approach i.e. Select all the files, right click and click “Open” (the file extension being associated with my app), thus opening the app and all the file names getting populated in the listbox. This approach is not as straight-forward as the previous ones, and it’s what I aim at…
This blog entry is getting too large so let’s do it in the next one.

[...] Continued from Adding File Names to a ListBox [...]
Pingback by Adding File Names to a ListBox - 2 « Jalaj — December 13, 2006 @ 6:25 am
[...] Adding File Names to a ListBox [...]
Pingback by Just Looking Back « Jalaj — May 28, 2007 @ 6:24 am
[...] for me was still uncharted (leaving the copy/paste that many do). This post spanned three posts Adding File Names to a ListBox, Adding File Names to a ListBox - 2, Adding File Names to a ListBox - [...]
Pingback by The Blog Revisited - 1 « Jalaj — November 16, 2007 @ 4:49 am