Yes, it's true, the VB file controls are outdated, weak, finicky, blah, blah, blah. They also work fairly well, when you just need a simple pick list. The problem has always been, they're very tedious to set up properly. This project solves that, by providing a drop-in ready UserControl module for use in either VB5 or VB6. The UserControl contains a DriveListBox, a DirListBix and an ordinary ListBox, stacked vertically:
The image control (thin dashed rectangle) you see near the bottom is used at runtime as a sliding window splitter, to adjust the size of the DirListBox and ListBox controls.You might notice the two radio buttons on the test form? Those toggle the control's Style property, which determines whether the DriveList is shown on top or bottom. In all cases, the DirListBox is in the middle.
Other properties of interest include:
- BackColor: sets/returns the color used for the bars between constituent controls.
- Drive: sets/returns currently selected drive.
- FileCount: returns the number of files found in current Path that match Pattern.
- FileName: returns name of currently selected file.
- Files: psuedo-array of all the files in the currently selected folder.
- FullFileName: returns full name, including path, of currently selected file.
- Path: sets/returns currently selected folder.
- Pattern: sets/returns the (optionally multipart) file specification.
- Spacing: sets/returns the number of pixels between constituent controls.
Note: There are two string functions, Replace and Split, coded up for use in VB5 which really ought to be commented out if used in VB6! The demo, as packaged, has them so commented just to be safe. If you try running the demo and an error is generated in one of these two places, you are running VB5 and need to uncomment these two functions. They may be found at the bottom of the CTL file.
Public Property Let Pattern( _ ByVal NewValue As String) ' Assign delimited list to child control, ' replacing all commas with semi-colons, ' and eliminating spaces. m_Pattern = Replace$( _ Replace$(NewValue, " ", ""), ",", ";") Call UpdateFileList PropertyChanged "Pattern" End Property Private Sub UpdateFileList() Dim Specs As Variant Dim FileName As String Dim FilePath As String Dim Attr As Long Dim i As Long ' Clear current settings. File1.Clear m_FullFileName = "" ' Parse out current pattern specifications. Specs = Split(m_Pattern, ";") FilePath = Me.Path ' Find all matching files and add to list. For i = LBound(Specs) To UBound(Specs) FileName = Dir(FilePath & Specs(i)) Do While Len(FileName) ' Make sure this isn't a directory. Attr = GetAttr(FilePath & FileName) If (Attr And vbDirectory) = 0 Then File1.AddItem FileName End If FileName = Dir() Loop Next i End Sub
That native ListBox control was chosen over the FileListBox for several reasons. Principle among them being its support for the IntegralHeight property. Setting that to False allows us to far more cleanly adjust the vertical dimensions of the overall UserControl. Of course, this design means that we need to replicate the Pattern property (basic idea shown above) the FileListBox brings to the game. The side-benefit of this design is that we are free to inject any other criteria we wish into the UpdateFileList routine. Perhaps you only want to show files of a certain size or age? This is where'd you do that!
This control isn't necessarily meant for industrial usage, but I think you'll find it really useful in all those quick knock-offs you no doubt toss together "just to get the job done!" Enjoy.
This sample hasn't been published anywhere except here on this website, but first rights to such publication are jealously guarded - you have been warned. <g>
No APIs were harmed in the making of this sample.
Please, enjoy and learn from this sample. Include its code within your own projects, if you wish. But, in order to insure only the most recent code is available to all, I ask that you don't share the sample by any form of mass distribution.
Download FileControls.zip, 7Kb, Last Updated: Tuesday, July 11, 2006
The following resources may also be of interest: