Board index » delphi » Sub-classing TTreeNode for use with TCustomTreeView

Sub-classing TTreeNode for use with TCustomTreeView

I'm working on a tree object browser, and thought that the best way to
encapsulate the different actions for nodes in the tree would be to
create subclasses for each of the different types of nodes.  In
practice I'm finding this does not work since TCustomTreeView seems to
regard every node as a bog standard TTreeNode.  For example:

  The OnChanged Node variable always seems to have
  ClassType() = TTreeNode, not the subclasses I specified when
  creating those nodes. I cannot override ClassType() because
  it is static, so I guess that's the reason.

  Similarly, it is impossible to cast the Node variable returned
  as any of the subclasses, and so invoke their methods.

I'm coming to the conclusion that this just cannot work in Delphi, but
what do you think?  Can you recommend an elegant way of encapsulating
the different behaviours for my various tree node types?

Jeremy Rickard

 

Re:Sub-classing TTreeNode for use with TCustomTreeView


In article <d36116ef.0209081618.726b6...@posting.google.com>,

Quote
jrick...@unisystems.biz (Jeremy Rickard) writes:
>I'm working on a tree object browser, and thought that the best way to
>encapsulate the different actions for nodes in the tree would be to
>create subclasses for each of the different types of nodes.  In
>practice I'm finding this does not work since TCustomTreeView seems to
>regard every node as a bog standard TTreeNode.

Do you ...

1 Code a descendant of TTreeNode encapsulating your desired changes to the tree
node.

2 Code a descendant (if necessary) of TTreeView encapsulating the differences
(if any) you need to cope with the TTreeNode descendant.

3 Create the TTreeview by calling MyTreeView :=
TMyTreeview.Create(TMyTreeNode);

The following was a note I had found on changing TTreeNode characteristics ...

"Adding Extra Properties to TreeView Nodes
unit mytreeview;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, CommCtrl;

type
  TMyTreeNode = class(TTreeNode)
  private
    fDataString: String;
  public
    constructor Create(AOwner: TTreeNodes);
    property DataString: string read fDataString write fDataString;
  end;

  TMyTreeview = class(TTreeView)
  public
    function  CreateNode: TTreeNode; override;
  end;

  procedure Register;

implementation

function TMyTreeView.CreateNode: TTreeNode;
begin
  Result := TMyTreeNode.Create(Items);
end;

constructor TMyTreeNode.Create(AOwner: TTreeNodes);
begin
  inherited Create(AOwner);
  fDataString := '';
end;

procedure Register;
begin
  RegisterComponents('Samples', [TMyTreeView]);
end;

end."

Alan Lloyd
alangll...@aol.com

Re:Sub-classing TTreeNode for use with TCustomTreeView


"Jeremy Rickard" <jrick...@unisystems.biz> skrev i melding
news:d36116ef.0209081618.726b6110@posting.google.com...

Quote
> I'm working on a tree object browser, and thought that the best way to
> encapsulate the different actions for nodes in the tree would be to
> create subclasses for each of the different types of nodes.  In
> practice I'm finding this does not work since TCustomTreeView seems to
> regard every node as a bog standard TTreeNode.  For example:

>   The OnChanged Node variable always seems to have
>   ClassType() = TTreeNode, not the subclasses I specified when
>   creating those nodes. I cannot override ClassType() because
>   it is static, so I guess that's the reason.

method ClassType; returns the *actual* class table of any object. It's
static, but that doesn't mean the implementing class controls what class
type is returned.

Quote
>   Similarly, it is impossible to cast the Node variable returned
>   as any of the subclasses, and so invoke their methods.

That may be caused by the fact that regular TTreeNode's are actually
created.

Quote
> I'm coming to the conclusion that this just cannot work in Delphi, but
> what do you think?  Can you recommend an elegant way of encapsulating
> the different behaviours for my various tree node types?

The method you need to override, is the following;

    function TCustomTreeView.CreateNode: TTreeNode; virtual;

..to make it create a node of your class type:

function TMyTreeView.CreateNode: TTreeNode;
begin
  Result := TMyTreeNode.Create(Items);
end;

--
Bj?rge S?ther
bjorge@hahaha_itte.no

Re:Sub-classing TTreeNode for use with TCustomTreeView


Thank you both for your replies, which seemed to identify overriding
CreateNode as the key to the solution.  This wasn't something I had
imagined necessary, as I had hoped that AddNode would preserve the
instance of the subclass derived from TTreeNode (rather than
converting it back to a plain TTreeNode, as seems to be the case - or
maybe I've misunderstood).

Bottom line is I've obviously some learning to do about the Delphi
object model.  I'll play around with your suggestions over the coming
weekend, and see how far that progresses me.

Thanks again,

Jeremy Rickard

Other Threads