A string list is typically part of a component. There are times, however, when it is convenient to create independent string lists, for example to store strings for a lookup table. The way you create and manage a string list depends on whether the list is short-term (constructed, used, and destroyed in a single routine) or long-term (available until the application shuts down). Whichever type of string list you create, remember that you are responsible for freeing the list when you finish with it.Short-term string listsIf you use a string list only for the duration of a single routine, you can create it, use it, and destroy it all in one place. This is the safest way to work with string lists. Because the string-list object allocates memory for itself and its strings, you should use a try...finally block to ensure that the memory is freed even if an exception occurs.1 Construct the string-list object. 2 In the try part of a try...finally block, use the string list. 3 In the finally part, free the string-list object.The following event handler responds to a button click by constructing a string list, using it, and then destroying it.procedure TForm1.Button1Click(Sender: TObject); var TempList: TStrings; { declare the list } begin TempList := TStringList.Create; { construct the list object } try { use the string list } finally TempList.Free; { destroy the list object } end; end;Long-term string listsIf a string list must be available at any time while your application runs, construct the list at start-up and destroy it before the application terminates.4 In the unit file for your application's main form, add a field of type TStrings to the form's declaration. 5 Write an event handler for the main form's OnCreate event that executes before the form appears. It should create a string list and assign it to the field you declared in the first step. 6 Write an event handler that frees the string list for the form's OnClose event.This example uses a long-term string list to record the user's mouse clicks on the main form, then saves the list to a file before the application terminates.unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; {For CLX apps: uses SysUtils, Variants, Classes, QGraphics, QControls, QForms, QDialogs;}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.
sL.Lines.Add('abc'); //第1行
sL.Lines.Add('bcdfasdfasdf'); //第2行
Richedit1.Lines.Assign(sL);sL.Lines.Assign(Richedit1.Lines);
2 In the try part of a try...finally block, use the string list.
3 In the finally part, free the string-list object.The following event handler responds to a button click by constructing a string list, using it, and then destroying it.procedure TForm1.Button1Click(Sender: TObject);
var TempList: TStrings; { declare the list }
begin
TempList := TStringList.Create; { construct the list object }
try { use the string list }
finally TempList.Free; { destroy the list object }
end;
end;Long-term string listsIf a string list must be available at any time while your application runs, construct the list at start-up and destroy it before the application terminates.4 In the unit file for your application's main form, add a field of type TStrings to the form's declaration.
5 Write an event handler for the main form's OnCreate event that executes before the form appears. It should create a string list and assign it to the field you declared in the first step.
6 Write an event handler that frees the string list for the form's OnClose event.This example uses a long-term string list to record the user's mouse clicks on the main form, then saves the list to a file before the application terminates.unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;
{For CLX apps: uses SysUtils, Variants, Classes, QGraphics, QControls, QForms, QDialogs;}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.
sL.Lines.Add('abc'); //第1行
sL.Lines.Add('bcdfasdfasdf'); //第2行
sL.free;Richedit1.Lines.Assign(sL);sL.Lines.Assign(Richedit1.Lines);
strArray:TStrings;
i:integer;
begin
i := 1;
strArray := TStringList.Create();
try
strArray.add('a',pointer(i));
....;
finally
strArray.free;
end;