Design idea of 6.3.2 filename browsing and searching system
As an application instance of the file control, we developed a simple file name browsing lookup system. This system can be used to display the file name, to write the selected file to the list box, and to find the file by using the wildcard character entered in the File edit box.
Table 6.5 Design of parts
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Part property Features
─────────────────────────────────────
Filectrform position=podefault main Window
Dirlabel Display current directory
Fileedit taborder=0 Display current file/input file display match
FileListBox1 Fileedit=fileedit Display the current directory file
DirectoryListBox1 Dirlabel=dirlabel Display current drive directory
filelist= FileListBox1
DriveComboBox1 dirlist= DirectoryListBox1 Select the current drive
FilterComboBox1 filelist=filelistbox1 Select File Display Type
Filter= ' All Files (*.*) |*.*|
Source Files (*.pas) |*.pas|
Form Files (*.DFM) |*.dfm|
Project Files (*.DPR) |*.DPR '
LISTBOX1 Displays the selected or found files
Button1 caption= ' Find ' according to the contents of Fileedit
Button2 caption= ' exit ' exit system
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
function and implementation of 6.3.3 file name browsing lookup system
6.3.3.1 displays the files in the current directory by the specified suffix name
Implementing this functionality requires only a proper connection between the controls, and no code support is required. The method of establishing a connection, as described in (6.3.1).
6.3.3.2 to add the selected file to the list box
In the FileListBox1 onclick event:
Procedure Tfilectrform.filelistbox1click (Sender:tobject);
Begin
If searched then
Begin
Searched: = False;
ListBox1.Items.Clear;
Label5.caption: = ' Selected Files ';
End
If NotInList (Extractfilename (filelistbox1.filename), listbox1.items) Then
LISTBOX1.ITEMS.ADD (Extractfilename (filelistbox1.filename));
End
Searched is a global variable that indicates whether the current display of ListBox1 is the result of a lookup or a file selected from FileListBox1.
function NotInList is used to determine whether the string to be added already exists in a Tstrings object. function returns a Boolean variable.
The specific implementation of NotInList is as follows.
Function tfilectrform.notinlist (filename:string;items:tstrings): Boolean;
Var
I:integer;
Begin
For I: = 0 to Items.count-1 do
If items[i] = FileName Then
Begin
NotInList: = False;
Exit;
End
NotInList: = True;
End
6.3.3.3 displays the files in the current directory by the specified matching string
When you enter a matching string in Fileedit and return, the File list box displays the matching result. This functionality is implemented in the Fileedit onkeypress event.
Procedure tfilectrform.fileeditkeypress (Sender:tobject; var key:char);
Begin
If Key = #13 Then
Begin
Filelistbox1.applyfilepath (Fileedit.text);
Key: = #0;
End
End
The Applyfilepath method provided by the File list box is the key to solving this problem.
6.3.3.4 finds files in the current directory by the specified matching string
In order to compare, we use another method to achieve the search function of the file, that is, using standard process FindFirst, FindNext. FileList1 is exactly the same as the content in ListBox1.
When the user clicks the Find button, the file that matches the string in Fileedit is displayed in ListBox1. Here is the implementation code.
Procedure Tfilectrform.button1click (Sender:tobject);
Var
I:integer;
Searchrec:tsearchrec;
Begin
Searched: = True;
Label5.caption: = ' Search result ';
ListBox1.Items.Clear;
FindFirst (FILEEDIT.TEXT,FAANYFILE,SEARCHREC);
LISTBOX1.ITEMS.ADD (Searchrec.name);
Repeat
I: = FindNext (SEARCHREC);
If i = 0 Then
LISTBOX1.ITEMS.ADD (Searchrec.name);
Until I <> 0;
End
Searchrec is a tsearchrec type of record. The definition of Tsearchrec is as follows:
Tsearchrec = Record
FILL:ARRAY[1..21] of Byte;
Attr:byte;
Time:longint;
Size:longint;
NAME:STRING[12];
End
A lot of information is provided in this structure, and flexible application will bring great convenience to programming. Let me give you a few examples here.
1. Detects the size of a given file.
function GetFileSize (const filename:string): Longint;
Var
Searchrec:tsearchrec;
Begin
If FindFirst (Expandfilename (FileName), faanyfile, Searchrec) = 0 Then
Result: = Searchrec.size
Else
Result: =-1;
End
This procedure will be applied in the next section.
2. Getting the timestamp of a given file is in fact equivalent to the Fileage function.
function Getfiletime (const filename:string): Longint;
Var
Searchrec:tsearchrec;
Begin
If FindFirst (Expandfilename (FileName), faanyfile, Searchrec) = 0 Then
Result: = Searchrec.time
Else
Result: =-1;
End
3. Detect the properties of the file. If the file has some attribute, the
Searchrec.attr and givenattr > 0
The values and meanings of the attribute constants correspond to the following table:
Table 6.6 Value and meaning of attribute constants
━━━━━━━━━━━━━━━━━━━━
Constant Value Description
─────────────────────
Fareadonly $01 read-only file
Fahidden $02 hidden files
Fasysfile $04 System files
Favolumeid $08 Volume label file
Fadirectory $ directory File
Faarchive $ file
Faanyfile $3f Any File
━━━━━━━━━━━━━━━━━━━━