Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Bob Swart (aka Dr.Bob) - Medical Officer Delphi 6 Developer's Guide
 Dr.Bob's Tip-Of-The-Hat #4
See Also: Delphi Papers, Columns and other Tip-Of-The-Hats

Refresh a FileListBox
For this Tip-Of-The-Hat item, we'll examine a component from the old Win31 tab of Delphi and C++Builder (note: this tab won't be available in Kylix - or so we've been told). In fact, let's drop three components from this tab - the TDriveComboBox, TDirectoryListBox and TFileListBox - to create a nice drive/directory browser application. Set the DirList property of the DriveComboBox component to DirectoryListBox1, and the FileList property of the TDirectoryListBox to FileListBox1, so they're all connected to each other.

You can now use the three components to walk through your disks and directories. And if new files are added to (or removed from) a directory, the updated information will show when you get back to that directory.
However, if you want the directory to refresh itself automatically (say you just added a second form to your Delphi project, and you want to see the Unit2.pas and Unit2.dfm in the FileListBox), then you're out of luck - it won't do that. So you can try to add a button to the Form to "Refresh" the FileListBox using the following code:

  procedure TForm1.Button1Click(Sender: TObject);
  begin
    FileListBox1.Refresh
  end;
Unfortunately, this has no effect: the contents of the FileListBox won't be updated. It appears that in order to refresh the contents of a FileListBox, you must explicitly change the value of the Directory property and reset it to force a rescan of the directory. Usually, that could consist of the following two statements (for the second button):
  procedure TForm1.Button2Click(Sender: TObject);
  begin
    FileListBox1.Directory := '.';
    FileListBox1.Directory := DirectoryListBox.Directory // reset
  end;
Unfortunately (again), this won't work, since the first statement is ignored (we connected the FileListBox explicitly to the DirectoryListBox, so we can't change the FileListBox.Directory property manually anymore). We actually need to disconnect and later reconnect the DirectoryListBox from the FileListBox, as follows:
  procedure TForm1.Button2Click(Sender: TObject);
  begin
    DirectoryListBox1.FileList := nil;
    FileListBox1.Directory := '.';
    DirectoryListBox1.FileList := FileListBox1 // reset
  end;
This code will work and the second button (MyRefresh) will indeed refresh the contents of the FileListBox.
Using C++Builder, you would have to enter the following code to get the same effect:
  void __fastcall TForm1::Button2Click(TObject *Sender)
  {
    DirectoryListBox1->FileList = NULL;
    FileListBox1->Directory = ".";
    DirectoryListBox1->FileList = FileListBox1; // reset
  }

Epilogue
So, now that we've seen a few ways to refresh the contents of a TFileListBox component at run-time, what about design-time? When I drop a TFileListBox component on my form, it lists the files that are present at that moment in the current directory (usually the project directory). It seems, however, that there is no easy way to refresh this list at design-time - if for some reason you want it to refresh. I found a few ways to refresh the list at design-time:

  1. Hit Alt+F12 to view the form as text, and Alt+F12 again to view the form in design-time. The FileListBox will now be updated.
  2. Toggle the property ExtendedSelect from true to false (and back to the original setting again). A single toggle will refresh the contents of the FileListBox.
  3. Toggle any of the possible values for the FileType property (for example set ftArchive to true). Any toggle here will refresh the contents of the FileListBox.


    This webpage © 2001-2009 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.