TTreeView

A volte pu nascere l'esigenza di creare dei men laterali, e il modo pi semplice per crearli attraverso l'oggetto TTreeView che contiene una struttura ad albero, segue un esempio e al fondo trovate il link all'esempio da scaricarvi per analizzarlo e giocarci.

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    TreeView1: TTreeView;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    Procedure DeleteNode(Node:TTreeNode);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
     TreeView1.Items.Add(nil,'Primo');
     TreeView1.Items.AddChild(TreeView1.Items.GetLastNode,'1');
     TreeView1.Items.AddChild(TreeView1.Items.GetLastNode,'2');
     TreeView1.Items.AddChild(TreeView1.Items.GetLastNode,'3');
     TreeView1.Items.Add(nil,'Secondo');
     TreeView1.Items.AddChild(TreeView1.Items.GetLastNode,'4');
     TreeView1.Items.AddChild(TreeView1.Items.GetLastNode,'5');
     TreeView1.Items.AddChild(TreeView1.Items.GetLastNode,'6');
     TreeView1.Items.Add(nil,'Terzo');
     TreeView1.Items.AddChild(TreeView1.Items.GetLastNode,'fine');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
  s: string;
begin
  // if there is no nodes, create a root node with a parent of Nil
  if TreeView1.Items.Count = 0 then
    begin
      Treeview1.Items.Add (nil,'Root Node');
      exit;
    end;

  // Set up a simple text for each new node - Node1 , Node2 etc
  i := treeview1.Items.Count;
  s := 'Node ' + inttostr(i);
  //Add a new node to the currently selected node
  if TreeView1.Selected <> nil then
    Treeview1.Items.AddChild(Treeview1.Selected ,s);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if TreeView1.Selected = nil then exit;
  //If selected node has child nodes, first ask for confirmation
  If treeview1.Selected.HasChildren then
    if messagedlg('Delete node and all children ?',mtConfirmation,
                 [mbYes,mbNo],0) <> mrYes then exit;
  DeleteNode(TreeView1.Selected);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
     ShowMessage(TreeView1.Selected.GetTextPath);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
     ShowMessage(TreeView1.Selected.Text);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
     ShowMessage('Indice assoluto: ' + IntToStr(TreeView1.Selected.AbsoluteIndex));
end;

//A procedure to recursively delete nodes
Procedure TForm1.DeleteNode(Node:TTreeNode);
begin
  while Node.HasChildren do DeleteNode(node.GetLastChild);
  TreeView1.Items.Delete(Node) ;
end;

end.

Link: www.lazaruspascal.it/esempi/TTreeView.zip


SMF 2.0.8 | SMF © 2011, Simple Machines
Privacy Policy
SMFAds for Free Forums
TinyPortal © 2005-2012

Go back to article