?
Solved

Can't create TreeNodes while in Create constructor of a TWinControl

Posted on 2004-04-29
5
Medium Priority
?
344 Views
Last Modified: 2010-04-05
I designed a new visual component (TWinComponent) that contains a TreeView. When I create a node in the Create constructor (after initialization of the TreeView), I can't use this Node in latter procedure. i.e. when inserting a new node (Child or Sibling) if works fine in the create constructor but failed (raise various exception) when added in other function. Here is the Example that failed:

TTest = class(TWinControl)
    private
       fTreeView : TTreeView;
       fNode1 : TTreeNode;
       fNode2 : TTreeNode;
    public
       constructor Create(AOwner : TComponent); override;
       destructor Destroy; override;
       procedure Test(AString : String);
    end;

constructor TTest.Create(AOwner: TComponent);
begin
     inherited;
     fTreeView := TTreeView.Create(Self);
     fTreeView.Parent := Self;
     fTreeView.Align := alClient;
     fTreeView.ReadOnly := True;

     fNode1 := fTreeView.Items.Add(nil,'Node1');
     fNode2 := fTreeView.Items.AddChild(fNode1,'Node2'); //This works fine
end;

procedure TTest.Test(AString : String);
var
   ANode : TTreeNode;
begin
       ANode := fTreeView.Items.AddChild(fNode1,AString); // This yield to various Exception
       // ... Perform various operation
end;

Alternatively the following work around works fine.

TTest = class(TWinControl)
    private
       fTreeView : TTreeView;
       fNode1 : TTreeNode;
       fNode2 : TTreeNode;
       fPrepared : Boolean;
       procedure Prepare;
    public
       constructor Create(AOwner : TComponent); override;
       destructor Destroy; override;
       procedure Test(AString : String);
    end;

constructor TTest.Create(AOwner: TComponent);
begin
     inherited;
     fTreeView := TTreeView.Create(Self);
     fTreeView.Parent := Self;
     fTreeView.Align := alClient;
     fTreeView.ReadOnly := True;
     fPrepared := False;
end;

procedure TTest.Test(AString : String);
begin
       fNode1 := fTreeView.Items.Add(nil,'Node1');
       fNode2 := fTreeView.Items.AddChild(fNode1,'Node2'); //This works fine
       fPrepared := true;
end;

procedure TTest.Prepare;
var
   ANode : TTreeNode;
begin
       if not fPrepared then
            Prepare;
       ANode := fTreeView.Items.AddChild(fNode1,AString); // This yield to various Exception
       // ... Perform various operation
end;

Note: the TTest is registered in the VCL Palette and dropped in the form during design.
0
Comment
Question by:mavrj
[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
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:MikProg
ID: 10948790
A piece of code where you call Test and Prepare. (and i think you swap body of them)
0
 
LVL 1

Author Comment

by:mavrj
ID: 10948852
Good point ! Sorry for the mistake. Tried to make it to simple :-)

Read:

procedure TTest.Prepare;
begin
       fNode1 := fTreeView.Items.Add(nil,'Node1');
       fNode2 := fTreeView.Items.AddChild(fNode1,'Node2'); //This works fine
       fPrepared := true;
end;

procedure TTest.Test(AString : String);
var
   ANode : TTreeNode;
begin
       if not fPrepared then
            Prepare;
       ANode := fTreeView.Items.AddChild(fNode1,AString); // This yield to various Exception
       // ... Perform various operation
end;
0
 
LVL 17

Accepted Solution

by:
geobul earned 1000 total points
ID: 10952126
Hi,

Perhaps the reason is in the streaming mechanism when you are saving/loading your component.

1. When you drop your component on a form these two nodes are created by the constructor(design-time).
2. When you save/run your project these two nodes are already there (created automatically because they were saved/streamed) and
fNode1 := fTreeView.Items.Add(nil,'Node1');
actually fails to add a new node and fNode1 remaims unititialized.
3. When you execute your Test method it raises an AV because fNode1 is nil (or points somewhere else).

I might be wrong, of course.

Regards, Geo
0
 
LVL 3

Expert Comment

by:MikProg
ID: 10956324
geobul>You forget to write solution code

TTest = class(TWinControl)
    private
       fTreeView : TTreeView;
       fNode1 : TTreeNode;
       fNode2 : TTreeNode;
    protected
       procedure Loaded; override;
    public
       constructor Create(AOwner : TComponent); override;
       destructor Destroy; override;
       procedure Test(AString : String);
    end


procedure TTest.Loaded;
begin
   fNode1:=fTreeView.Items.GetFirstNode;
   fNode2:=fNode2.GetFirstChild;
end;
0
 
LVL 17

Expert Comment

by:geobul
ID: 10956777
Yes, exactly - overriding Loaded is the way to go. I had no time for testing yesterday (last night actually :-).
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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