Stark
2011-11-13 18:17:40 UTC
A frame containing a row of edits is represented by a TFrameEdit class
(derived from a Frame class).
As you can see in the following code, a button let the user add additional
row of edits to a pane. Each new newRow object created, is also added to a
TList:
procedure TForm1.BtnAddRowClick(Sender: TObject);
var
newRow: TFrameEdit;
begin
newRow := TFrameEdit.Create(Self); //creating a new instance of TFrame
Edit
inc(numRows);
newRow.Top := lastRow.Top + lastRow.Height;
newRow.Left := lastRow.Left;
newRow.Name := 'Row' + inttostr(numRows);
newRow.Parent := Self;
lastRow := newRow;
frmList.Add(newRow); // adding object to TList frmList
end;
I also need to let the user remove the rows he created on the panel. I am
trying to use the following simple proc, but it doesn't work:
procedure TForm1.BtnClearRowClick(Sender: TObject);
var i: integer;
aFrame: TFrameEdit;
begin
for i := frmList.Count - 1 downto 0 do
begin
aFrame := TFrameEdit(frmList.Items[I]);
if aFrame <> nil then
FreeAndNil(aFrame);
end;
Suppose 3 rows where created, then the aFrame representing Row3 is cleared
all right. The attempt to clear Row2 fails with "Invalid pointer operation".
In fact, the proc is trying to free again the first object in the TList wich
is Row3. I don't understand why this is not found nil in the "if aFrame <>
nil" test, which would avoid the FreAndNil execution where it fails.
Theri is something conceptual which I miss. Can anyone help ?
(derived from a Frame class).
As you can see in the following code, a button let the user add additional
row of edits to a pane. Each new newRow object created, is also added to a
TList:
procedure TForm1.BtnAddRowClick(Sender: TObject);
var
newRow: TFrameEdit;
begin
newRow := TFrameEdit.Create(Self); //creating a new instance of TFrame
Edit
inc(numRows);
newRow.Top := lastRow.Top + lastRow.Height;
newRow.Left := lastRow.Left;
newRow.Name := 'Row' + inttostr(numRows);
newRow.Parent := Self;
lastRow := newRow;
frmList.Add(newRow); // adding object to TList frmList
end;
I also need to let the user remove the rows he created on the panel. I am
trying to use the following simple proc, but it doesn't work:
procedure TForm1.BtnClearRowClick(Sender: TObject);
var i: integer;
aFrame: TFrameEdit;
begin
for i := frmList.Count - 1 downto 0 do
begin
aFrame := TFrameEdit(frmList.Items[I]);
if aFrame <> nil then
FreeAndNil(aFrame);
end;
Suppose 3 rows where created, then the aFrame representing Row3 is cleared
all right. The attempt to clear Row2 fails with "Invalid pointer operation".
In fact, the proc is trying to free again the first object in the TList wich
is Row3. I don't understand why this is not found nil in the "if aFrame <>
nil" test, which would avoid the FreAndNil execution where it fails.
Theri is something conceptual which I miss. Can anyone help ?