I have a TTreeView
on my form which gets populated from a DB table. The list currently has 22 items and all of them have checkboxes that can be checked.
The TTreeView
is on a TForm
that has a TPageControl
with a pre-made TTabSheet
and all other TTabSheet
s are created dynamically and assigned TFrame
s to them.
My current code to create a new TTabSheet
at runtime looks like this:
procedure TForm1.Button2Click(Sender: TObject);
var
aTab: TTabSheet;
begin
aTab := TTabSheet.create(self);
aTab.Name := 'tabProduct_' + IntToStr(PageControl1.PageCount+1);
aTab.PageControl := PageControl1;
aTab.Caption := 'Product ' + IntToStr(aTab.PageIndex);
LoadFrame(aTab.PageIndex);
end;
The code for the LoadFrame()
procedure is:
procedure TForm1.LoadFrame(const index: integer);
var
aClassName: string;
aFrameClass : TFrameClass;
I: Integer;
begin
if index >= 50 then
raise Exception.create('Max product count reached');
if index >= Length(frames) then
SetLength(frames, index+1);
if assigned(frames[curIndex]) then frames[curIndex].hide;
if not assigned(frames[index]) then
begin
if index = 0 then
aClassname := 'TframeClient' // client
else
aClassname := 'TframeProdus'; // anything over pageindex 0 is a product
aFrameClass := TFrameClass(GetClass(aClassname));
if not assigned(aFrameClass) then
raise exception.createfmt('Could not find class %s', [aClassname]);
frames[index] := aFrameClass.Create(self);
frames[index].name := 'myframe' + IntToStr(index); // unique name
frames[index].parent := PageControl1.pages[index];
frames[index].align := alClient;
end;
frames[index].show;
curIndex := Index;
end;
Other relevant code:
type
TFrameArray = array of TFrame;
<...>
private
{ Private declarations }
curIndex: integer;
frames: TFrameArray;
procedure LoadFrame(const index: integer);
public
{ Public declarations }
end;
TFrameClass = class of TFrame;
<...>
Let's say I check the box next to items 1, 5 and 13 in the TTreeView
.
How can I determine that and modify the Button2
code to create TTabSheet
s and their TFrame
s only for the items I have checked in the TTreeView
?
Meanwhile, managed to figure out this approach and it seems to work well
procedure TForm1.Button2Click(Sender: TObject);
var
aTab: TTabSheet;
i: Integer;
begin
for i:=1 to TreeView1.Items.Count do
begin
if TreeView1.Items[i-1].Checked then
begin
aTab := TTabSheet.create(self);
aTab.Name := 'tabProduct_' + IntToStr(PageControl1.PageCount+1);
aTab.PageControl := PageControl1;
aTab.Caption := TreeView1.Items.Item[i-1].Text;
LoadFrame(aTab.PageIndex);
end;
end;
end;