I have no experience with frames.
How to use a Tree View with frames? I need to switch among 开发者_StackOverflow中文版nods of the Tree View and show assigned Frame to the selected node.
Big big thank for help!!!
It doesn't really make any difference if the Data
of nodes hold a pointer to a frame or any other kind of object, typecast the pointer to the type of object it holds.
Below code adds two frames ('Frame2' and 'Frame3', created by the IDE - much like a new form), as nodes of a TreeView, and sets the visibility of the selected node's frame to true and the deselected one's to false.
type
TForm1 = class(TForm)
TreeView1: TTreeView;
procedure FormCreate(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure TreeView1Changing(Sender: TObject; Node: TTreeNode;
var AllowChange: Boolean);
procedure FormDestroy(Sender: TObject);
end;
var
Form1: TForm1;
implementation
uses Unit2, Unit3;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
TreeView1.Align := alLeft;
with TFrame(TreeView1.Items.AddObject(nil, 'Frame2', TFrame2.Create(nil)).Data) do begin
Visible := False;
Parent := Self;
Align := alClient;
end;
with TFrame(TreeView1.Items.AddObject(nil, 'Frame3', TFrame3.Create(nil)).Data) do begin
Visible := False;
Parent := Self;
Align := alClient;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
i: Integer;
begin
for i := 0 to TreeView1.Items.Count - 1 do
TFrame(TreeView1.Items[i].Data).Free;
end;
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
TFrame(Node.Data).Visible := True;
end;
procedure TForm1.TreeView1Changing(Sender: TObject; Node: TTreeNode;
var AllowChange: Boolean);
begin
if Assigned((Sender as TTreeView).Selected) then
TFrame(TTreeView(Sender).Selected.Data).Visible := False;
end;
精彩评论