Help needed with Treeview

Dear Experts,

I have made a programm with a treeview on it that loads and saves the nodes to a database.
On the form are also 2 buttons, one for creating a Folder-node and the other for creating
a File-node. The procedures of both buttons looks like this:

procedure TMainForm.AddItem(aText: string; aIndex: Integer; aParent: TTreeNode);
var n: TTreeNode;
  data: TItem;
  parentId: integer;
begin
  parentId := 0;
  if (aParent <> nil) and (TItem(aParent.Data) <> nil) then
    parentId := TItem(aParent.Data).ID;
  data := TItem.Create(AdoQuery1, -1, parentId, 0);
  data.NodeText := aText;
  data.ImageIndex := aIndex;
  n := TTreeNode.Create(TreeView1.Items);
  n.Text := data.NodeText;
  n.ImageIndex := data.ImageIndex;
  n.SelectedIndex := data.ImageIndex;
  TreeView1.Items.AddNode(n, aParent, aText, data, naAddChild);
end;
(*-----------------------------------------*)
procedure TMainForm.NewFolder1Click(Sender: TObject);
var aText: string;
begin
  if InputQuery('Folder', 'Enter a folder name', aText) then
    AddItem(aText, 15, nil);
end;
(*-----------------------------------------*)
procedure TMainForm.NewItem1Click(Sender: TObject);
var aText: string;
begin
  if (TreeView1.Selected <> nil) and (TreeView1.Selected.ImageIndex = 15) then
  begin
     if InputQuery('File', 'Enter a file name', aText) then
      AddItem(aText, 17, TreeView1.Selected);
  end else
    ShowMessage('Select a folder in the treeview first.');
end;
(*-----------------------------------------*)

My question:
1. To make a subfolder node in a folder node.
2. When the folder is selected display image-index 16

I have put the code for loading and saving the nodes to a database
in the code-section, maybe you experts need this too to solve my problem.

Greetings, Peter Kiers

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, ComCtrls, StdCtrls, ImgList, ExtCtrls;
 
