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

Source: Internet
Author: User
Tags finally block integer 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);

Var

filename:string;

Begin

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

With Memo1 do

Begin

LoadFromFile (FileName)

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

End

End

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.

3.1.3.1 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);

Var

Temlist:tstrings;

Begin

Templist:=tstringlist.create;

Try

{Use the string list}

Finally

Templist.free;

End

End

3.1.3.2 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;

Interface

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

Type

TForm1 = Class (Tform)

Procedure Formcreate (Sender:tobject);

Procedure Formdestroy (Sender:tobject);

Procedure Formmousedown (Sender:tobject; Button:tmousebutton;

Shift:tshiftstate; X, Y:integer);

Private

{Private declarations}

Public

{Public declarations}

Clicklist:tstrings; {Declare the field}

End

Var

Form1:tform1;

Implementation

{$R *. DFM}

Procedure Tform1.formcreate (Sender:tobject);

Begin

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

End

Procedure Tform1.formdestroy (Sender:tobject);

Begin

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

{Save the list}

Clicklist.free; {Destroy the List object}

End

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

Shift:tshiftstate; X, Y:integer);

Begin

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

String to the list}

End

End.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.