• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

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

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
Marius0188
Asked:
Marius0188
  • 3
1 Solution
 
mikelittlewoodCommented:
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
 
Marius0188Author Commented:
And what about the Node's Data value?
Also?
0
 
mikelittlewoodCommented:
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
 
mikelittlewoodCommented:
Ignore the popup component on the form, I was going to do it another way.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now