Home

VCL Controls: The File List Box

   

Introduction

To assist you with visually getting a list of files inside a directory, the VCL provides an object named the file list box. To get it, in the Win 3.1 section of the Toolbox, click the TFileListBox control Directory List Box and click the form (or other container). In the VCL, this control is represented by a class of the same name.

Practical LearningPractical Learning: Introducing the Directory List Box

  1. Start Embarcadero RAD Studio
  2. To create a new project, on the main menu, click File -> New -> VCL Forms Application - C++Builder
  3. Change the Caption of the form to File Viewer
  4. On the main menu, click File -> New -> Other
  5. On the main menu, click File -> New -> Other
  6. In the right list of the New Items dialog box, double-click Frame
  7. Notice the name on the tab of the new frame
  8. In the top section of the Code Edirtor, click Unit1.cpp
  9. In the Standard section of the Tool Palette, click Frame and click the form
  10. In the Select Frame to Insert dialog box, select the name of the new frame (it should be the last in the list) and click OK
  11. In the top section of the Code Editor, click the UnitX.cpp of the last frame
  12. In the Tool Palette, click Win 3.1
  13. Click the TDriveComboBox button Drive Combo Box and click inside the frame
  14. Press F9 to execute
  15. Click the down-point button of the drive combo box
  16. Close the form and return to your programming environment
  17. In the Win 3.1 section of the Tool Palette, click the TDirectoryListBox control Directory List Box and click inside the frame
  18. Adjust the design as follows:
     
    File Viewer
  19. On the Tool Palette, click Standard
  20. Click the TLabel control Directory List Box and click inside the frame under the directory list box
     
    File Viewer
  21. In the frame, click the directory list box to select it
  22. In the Object Inspector, click DirLabel and select Label1
  23. Press F9 to execute
  24. Change a drive in the combo box
  25. Close the form and return to your programming environment
  26. In the frame, click the drive combo box to select it
  27. In the Object Inspector, click DirList and select DirectoryListBox1
  28. Press F9 to execute
  29. Change a drive in the combo box
  30. Close the form and return to your programming environment

 

Practical LearningPractical Learning: Adding a File List Box

  1. In the Tool Palette, cllick Win 3.1
  2. In the Win 3.1 section, click the TFileListBox control Directory List Box and click inside the frame
  3. Adjust the design as follows:
     
    File Viewer

Using a File List Box

A file list box is used to show a list of files:

File List Box

Normally, before uing it, a user should first select a directory, which is not possible in the file list box. To make this possible, you should add a directory list box and possibly a drive combo box:

File List Box

After adding a directory list box, you should assign the file list box to it to allow the file list box to show the files of the currently selected directory. To support this, the TDirectoryListBox class is equipped with a property named FileList. This property is of type TFileListBox:

__property Filectrl::TFileListBox * FileList =
  		{read=FFileList,write=SetFileListBox};

To visually assign the file list box, access the Object Inspector for the directory list box. In the FileList field, select the name of the file list box.

Practical LearningPractical Learning: Adding a Directory List Box

  1. In the frame, click the directory list box to select it
  2. In the Object Inspector, click FileList and select FileListBox1
  3. Press F9 to execute
  4. Double-click a folder in the directory list box
  5. Close the form and return to your programming environment

The Drive of a File List Box

Before accessing a file, the user should first select the apppropriate drive in the drive combo box. The drive that is currently selected is identified by the Drive property of the TFileListBox class:

__property wchar_t Drive = {read=GetDrive,write=SetDrive};

This property behaves as its counterpart of the directory list box.

To use a directory list box, the user can click a directory to select it. When this is happens, the control fires an OnChange event. The file that is currently selected is represented by the FileName prpoperty:

__property System::UnicodeString FileName =
 		   {read=GetFilePath,write=ApplyFilePath};

To get the complete path and the name of the file that is currently selected, get the value of this property;

//---------------------------------------------------------------------------
void __fastcall TForm1::FileListBox1Change(TObject *Sender)
{
    ShowMessage(FileListBox1->FileName);
}
//---------------------------------------------------------------------------

The File Name

By contrast, and because TFileListBox::FileName is a read-write property, to select a file, assign its complete path and name to this property. When you do this, make sure you provide a path that exists. Otherwise, you would receive an error.

When a file is selected, if you want, you can show its name in an edit box. To suppport this, the TFileListBox class is equipped with a property named EditFile:

__property Stdctrls::TEdit * FileEdit = {read=FFileEdit,write=SetFileEdit};

To visually use this property, add a TEdit control to your form. On the form, select the file list box. In the Object Inspector, select the name of the edit box in the EditFile field.

Practical LearningPractical Learning: Adding an Edit Control

  1. In the Tool Palette, click Standard
  2. Click the TEdit control Edit and click inside the frame
  3. Adjust the design as follows:
     
    File Viewer
  4. In the Object Inspector, click the file list box to select it
  5. In the Object Inspector, click FileEdit and select the Edit1 object
  6. Press F9 to execute
  7. Double-click a folder in the directory list box
  8. Close the form and return to your programming environment
 
 
 

Home Copyright © 2010-2016, FunctionX