type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Splitter1: TSplitter;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    ImageList1: TImageList;
    ADOQuery1: TADOQuery;
    ADOConnection1: TADOConnection;
    Memo1: TMemo;
    FolderBtn: TButton;
    FileBtn: TButton;
    procedure FolderBtnClick(Sender: TObject);
    procedure FileBtnClick(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Memo1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    procedure savetree(t: ttreeview);
    procedure loadtree(t: TTreeView);
    procedure saveNode(n: TTreeNode);
    function findNode(t:TTreeView; id: integer): TTreeNode;
    procedure AddItem(aText: string; aIndex: Integer; aParent: TTreeNode);
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
type
  TItem = class(TObject)
  private
    fStatus: byte;
    fParentID: integer;
    fNodeText: string;
    fID: integer;
    fData: string;
    fSaver: TAdoQuery;
    fChanged: Boolean;        
    fSaveImmediate: boolean;
    fIsNew: Boolean;
    fImageIndex: integer;
    procedure SetData(const Value: string);
    procedure SetNodeText(const Value: string);
    procedure SetParentID(const Value: integer);
    procedure SetStatus(const Value: byte);
    procedure SetImageIndex(const Value: integer);
  protected
    procedure Changed; dynamic;
  public
    constructor Create(aSaver: TAdoQuery; AId, AParentID: integer; AStatus: byte);
    constructor LoadFromDataset(aSaver: TAdoQuery);
    procedure BeginUpdate;
    procedure EndUpdate;
    procedure Delete;
    procedure Save;
  published
    property ID: integer read fID write fID;
    property ParentID: integer read fParentID write SetParentID;
    property ImageIndex: integer read fImageIndex write SetImageIndex;
    property Status: byte read FStatus write SetStatus;
    property Data: string read fData write SetData;
    property NodeText: string read fNodeText write SetNodeText;
  end;
 
{ TItem }
 
procedure TItem.BeginUpdate;
begin
  fSaveImmediate := false;                 
end;
(*---------------------------------------------------*)
procedure TItem.Changed;
begin
  fChanged := True;
  if fSaveImmediate then Save;
end;
(*---------------------------------------------------*)
constructor TItem.Create(aSaver: TAdoQuery; AId, AParentID: integer;
  AStatus: byte);
begin
  inherited Create;
  fChanged := False;
  fSaveImmediate := True;
  fIsNew := True;
  fSaver := aSaver;
  fID := AID;
  fParentID := AParentID;
  fStatus := AStatus;
  fNodeText := '';
  fData := '';
end;
(*---------------------------------------------------*)
procedure TItem.EndUpdate;
begin
  fSaveImmediate := True;
  if fChanged then Save;
end;
(*---------------------------------------------------*)
constructor TItem.LoadFromDataset(aSaver: TAdoQuery);
begin
  Create(aSaver, 0, 0, 0);
  if not aSaver.IsEmpty then
  begin
    fId := aSaver.FieldByName('ID').AsInteger;
    fParentID := aSaver.FieldByName('PARENT').AsInteger;
    fStatus := aSaver.FieldByName('STATUS').AsInteger;
    fNodeText := aSaver.FieldByName('NAME').AsString;
    if aSaver.FindField('DATA') <> nil then
      fData := aSaver.FieldByName('DATA').AsString;
    fImageIndex := aSaver.FieldByName('IMAGE_INDEX').AsInteger;
    fIsNew := False;
  end;
end;
(*---------------------------------------------------*)
procedure TItem.Save;
begin
  if fIsNew then
    fSaver.SQL.Text :=
      'insert into tree (parent, name, status, image_index, data) values (:v1, :v2, :v3, :v4, :v5)'
  else
    fSaver.SQL.Text :=
      'update tree set parent = :v1, name = :v2, status = :v3, image_index = :v4, data = :v5 where id = :v6';
  fSaver.Parameters.ParamByName('v1').Value := fparentID;
  fSaver.Parameters.ParamByName('v2').Value := fNodeText;
  fSaver.Parameters.ParamByName('v3').Value := fStatus;
  fSaver.Parameters.ParamByName('v4').Value := fImageIndex;
  fSaver.Parameters.ParamByName('v5').Value := fData;
  if not fIsNew then
    fSaver.Parameters.ParamByName('v6').Value := fId;
  fSaver.ExecSQL;
  if fIsNew then
  begin
    fSaver.SQL.Text :=
      'SELECT id from tree order by id desc';
    fSaver.Open;
    fId := fSaver.FieldByName('ID').asInteger;
    fSaver.Close;
    fIsNew := False;
  end;
  fChanged := False;
end;
(*---------------------------------------------------*)
procedure TItem.SetData(const Value: string);
begin
  if fData <> Value then
  begin
    fData := Value;
    Changed;
  end;
end;
(*---------------------------------------------------*)
procedure TItem.SetImageIndex(const Value: integer);
begin
  if fImageIndex <> Value then
  begin
    fImageIndex := Value;
    Changed;
  end;
end;
(*---------------------------------------------------*)
procedure TItem.SetNodeText(const Value: string);
begin
  if fNodeText <> Value then
  begin
    fNodeText := Value;
    Changed;
  end;
end;
(*---------------------------------------------------*)
procedure TItem.SetParentID(const Value: integer);
begin
  if fParentId <> Value then
  begin
    fParentID := Value;
    Changed;
  end;
end;
(*---------------------------------------------------*)
procedure TItem.SetStatus(const Value: byte);
begin
  if fStatus <> Value then
  begin
    FStatus := Value;
    Changed;
  end;
end;
(*---------------------------------------------------*)
procedure TForm1.Button1Click(Sender: TObject);
begin
  loadTree(treeview1);
end;
(*---------------------------------------------------*)
procedure TForm1.Button2Click(Sender: TObject);
begin
  savetree(treeview1);
end;
(*---------------------------------------------------*)
function TForm1.findNode(t: TTreeView; id: integer): TTreeNode;
var i:integer;
begin
  i:=0;
  while (i<t.items.count) and (TItem(t.items[i].data).ID<>id) do
    inc(i);
  if i<t.items.count then result:=t.items[i]
                     else result:=nil;
end;
(*---------------------------------------------------*)
procedure TForm1.FormCreate(Sender: TObject);
begin
  ADOConnection1.Open;
end;
(*---------------------------------------------------*)
procedure TForm1.loadtree(t: TTreeView);
var
  p, n: TTreeNode;
  data: TItem;
begin
  t.Items.Clear;
  ADOQuery1.SQL.Text := 'select * from tree order by parent asc';
  ADoQuery1.Open;
  while not ADoQuery1.Eof do
  begin
    data := TItem.LoadFromDataset(AdoQuery1);
    p := FindNode(t, data.ParentID);
    n := t.items.AddChildObject(p, data.NodeText, data);
    n.ImageIndex := data.ImageIndex;
    n.SelectedIndex := data.ImageIndex;
    AdoQuery1.Next;
  end;
  AdoQuery1.Close;
end;
(*---------------------------------------------------*)
procedure TForm1.saveNode(n: TTreeNode);
var i, aParent: integer;
  item: TItem;
begin
  item := TItem(n.Data);
  if item <> nil then
  begin
    item.BeginUpdate;
    try
      Item.NodeText := n.Text;
      aParent := 0;
      if (n.Parent <> nil) and (TItem(n.Parent.Data) <> nil) then
        aParent := TItem(n.Parent.Data).ID;
      Item.ParentID := aParent;
      Item.ImageIndex := n.ImageIndex;
    finally
      Item.EndUpdate;
    end;
  end;
  for i := 0 to n.Count-1 do SaveNode(n.Item[i]);
end;
(*---------------------------------------------------*)
procedure TForm1.savetree(t: ttreeview);
var n: TTreeNode;
begin
  n := t.items.getFirstNode;
  while n <> nil do
  begin
    SaveNode(n);
    n := n.getNextSibling;
  end;
end;
(*---------------------------------------------------*)
 
end.

Open in new window

LVL 1
Peter KiersOperatorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dougaugCommented:
Hi Peter,

here are the answers:

1. To make a subfolder node in a folder node.

Try this:

procedure TMainForm.BtnPromoveToFolderClick(Sender: TObject);
begin
  with TreeView1 do
  begin
    // If the selected node has a parent, then it's a file (subfolder node)
    if Selected.Parent <> nil then
    begin
      AddItem(Selected.Text, 15, nil);
      Selected.Delete;
    end;
  end;
end;

2. When the folder is selected display image-index 16
Change your AddItem Method on the referenced line below:

...
  n := TTreeNode.Create(TreeView1.Items);
  n.Text := data.NodeText;
  n.ImageIndex := data.ImageIndex;
  n.SelectedIndex := 16; //<== Change this line
  TreeView1.Items.AddNode(n, aParent, aText, data, naAddChild);
...

I hope this help you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KiersOperatorAuthor Commented:
thanks you have helped me alot.

Peter Kiers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.