Friday, December 27, 2019

How to Use Delphi to Build a Custom Windows Explorer

Windows Explorer is what you use in the Windows operating system to browse for files and folders. You can create a similar structure with Delphi so that the same content is populated within your programs user interface. Common dialog boxes are used in Delphi to open and save a file in an application. If you want to use customized file managers and directory browsing dialogs, you have to deal with file system Delphi components. The Win 3.1 VCL palette group includes several components that allow you to build your own custom File Open or File Save dialog box: TFileListBox, TDirectoryListBox, TDriveComboBox, and TFilterComboBox. Navigating Files The file system components allow us to select a drive, see the hierarchical directory structure of a disk, and see the names of the files in a given directory. All of the file system components are designed to work together. For example, your code checks what the user has done to, say, a DriveComboBox and then passes this information on to a DirectoryListBox. The changes in DirectoryListBox are then passed to a FileListBox in which the user can select the file(s) needed. Designing the Dialog Form Start a new Delphi application and select the Win 3.1 tab of the Component palette. Then do the following: Place one TFileListBox, TDirectoryListBox, TDriveComboBox, and TFilterComboBox component on a form, keeping all of their default namesAdd one TEdit (named FileNameEdit) and one TLabel (call it DirLabel).Include a few labels with captions, like File Name, Directory, List Files of Type, and Drives. To show the currently selected path as a string in a DirLabel components caption, assign the Labels name to the DirectoryListBoxs DirLabel property. If you want to display the selected filename in an EditBox (FileNameEdit), you have to assign the Edit objects Name (FileNameEdit) to the FileListBoxs FileEdit property. More Lines of Code When you have all the file system components on the form, you just have to set the DirectoryListBox.Drive property and the FileListBox.Directory property in order for the components to communicate and show what the user wants to see. For example, when the user selects a new drive, Delphi activates the DriveComboBox OnChange event handler. Make it look like this:   procedure TForm1.DriveComboBox1Change(Sender: TObject) ;beginDirectoryListBox1.Drive : DriveComboBox1.Drive;end; This code changes the display in the DirectoryListBox by activating its OnChange event Handler:   procedure TForm1.DirectoryListBox1Change(Sender: TObject) ;beginFileListBox1.Directory : DirectoryListBox1.Directory;end; In order to see what file the user has selected, you need to use the OnDblClick event of the FileListBox:   procedure TForm1.FileListBox1DblClick(Sender: TObject) ;beginShowmessage(Selected: FileListBox1.FileName) ;end; Remember that the Windows convention is to have a double-click choose the file, not a single click. This is important when you work with a FileListBox because using an arrow key to move through a FileListBox would call any OnClick handler that you have written. Filtering the Display Use a FilterComboBox to control the type of files that are displayed in a FileListBox. After setting the FilterComboBoxs FileList property to the name of a FileListBox, set the Filter property to the file types that you want to display. Heres a sample filter:   FilterComboBox1.Filter : All files (*.*)|*.* | Project files (*.dpr)|*.dpr | Pascal units (*.pas)|*.pas; Hints and Tips Setting the DirectoryListBox.Drive property and the FileListBox.Directory property (in the previously written OnChange event handlers) at runtime  can be also be done at design time. You can accomplish this kind of connection at design time by setting the following properties (from the Object Inspector): DriveComboBox1.DirList : DirectoryListBox1DirectoryListBox1.FileList : FileListBox1 Users can select multiple files in a FileListBox if its MultiSelect property is True. The following code shows how to create a list of multiple selections in a FileListBox and show it in a SimpleListBox (some ordinary ListBox control).   var k: integer;...with FileListBox1 doif SelCount 0 thenfor k:0 to Items.Count-1 doif Selected[k] thenSimpleListBox.Items.Add(Items[k]) ; To display full path names that are not shortened with an ellipsis, do not assign a Label object name to the DirLabel property of a DirectoryListBox. Instead, insert a Label into a form and set its caption property in the DirectoryListBoxs OnChange event to the DirectoryListBox.Directory property.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.