April 17th, 2013, 09:41 AM
-
Deleting Arrary
Hello all, I want to ask, how to delete array.. I mean, I have an array of shapes, and I want to delete them all when I click a button..
This is my listing program, just want to know what must I do with the button to erase the array of shapes..
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Shape1: TShape;
Edit1: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
procedure hapus(C: array of TShape);
{ Public declarations }
end;
var
Form1: TForm1;
Shape : array of TShape;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var I, kiri, a : integer;
begin
a:=(strtoint(edit1.text)) DIV 2;
hapus(Shape);
SetLength(Shape, a);
kiri := 10;
for I := 0 to a-1 do
begin
Shape[I] := TShape.create(nil);
with Shape[I] do
begin
hide;
parent := Panel1;
width := 100;
height := 120;
left := kiri;
top := 50;
kiri := kiri + 100 + 10;
show;
end;
end;
end;
procedure TForm1.hapus(C: array of TShape);
var
I : integer;
begin
for I := low(C) to High(C) do
begin
C[I].free;
C[I] := nil;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
??????????
end;
end.
April 17th, 2013, 10:52 AM
-
Code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Shape1: TShape;
Edit1: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
Shape: array of TShape; //make your array part of your form's declaration
{ Private declarations }
public
procedure hapus(C: array of TShape);
{ Public declarations }
end;
var
Form1: TForm1;
//Shape : array of TShape; Do not use Global Variables!
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
I, kiri, a : integer;
begin
a:=(strtoint(edit1.text)) DIV 2;
hapus(Shape);
SetLength(Shape, a);
kiri := 10;
for I := 0 to a-1 do
begin
Shape[I] := TShape.create(nil);
with Shape[I] do
begin
hide;
parent := Panel1;
width := 100;
height := 120;
left := kiri;
top := 50;
kiri := kiri + 100 + 10;
show;
end;
end;
end;
procedure TForm1.hapus(C: array of TShape);
var
I : integer;
begin
//not necessary to do High to Low in this instance
//but if you were affecting the size of the array
//in this code, you would need to do.
//I prefer to do high to low for this reason.
for I := High(C) downto low(C) do
begin
C[I].free;
C[I] := nil;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Hapus(Shape);//Free all objects from your array.
SetLength(Shape, 0);//set its length to 0.
end;
You may also want to ensure that the array is emptied and length set to zero when the application terminates (in case your user doesn't click the button).
April 17th, 2013, 10:36 PM
-
Thank you very much Majlumbo,, it works.. Great answer..
Originally Posted by majlumbo
Code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Shape1: TShape;
Edit1: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
Shape: array of TShape; //make your array part of your form's declaration
{ Private declarations }
public
procedure hapus(C: array of TShape);
{ Public declarations }
end;
var
Form1: TForm1;
//Shape : array of TShape; Do not use Global Variables!
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
I, kiri, a : integer;
begin
a:=(strtoint(edit1.text)) DIV 2;
hapus(Shape);
SetLength(Shape, a);
kiri := 10;
for I := 0 to a-1 do
begin
Shape[I] := TShape.create(nil);
with Shape[I] do
begin
hide;
parent := Panel1;
width := 100;
height := 120;
left := kiri;
top := 50;
kiri := kiri + 100 + 10;
show;
end;
end;
end;
procedure TForm1.hapus(C: array of TShape);
var
I : integer;
begin
//not necessary to do High to Low in this instance
//but if you were affecting the size of the array
//in this code, you would need to do.
//I prefer to do high to low for this reason.
for I := High(C) downto low(C) do
begin
C[I].free;
C[I] := nil;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Hapus(Shape);//Free all objects from your array.
SetLength(Shape, 0);//set its length to 0.
end;
You may also want to ensure that the array is emptied and length set to zero when the application terminates (in case your user doesn't click the button).