Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 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