Solved

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

Posted on 2007-03-20
8
1,639 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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