📄 dynamicarrayexamplespas.pas
字号:
unit DynamicArrayExamplespas;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Seths,
StdCtrls, ScktComp;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
procedure FreeThem;
public
{ Public declarations }
end;
var
Form1: TForm1;
p: TPointerArray;
l: TLongArray;
//Byte arrays are also available TByteArray
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
// Create the dynamic arrays
P := TPointerArray.Create( nil );
L := TLongArray.Create(nil);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//Free the buttons
FreeThem;
//Free the arrays. They will automaticly empty themselves
P.Free;
L.Free;
end;
procedure TForm1.FreeThem;
var
lngIndex: LongInt;
begin
//This will empty the buttons pointer array
while p.UBound > 0 do begin
lngIndex := p.UBound;
TButton( P.GetElement(lngIndex) ).Free;
p.RedimPreserve(lngIndex - 1);
end;
p.FreeArray;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
lngIndex: LongInt;
B: TButton;
begin
//Add an element to the array
lngIndex := p.UBound;
p.RedimPreserve( lngIndex + 1 );
//Create a new button
B := TButton.Create(Form1);
B.Parent := Form1;
B.Height := 20;
B.Width := 20;
B.Left := lngIndex * B.Width;
B.Top := 0;
B.Caption := IntToStr( lngIndex );
B.OnClick := Button1Click;
//Point the new array element to the new button
p.SetElement( lngIndex, B );
end;
procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
lngIndex: LongInt;
begin
//Add a new array element
lngIndex := l.UBound + 1;
l.RedimPreserve( lngIndex );
//Assign a value to the new element
//This
l.SetElement( lngIndex, lngIndex );
//or
CopyMemory( l.AddrElement(lngIndex), @lngIndex, 4 );
//do the same thing
//Empty it once it gets to big. Just for the fun of it
if l.UBound = 21 then L.FreeArray;
//Display array inside the list box
ListBox1.Clear;
for lngIndex := 0 to l.UBound do
ListBox1.Items.Add( IntToStr( l.GetElement(lngIndex) ) );
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -