TTreeview e Menu

Non vi č mai capitato di dover usare dei menų nei vostri programmi? Se la risposta č si allora sappiate che č possibile tabellare i menų (con le dovute premure) e visualizzare tali menų in una TTreeview in maniere tale da rendere il programma pulito ed efficente.


Per prima cosa vediamo il codice d'esempio che potete trovare in versione integrale all'indirizzo: www.lazaruspascal.it/esempi/MenuTabellato.zip

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  PairSplitter, ComCtrls, StdCtrls, ZConnection, ZDataset;

type

  { TForm1 }

  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    PageControl1: TPageControl;
    PairSplitter1: TPairSplitter;
    PairSplitterSide1: TPairSplitterSide;
    PairSplitterSide2: TPairSplitterSide;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    TreeView1: TTreeView;
    MyConn: TZConnection;
    MyQuery: TZQuery;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
  private
    { private declarations }
    function InserisciNodiFigli(NodoPadre: integer): boolean;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
   i: integer;
begin
     MyConn.Protocol:='sqlite-3';
     MyConn.HostName:='localhost';
     MyConn.Database:= Application.Location + 'menu.s3db';
     MyConn.Connect;

     i:=0;
     InserisciNodiFigli(i);
     TreeView1.ShowRoot:=true;
end;

function TForm1.InserisciNodiFigli(NodoPadre: integer): boolean;
var
   MyQuery2: TZQuery;
   app: TTreeNode;
begin
     if NodoPadre>1 then
        app:=TreeView1.Items.GetLastSubNode;
     MyQuery2:=TZQuery.Create(nil);
     MyQuery2.Connection:=MyConn;
     MyQuery2.SQL.Text:='select * FROM T_Menu WHERE NodoPadre=' + IntToStr(NodoPadre) + ' order by Posizione ASC;';
     MyQuery2.Open;
     if not MyQuery2.EOF then
     begin
          MyQuery2.First;
          while not MyQuery2.EOF do
          begin
               if NodoPadre=0 then
               begin
                    TreeView1.Items.Add(nil, MyQuery2.FieldByName('Descrizione').Text);
               end
               else if NodoPadre=1 then
               begin
                    TreeView1.Items.AddChild(TreeView1.Items.GetLastNode, MyQuery2.FieldByName('Descrizione').Text);
               end
               else
               begin
                    TreeView1.Items.AddChild(app , MyQuery2.FieldByName('Descrizione').Text);
               end;

               InserisciNodiFigli(MyQuery2.FieldByName('Posizione').AsInteger);
               MyQuery2.Next;
          end;
     end;
     MyQuery2.Close;
     MyQuery2.Free;
     InserisciNodiFigli:=true;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
     MyConn.Disconnect;
end;

procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
     MyQuery.SQL.Text:='select NumTab FROM T_Menu WHERE Descrizione="' + StringReplace(TreeView1.Selected.Text,'''','''''', [rfReplaceAll]) + '";';
     MyQuery.Open;
     if not MyQuery.EOF then
     begin
          MyQuery.First;
          PageControl1.TabIndex:=MyQuery.FieldByName('NumTab').AsInteger;
     end;
     MyQuery.Close;
end;

end.


Come potete notare la funzione ricorsiva InserisciNodiFigli crea il menų popolato con i dati tabellati, e nell'esempio che abbiamo appena visto i dati sono cosė espressi

Descrizione NodoPadre Posizione NumTab
Clienti 0 1 0
Inserisci 1 2 0
Modifica 1 3 0
Ins1 3 4 1
Ins2 3 5 1
Elimina 1 6 2
Magazzino 0 7 0


Quello che bisogna capire č che il NodoPadre deve essere zero per tutte le righe che devono comparire come voci primarie del menų, mentre il campo Posizione identifica l'ordine assoluto con il quale vengono inseriti nel menų, quindi non ci devono essere voci duplicate e i valori devono essere incrementali da 1 a N.


Per capire meglio vi consiglio di scaricare l'esempio e analizzarlo, č pių facile da farlo che dirlo. Buona programmazione.



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

Go back to article