Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:37:19+00:00 2026-06-01T09:37:19+00:00

Im trying to make my custom TTreeNode Class for example TCustomTreeNode = class(TTreeNode) private

  • 0

Im trying to make my custom TTreeNode Class

for example

TCustomTreeNode = class(TTreeNode)
  private
  public
    Comment:string;
  end;

and i create and add the node in the tree view like this:

var
  NewCustomTreeNode:TCustomTreeNode;
begin
  NewCustomTreeNode:= TCustomTreeNode.Create(TreeView.Items);
  NewCustomTreeNode.Comment:='blqblq';
  TreeView.Items.AddChild(NewCustomTreeNode,'NodeText');

and when i try to access the custom created tree nodes error pops up. For example i do like this:

TCustomTreeNode(TreeNode).Comment:='asdadssadas';

plase help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:37:21+00:00Added an answer on June 1, 2026 at 9:37 am

    The problem with your code is that the call to AddChild results in the tree view creating a new node. And since you didn’t tell the tree view to create a node of your sub-class it creates a plain TTreeNode. And then when you try to cast it to a TCustomTreeNode, the world ends.

    You need to use the OnCreateNodeClass method to make sure that the tree view is able to create new nodes. Like this:

    type
      TCustomTreeNode = class(TTreeNode)
      protected
        procedure Assign(Source: TPersistent); override;
      public
        Comment: string;
      end;
    
    procedure TCustomTreeNode.Assign(Source: TPersistent);
    begin
      if Source is TCustomTreeNode then
        Comment := TCustomTreeNode(Source).Comment;
      inherited;
    end;
    
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      NewNode: TCustomTreeNode;
    begin
      NewNode := TreeView1.Items.Add(nil, 'Node1') as TCustomTreeNode;
      NewNode.Comment := 'A comment';
      NewNode := TreeView1.Items.Add(nil, 'Node2') as TCustomTreeNode;
      NewNode.Comment := 'Another comment';
    end;
    
    procedure TForm1.TreeView1Click(Sender: TObject);
    var
      Node: TCustomTreeNode;
    begin
      Node := TreeView1.Selected as TCustomTreeNode;
      if Assigned(Node) then
        ShowMessage(Node.Comment);
    end;
    
    procedure TForm1.TreeView1CreateNodeClass(Sender: TCustomTreeView; var NodeClass: TTreeNodeClass);
    begin
      NodeClass := TCustomTreeNode;
    end;
    

    I can’t claim to being the world’s greatest expert on Delphi tree views but in my experience you never create a tree node yourself. You should always call one of the AddXXX methods on TTreeView.Items to create new nodes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m trying to make a custom component to javafx, so I make my class
i'm trying to make a custom Gallery in my app. I created a class
I'm trying to make a custom class to hold data in my iPhone app.
I am trying to make Custom RelativeLayout which can scale and scroll. Right now
Trying to make a custom :confirm message for a rails form that returns data
I'm trying to make a custom view in Django admin. I'm reading from this
I'm trying to make a custom infowindow. Version = 2. I have added my
I am trying to make a custom Flex tooltip a la this tutorial .
I'm trying to make a custom converter that inherits from DependencyObject , but it
I am trying to make a custom dialog in Android. Everything works perfectly except

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.