Recent

Author Topic: How to add new property to TreeView  (Read 8914 times)

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
How to add new property to TreeView
« on: May 08, 2011, 04:28:10 pm »
Hello,

I need to add my own property to TreeView for each TTreeNode, that i would use like this:
Code: [Select]
TreeView1.Selected.MyProperty := 'some string';
What should i do? Is it possible?
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: How to add new property to TreeView
« Reply #1 on: May 08, 2011, 04:37:50 pm »
You can use TTreeNode.Data property to store any kind of object

Or:

Create your own node:

Code: Pascal  [Select][+][-]
  1. TMyNode = class(TTreeNode)
  2. private
  3.   FMyProperty: String;
  4. public
  5.   property MyProperty: String read FMyProperty write FMyProperty
  6. end;


Then in treeview OnCreateNodeClass do:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.TreeView1CreateNodeClass(Sender: TCustomTreeView;
  2.   var NodeClass: TTreeNodeClass);
  3. begin
  4.   NodeClass := TMyNode;
  5. end;

Finally cast selected node to your class:

TMyNode(TreeView1.Selected).MyProperty := 'test';

If you don't want cast then you need create own descendant of TreeView like:

Code: Pascal  [Select][+][-]
  1. TMyTreeView = class(TTreeView)
  2.   private
  3.     function GetMyNode: TMyNode;
  4.   public
  5.     property MySelected: TMyNode read GetMyNode;
  6.   end;  
  7.  
  8. function TMyTreeView.GetMyNode: TMyNode;
  9. begin
  10.   Result := TMyNode(Selected);
  11. end;  

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: How to add new property to TreeView
« Reply #2 on: May 08, 2011, 04:47:03 pm »
Thank you for really fast reply! :)

It works perfectly!
I just wanna ask, can i make my own component based on TreeView with my custom property "MyProperty: string"? I mean this new component would have all the same as "old" TreeView but plus this new property. Is it possible? What should i do to make it happen?

Thanks for your time, i'm just beginner.
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: How to add new property to TreeView
« Reply #3 on: May 08, 2011, 05:01:53 pm »
Yes you can:

Code: Pascal  [Select][+][-]
  1. TMyTreeView = class(TTreeView)
  2.   private
  3.     function GetMyNode: TMyNode;
  4.   public
  5.     property MySelected: TMyNode read GetMyNode;
  6.   end;
  7.  
  8. function TMyTreeView.GetMyNode: TMyNode;
  9. begin
  10.   Result := TMyNode(Selected);
  11. end;

This inherit all functionality from original TTreeView + yours. But with this way you can create your own component only in runtime like:

Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     procedure FormCreate(Sender: TObject);
  3.   private
  4.     { private declarations }
  5.     FMyTreeView: TMyTreeView;
  6.   public
  7.     { public declarations }
  8.   end;
  9.  
  10. procedure TForm1.FormCreate(Sender: TObject);
  11. begin
  12.   FMyTreeView := TMyTreeView.Create(Self);
  13.   FMyTreeView.Parent := Self;
  14. end;

If you want have your component in lazarus components palette and create at design time then you should read how to create lazarus components:
http://wiki.lazarus.freepascal.org/How_To_Write_Lazarus_Component

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: How to add new property to TreeView
« Reply #4 on: May 08, 2011, 05:04:12 pm »
Thank you very much! :) You have helped me a lot. Thanks! :)
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

GeorgeBaker

  • Newbie
  • Posts: 2
Re: How to add new property to TreeView
« Reply #5 on: May 09, 2011, 09:49:57 pm »
...

There is one problem with this concept - what if I want to use just default TTreeView functions and not my own (like GetMyNode)?

Like Manny wrote:
Code: [Select]
TreeView1.Selected.MyProperty := 'some string'; - he want to use original "Selected" property.

I have an idea of making my own TTreeNode (TMyNode with MyProperty) + change definitions in TTreeView and TTreeNodes and force them to use TMyNode instead of TTreeNode...
Simpliest way to do this is just modify code of TTreeNode (in comctrls) but this is little bit creepy, so the real question is how to do this in my own new (TMyTreeView) component.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: How to add new property to TreeView
« Reply #6 on: May 09, 2011, 10:48:36 pm »
Or you could use Virtual Treeview. It has all functionality you'll ever need (probably)  :D
http://wiki.lazarus.freepascal.org/VirtualTreeview
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: How to add new property to TreeView
« Reply #7 on: May 09, 2011, 11:40:31 pm »
...

There is one problem with this concept - what if I want to use just default TTreeView functions and not my own (like GetMyNode)?

Like Manny wrote:
Code: [Select]
TreeView1.Selected.MyProperty := 'some string'; - he want to use original "Selected" property.

I have an idea of making my own TTreeNode (TMyNode with MyProperty) + change definitions in TTreeView and TTreeNodes and force them to use TMyNode instead of TTreeNode...
Simpliest way to do this is just modify code of TTreeNode (in comctrls) but this is little bit creepy, so the real question is how to do this in my own new (TMyTreeView) component.
Well, editing existing code is known solution but can we call this as "solution"? I purposely did not write about it because it is not ethical and broke "inheritance" idea. What if you forget to do backup of all your modified lazarus sources? What if you want send this component to friend? Maybe exists some way to override "Selected" class from TTreeNode to TMyTreeNode but I doubt.
« Last Edit: May 09, 2011, 11:43:10 pm by Dibo »

M[a]nny

  • Full Member
  • ***
  • Posts: 130
  • Dreamer
Re: How to add new property to TreeView
« Reply #8 on: May 10, 2011, 12:24:05 am »
I totaly agree but is there any other way to achieve it? It looks harder than it seemed. I just want to add one property to TTreeNode...
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

 

TinyPortal © 2005-2018