Chapter III-List of strings and Applications (i) (2)

Source: Internet
Author: User
Tags finally block terminates

3.1.2 Load, save string list

The application can easily store a list of Delphi strings in a text file, or reload (or load another different list) from a text file, and a string list has a special way to handle such operations.

Use the LoadFromFile method to load a list of strings from a file, loadfromfile to load each line of string into the list from a text file.

Save the list in a file using the SaveToFile method, the parameter that passes the filename when used. If the file does not exist, SaveToFile will create it or overwrite the existing file contents with the list.

The following code loads the Autoexec.bat file and backs up the file name with Autoexec.bak.

Procedure Tform1.formcreat (Sender:tobject);




Filename:= ' C:\AUTOEXEC. BAT ';

With Memo1 do


LoadFromFile (FileName)

SaveToFile (Changefileext (FileName, ' BAK '));



3.1.3 Create a new string list

In most cases, the list of strings used by an application is a part of a part, so you do not have to create a list, but Delphi allows you to create a list of strings that do not depend on parts.

It is worth noting that the list of strings that the program creates must be free of memory space after it has been used. There are two different situations that need to be addressed: one is to create, use, and release a list of strings in a simple way, and the second is to be created by the program, which can be used during the run and released before the program terminates. Both of these situations depend primarily on creating a short list of strings or long lists of strings. Short-term string list

Short-term string lists are used to handle simple things. The program creates, uses, and releases the list at the same place. This is the safest way to use a list of strings.

Because the string list allocates memory for itself and its strings, you use the Try ... Finally, the list is protected to ensure that the memory space occupied by the list is freed after an exception occurs.

The basic steps for creating a short list of strings are:

1. Construct a string list object;

2. In try.. Finally block to use the list;

3. Release the list space after finally.

The following code creates a list, uses a list, and finally releases the list space:

Procedure Tform1.button1click (Sender:tobject);






{Use the string list}



End Long String list

If you want to use a list of strings at any time the program runs, you need to create the list when the program starts running and release it before the program terminates.

The steps for creating a list of strings at run time are:

1. Add the Tstringslist Type field to the domain of the main form object of the program;

2. Create a handle in the OnCreate event of the main form, which runs before the main form is displayed;

3. Creates a string list object after the event handle is created;

4. Create a handle to the OnDestroy event on the main form that runs before the main form disappears.

This allows any procedure or event to access the list of strings during the program's operation.

The following code adds a Clicklist string list to the program, each time the user presses a mouse button, the program adds a string to the Clicklist, which is saved to the file before the program ends.

Unit Unit1;


Uses Wintypes, Winprocs, Classes, Graphics, Forms, Controls, Apps;


TForm1 = Class (Tform)

Procedure Formcreate (Sender:tobject);

Procedure Formdestroy (Sender:tobject);

Procedure Formmousedown (Sender:tobject; Button:tmousebutton;

Shift:tshiftstate; X, Y:integer);


{Private declarations}


{Public declarations}

Clicklist:tstrings; {Declare the field}





{$R *. DFM}

Procedure Tform1.formcreate (Sender:tobject);


Clicklist: = tstringlist.create; {Construct the list}


Procedure Tform1.formdestroy (Sender:tobject);


Clicklist.savetofile (Changefileext) (Application.exename, '. LOG));

{Save the list}; {Destroy the List object}


Procedure Tform1.formmousedown (Sender:tobject; Button:tmousebutton;

Shift:tshiftstate; X, Y:integer);


Clicklist.add (Format (' Click at (%d,%d) ', [X, Y])); {Add a

String to the list}



Related Article

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.