PRB: FileListBox Control May Not List File Names Properly
Article ID: 196028
Article Last Modified on 3/7/2005
APPLIES TO
- Microsoft Visual Basic 5.0 Learning Edition
- Microsoft Visual Basic 6.0 Learning Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Visual Basic 6.0 Enterprise Edition
This article was previously published under Q196028
SYMPTOMS
Under some conditions, file names may not appear or may be listed
improperly in the FileListBox control.
RESOLUTION
To work around this problem, you can use the Dir() function to obtain a
list of files in a directory and programmatically populate a Listbox with
the file names. When a user double-clicks a file name in the Listbox, you
may retrieve the name in the Listbox's dblClick event.
Steps to Resolve
- Open a new Standard EXE project in Visual Basic. Form1 is created by
default.
- Add a DriveListBox, DirListBox, and a ListBox to the form.
- Add the following code to the form's code window:
Option Explicit
Private Sub Drive1_Change()
' Inform the DirListBox of the Drive change.
Dir1.Path = Drive1.List(Drive1.ListIndex)
End Sub
Private Sub Dir1_Change()
' Obtain the file names from the new directory and populate
' the ListBox.
Dim strCurrentPath As String
Dim strFileName As String
If Right(Dir1.Path, 1) = "\" Then
strCurrentPath = Dir1.Path
Else
strCurrentPath = Dir1.Path & "\"
End If
' Clear the Listbox.
List1.Clear
' Populate the Listbox with the file names.
strFileName = Dir(strCurrentPath)
Do While strFileName <> ""
List1.AddItem strFileName
strFileName = Dir
Loop
End Sub
Private Sub List1_DblClick()
' Retrieve the text from the currently selected item.
MsgBox List1.List(List1.ListIndex)
End Sub
- Run the project. As you change drives and directories, the Listbox will
populate with the names of any files in the directory. Double-clicking
a file name in the Listbox will cause a message box to display the
chosen file name.
MORE INFORMATION
Problems with the FileListBox has been know to occur with shares on Netware
Servers and on NT Servers where the 8.3 filenames have been disabled.
REFERENCES
For additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
196141 PRB: Problems Using Intrinsic File System Controls
Visual Basic Help, search on: "Dir function"
Keywords: kblistbox kbprb kbctrl kbcode KB196028