Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-11-13
4
Medium Priority
?
427 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

650 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