Solved

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

Posted on 2006-11-13
4
374 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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.

744 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

11 Experts available now in Live!

Get 1:1 Help Now