unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
btn3: TButton;
btn4: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
List: TList;
{声明 List}
frmRed: TForm; {用于测试的对象, 这里选择了窗体}
begin
List := TList.Create; {建立列表}
frmRed := TForm.Create(nil); {建立 frmRed}
frmRed.Caption:='red'; {frmRed 标题}
frmRed.Color := clRed; {frmRed 颜色}
List.Add(frmRed); {添加到列表}
frmRed:=TForm.Create(nil); {又建立个frmred}
frmRed.Caption:='bre'; {frmred 标题}
frmRed.Color :=clBlue; {frmred 颜色}
List.Add(frmRed); {添加到列表}
ShowMessage(IntToStr(List.Count)); {1; 列表元素总数}
//TForm(List.Items[0]).ShowModal; {调用对象}
TForm(List[0]).ShowModal; {调用对象, 比上一行的写法简洁, 但是一样的}
TForm(List[1]).ShowModal;
frmRed.Free;
List.Free;
end;
procedure TForm1.btn2Click(Sender: TObject);
// Insert、Remove、Delete、Clear、First、Last
var
List: TList;
frmRed,frmGreen,frmBlue: TForm;
i: Integer;
begin
List := TList.Create;
{建立两个窗体: 红色、绿色}
frmRed := TForm.Create(nil);
frmRed.Caption := 'Red';
frmRed.Color := clRed;
frmGreen := TForm.Create(nil);
frmGreen.Caption := 'Green';
frmGreen.Color := clGreen;
{添加到 List}
List.Add(frmRed);
List.Add(frmGreen);
{再建立一个蓝色窗体}
frmBlue := TForm.Create(nil);
frmBlue.Caption := 'Blue';
frmBlue.Color := clBlue;
{插入到 List}
List.Insert(0, frmBlue);
ShowMessage(IntToStr(List.Count)); {3}
TForm(List.First).ShowModal; {显示第一个: 蓝色窗体}
TForm(List.Last).ShowModal; {显示最后一个: 绿色窗体}
{看看红色窗体是第几个?}
i := List.IndexOf(frmRed);
ShowMessage(IntToStr(i)); {1; 是第二个}
{从列表中删除 - 用对象指针}
List.Remove(frmRed);
ShowMessage(IntToStr(List.Count)); {2}
frmRed.ShowModal; {frmRed 依然存在, 只是不在列表中了}
{从列表中删除 - 用元素索引}
List.Delete(0);
frmBlue.ShowModal; {frmBlue 依然存在, 只是不在列表中了}
{清空列表}
List.Clear;
frmRed.Free;
frmGreen.Free;
frmBlue.Free;
List.Free;
end;
procedure TForm1.btn3Click(Sender: TObject);
// Capacity、Move、Exchange、Extract
var
List: TList;
frmRed,frmGreen,frmBlue: TForm;
p: Pointer;
i: Integer;
begin
List := TList.Create;
List.Capacity := 3; {让 List 准备好接受 3 个元素}
{建立红、绿、蓝三个窗体, 并添加到 List}
frmRed := TForm.Create(nil);
frmRed.Caption := 'Red'; frmRed.Color := clRed;
frmGreen := TForm.Create(nil);
frmGreen.Caption := 'Green'; frmGreen.Color := clGreen;
frmBlue := TForm.Create(nil);
frmBlue.Caption := 'Blue'; frmBlue.Color := clBlue;
List.Add(frmRed);
List.Add(frmGreen);
List.Add(frmBlue);
{依次显示窗体标题}
for i := 0 to List.Count - 1 do
ShowMessage(TForm(List[i]).Caption); {将分别显示: Red Green Blue}
{移动}
List.Move(2,0); {把第 3 个转移到第 1 个}
for i := 0 to List.Count - 1 do
ShowMessage(TForm(List[i]).Caption); {将分别显示: Blue Red Green}
{置换}
List.Exchange(1,2); {把第 2 个与第 3 个交换}
for i := 0 to List.Count - 1 do
ShowMessage(TForm(List[i]).Caption); {将分别显示: Blue Green Red}
{提取指针}
p := List.Extract(frmGreen);
ShowMessage(TForm(p).Caption); {Green}
frmRed.Free;
frmGreen.Free;
frmBlue.Free;
List.Free;
end;
end.
请发表评论