Solved

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

Posted on 2007-03-20
8
1,637 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:henryreynolds
  • 4
  • 3
8 Comments
 
LVL 21

Assisted Solution

by:ziolko
ziolko earned 125 total points
ID: 18755009
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
 
LVL 21

Expert Comment

by:ziolko
ID: 18755014
...should be: with TreeView.Items[i] do begin of course

ziolko.
0
 
LVL 28

Accepted Solution

by:
2266180 earned 125 total points
ID: 18755083
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
 

Author Comment

by:henryreynolds
ID: 18755122
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:henryreynolds
ID: 18755126
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
 
LVL 21

Expert Comment

by:ziolko
ID: 18755137
well i think ciuly is on top of this problem :)

ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18755172
heh thanks for points mate:)

ziolko.
0
 

Author Comment

by:henryreynolds
ID: 18756045
I say thank you !!!!!!

keep well

henry
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ddeman not working in activex 3 92
Help on project with Soap 10 47
find a node in VST 2 63
Create a path if not exists 7 68
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now