Solved

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

Posted on 2007-03-20
8
1,634 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:
ciuly 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

22 Experts available now in Live!

Get 1:1 Help Now