Board index » delphi » TTreeview, TTreeNode Data Field

TTreeview, TTreeNode Data Field

Hi,
I'm using a TTreeview to store data and i'd like to associate a TStringGrid
to save additional info about some of the nodes. I'm wondering if it was
possible to use the Data property of the node to save a pointer on a
particular row of my TStringGrid.
Not all the nodes have a TStringGrid line associate so i'll check a 'nil'
data property to see if there is some.
Does somebody di such a thing or something similar
Thanks for help
Bob
 

Re:TTreeview, TTreeNode Data Field


In article <975441649.61...@jerusalem.magic.fr>, "Bob" <B...@nowhere.com>
writes:

Quote
>I'm using a TTreeview to store data and i'd like to associate a TStringGrid
>to save additional info about some of the nodes. I'm wondering if it was
>possible to use the Data property of the node to save a pointer on a
>particular row of my TStringGrid.
>Not all the nodes have a TStringGrid line associate so i'll check a 'nil'
>data property to see if there is some.
>Does somebody di such a thing or something similar

All the "Data" or "Object" fields will store any DWord. You just have to
typecast it in or out :-

  TreeView.Items[34].Date := pointer(14);

  StringGrid1.Row := integer(TreeView1.Items[34].Data);

I've recently used it in a stringgrid to hold a calculated index value for
sorting the rows, and to hold an enumerated value (of which the cell content is
a string) to control the colour of the cell.

Similarly for TStringList.Objects or ListBox.Item.Objects but here you would
have to typecast by a TObject going in, and an integer coming out.

Its a neat and powerful technique.

Alan Lloyd
alangll...@aol.com

Re:TTreeview, TTreeNode Data Field


"Bob" <B...@nowhere.com> skrev i melding
news:975441649.61871@jerusalem.magic.fr...

Quote
> Hi,
> I'm using a TTreeview to store data and i'd like to associate a
TStringGrid
> to save additional info about some of the nodes. I'm wondering if it
was
> possible to use the Data property of the node to save a pointer on a
> particular row of my TStringGrid.
> Not all the nodes have a TStringGrid line associate so i'll check a
'nil'
> data property to see if there is some.
> Does somebody di such a thing or something similar

If you want to make it simple, you could typecast the Data pointer to a
record:

TGridRef = packed record
  Col: Word
  Row: Word;
end;

...and store the references to the grid like this:

var
  ARec: TGridRef;
  ANode: TTreeNode;
  HasGridRow: boolean
begin

  // Create ANode
  // Add data to StringGrid1

  if HasGridRow then begin
    ARec.Col:=StringGrid1.Col;
    ARec.Row:=StringGrid1.Row;
    ANode.Data:=Pointer(integer(ARec));
  end;

var
  ARec: TGridRef;
  ANode: TTreeNode;
  HasGridRow: boolean
begin
[...]
  HasGridRow:=ANode.Data <> nil;
  if HasGridRow then begin
    ARec:=TGridRec(integer(ANode.Data));
    StringGrid.Col:=ARec.Col;
    StringGrid.Row:=ARec.Row;
  end;

..i'm not 100% sure whether you need to typecast via integer, or if a
TGridRec will typecast to pointer...
...just beware that if you use a grid with no header, the upper left
cell will be Col=0, Row=0, which, typecasted to a Pointer is...nil. In
that case, bias either Col or Row or Both with 1.

--
Bjoerge Saether
Consultant / Developer
Asker, Norway
bsaether.removet...@online.no (remove the obvious)

Other Threads