Loading,populating and Saving Treeview from database table, add Images saving and drag and drop

Good day Experts

This question is based on a previous question I asked about loading and saving a treeview to a database.
 
Ciuly has helped met a excellent example, but I would like to now how can I load the image index of each node to the treeview items.

I have declared a new field in my table called Image_Index, and have drop a ImageList on my form with all the images.

Ciuly if you are available can you please extend your example to show how to load and save images also.
I will give 250 points, but if you are able to also show me how I can drag and drop treeview items also (to re-arange the nodes in run time), then I will increase the points to 500.

This is very import exercise  for me.
henryreynoldsAsked:
Who is Participating?
 
2266180Connect With a Mentor Commented:
this is the previous question: http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22450852.html?cid=236#a18727487
henry, you should always post the link to other relevant questions jsut in case the expert who helped is not available so otehrs can tap in ;)

ziolko is right about adding the image index. this is the modified unit:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, DB, ADODB, StdCtrls;

type                                  
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    ADOCommand1: TADOCommand;
    ADOConnection1: TADOConnection;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TreeView1Deletion(Sender: TObject; Node: TTreeNode);
  private
    procedure saveNode(n: TTreeNode; parentID: integer);
    procedure savetree(t: ttreeview);
    procedure loadtree(t: TTreeView);
    function findNode(t:TTreeView; id: integer): TTreeNode;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses ADOInt;

{$R *.dfm}

type
  TItem=class
  private
    FStatus: byte;
    FParentID: integer;
    FID: integer;
  public
    constructor Create(AId, AParentID:integer; AStatus:byte);
  published
    property ID:integer read FID write FID;
    property ParentID:integer read FParentID write FParentID;
    property Status:byte read FStatus write FStatus;
  end;

{ TItem }

constructor TItem.Create(AId, AParentID: integer; AStatus: byte);
begin
  ID:=AID;
  ParentID:=AParentID;
  Status:=AStatus;
end;

procedure TForm1.saveNode(n:TTreeNode; parentID:integer);
var id, i:integer;
begin
  ADOCommand1.CommandText:='insert into tree (parent, name, status, image_index) values ('+inttostr(parentID)+','+quotedstr(n.text)+', 1,'+inttostr(n.imageIndex)+')';// <--------------modification here
  ADOCommand1.Execute;
  ADOCommand1.CommandText:='select id from tree where parent='+inttostr(parentid)+' and name='+quotedstr(n.text);
  with ADOCommand1.Execute do
  begin
    MoveFirst;
    id:=Fields.Item[0].Value;
    Close;
  end;
  for i:=1 to n.count do
    savenode(n.item[i-1], id);
end;

procedure TForm1.savetree(t:ttreeview);
var n:TTreeNode;
begin
  ADOCommand1.CommandText:='delete from tree';
  ADOCommand1.Execute;
 
  n:=t.items.getFirstNode;
  while n<>nil do
  begin
    savenode(n, 0);
    n:=n.getNextSibling;
  end;
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.loadtree(t:TTreeView);
var ds:_Recordset;
  function getFieldByName(fieldName:string):Field;
  var i:integer;
  begin
    i:=0;
    while (i<ds.Fields.Count) and (not sametext(ds.Fields[i].Name, fieldName)) do
      inc(i);
    if i<ds.Fields.Count then result:=ds.fields[i]
                         else result:=nil;
  end;
var pid:integer; p:TTreeNode;
begin
  t.Items.Clear;
  ADOCommand1.CommandText:='select * from tree';
  ds:=ADOCommand1.execute;
  with ds do
  begin
    if eof then
      exit;
    movefirst;
    while not eof do
    begin
      pid:=getFieldByName('parent').Value;
      if pid>0 then p:=findNode(t, pid)
               else p:=nil;
      with t.items.addchild(p, getFieldByName('name').value) do//<-------- modifications here
      begin
        data:=TItem.Create(
             getFieldByName('id').value,
             pid,
             getFieldByName('status').value);
        ImageIndex:=getFieldByName('imageIndex').value;
        SelectedIndex:=ImageIndex;
      end;
      movenext;
    end;
    close;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i,j:integer; p:TTreeNode;
begin
  ADOConnection1.Open;
  loadtree(treeview1);
  if treeview1.items.count=0 then
  begin
    for i:=1 to 5 do// add some dummy data
    begin
      p:=treeview1.items.addchild(nil, inttostr(i));
      for j:=1 to 5 do
        treeview1.items.addchild(p, inttostr(i)+'-'+inttostr(j));
    end;
    savetree(treeview1);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  savetree(treeview1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  loadTree(treeview1);
end;

procedure TForm1.TreeView1Deletion(Sender: TObject; Node: TTreeNode);
begin
  if node.Data<>nil then
  begin
    TItem(node.data).free;
    node.data:=nil;
  end;
end;

end.


---------------- (the above was modified directly in the browser editor. hope it compiles and runs fine :D

for the drag and drop part see this: http://www.swissdelphicenter.ch/torry/showcode.php?id=1849
it should be fairly simple to integrate it into the existing code. If you have problems with it, let me know and I'll load anotehr IDE instance to change the demo myself.

(jsut in case: you should split since ziolko was first with a correct answer)
0
 
ziolkoConnect With a Mentor Commented:
dunno about previouse Q but in general with ImageIndex:

for i := 0 to TreeView.Items.Count - 1 do
  with TreeView.Items[0] do begin
    ImageIndex := img_idx;
    SelectedIndex := img_idx
  end;

ziolko.
0
 
ziolkoCommented:
...should be: with TreeView.Items[i] do begin of course

ziolko.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
henryreynoldsAuthor Commented:
Hi Experts

sorry I did not post the previoused link, thank you for all your help.

Thanx ziolko for your input and help.

Henry
0
 
henryreynoldsAuthor Commented:
Hi Experts

sorry I did not post the previoused link, thank you for all your help.

Thanx ziolko for your input and help.

Henry
0
 
ziolkoCommented:
well i think ciuly is on top of this problem :)

ziolko.
0
 
ziolkoCommented:
heh thanks for points mate:)

ziolko.
0
 
henryreynoldsAuthor Commented:
I say thank you !!!!!!

keep well

henry
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.