Solved

Delphi 7 :: Duplicate a TTreeView with Images Node.Data

Posted on 2006-11-13
4
412 Views
Last Modified: 2010-04-05
Hi Experts,

I have a TTreeView which I populate from a database.

Now I need to create a second TTreeView in code exactly the same as the first
but I dont want to populate it again from the database. I believe there should be a
benefit in processing time if I can only copy the original database.
But I need to copy it complete with images and data property as well.

Any advise?
0
Comment
Question by:Marius0188
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17934816
This will copy all nodes from one treeview and their properties to another.
Just make sure you assign the same image list to both treeviews so that the images are the same.

TreeView2.Items.Assign( TreeView1.Items)
0
 

Author Comment

by:Marius0188
ID: 17934902
And what about the Node's Data value?
Also?
0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 500 total points
ID: 17936757
It will copy any of the data information over as well.
Here is a really basic example just to show you.


unit Unit1;

interface

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

type
  TMyRec = record
    ID: Integer;
  end;

  ptrObj = ^TMyRec;

  TForm1 = class(TForm)
    TreeView1: TTreeView;
    TreeView2: TTreeView;
    PopupMenu1: TPopupMenu;
    ShowNodeID1: TMenuItem;
    StatusBar1: TStatusBar;
    Button1: TButton;
    procedure FormShow(Sender: TObject);
    procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
    procedure TreeView2Change(Sender: TObject; Node: TTreeNode);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
var
  Node: TTreeNode;
  i: Integer;
  ptrTemp: ptrObj;
begin
  for i := 0 to 9 do
  begin
    New( ptrTemp);
    ptrTemp^.ID := i*i;
    Node := TreeView1.Items.AddObject(nil, IntToStr(i), ptrTemp);
  end
end;

procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
  i: Integer;
begin
  if (TreeView1.Selected = nil) then Exit;

  i :=  TMyRec( Node.Data^).ID;

  StatusBar1.Panels[0].Text := 'Treeview1 selected id = ' + IntToStr( i);
end;

procedure TForm1.TreeView2Change(Sender: TObject; Node: TTreeNode);
var
  i: Integer;
begin
  if (TreeView2.Selected = nil) then Exit;

  i :=  TMyRec( Node.Data^).ID;

  StatusBar1.Panels[1].Text := 'Treeview2 selected id = ' + IntToStr( i);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TreeView2.Items.Assign( TreeView1.Items);
end;

end.



object Form1: TForm1
  Left = 260
  Top = 160
  Width = 541
  Height = 448
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object TreeView1: TTreeView
    Left = 96
    Top = 56
    Width = 121
    Height = 257
    Indent = 19
    PopupMenu = PopupMenu1
    TabOrder = 0
    OnChange = TreeView1Change
  end
  object TreeView2: TTreeView
    Left = 296
    Top = 56
    Width = 121
    Height = 257
    Indent = 19
    PopupMenu = PopupMenu1
    TabOrder = 1
    OnChange = TreeView2Change
  end
  object StatusBar1: TStatusBar
    Left = 0
    Top = 395
    Width = 533
    Height = 19
    Panels = <
      item
        Width = 250
      end
      item
        Width = 250
      end>
  end
  object Button1: TButton
    Left = 216
    Top = 336
    Width = 75
    Height = 25
    Caption = 'Copy Tree'
    TabOrder = 3
    OnClick = Button1Click
  end
  object PopupMenu1: TPopupMenu
    Left = 248
    Top = 176
    object ShowNodeID1: TMenuItem
      Caption = 'Show Node ID'
    end
  end
end
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17936762
Ignore the popup component on the form, I was going to do it another way.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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