Link to home
Start Free TrialLog in
Avatar of Raymond
RaymondFlag for Hong Kong

asked on

Index of TTreeView's SubItem

+ABC
|    |__ SubA
|    |__ SubB
|
+XYZ
     |__ SubX
     |__ SubY

When item "ABC" is selected, TTreeView.Selected.Index returns 0; when SubItem "SubA" or "SubB" is selected, it also returns 0 !!  So how can I differentiate which item/subitem is being selected ??

Thank You.

raymng
Avatar of kretzschmar
kretzschmar
Flag of Germany image

this could not be

how do you do evaluate the index
Avatar of snehanshu
snehanshu

raymng,
  You would have to recursively traverse the parents of the selected treenode till you get a nil parent. Something like this (Where the GetIndexStr procedure will build a comma-separated index string for you):

procedure TForm1.Button1Click(Sender: TObject);
Var
  MyStr: String;

Procedure GetIndexStr(MyNode: TTreeNode; Var MyIndexStr:String);
begin
 If MyNode.Parent <> nil then
   GetIndexStr(MyNode.Parent, MyIndexStr)
 Else
   MyIndexStr := '';
 MyIndexStr := MyIndexStr +','+inttostr(MyNode.index);

end;

begin

  ShowMessage(IntToStr(TreeView1.Selected.Index));
  GetIndexStr(TreeView1.Selected, MyStr);
  ShowMessage(MyStr);

end;
Or you could get the indexes in an array like:

unit Unit1;

interface

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

type
  AOI = Array Of Integer;
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
Var
  MyStr: String;
  MyAry: AOI;
  i: Integer;

Procedure GetIndexAry(MyNode: TTreeNode; Var Ary: AOI);
begin
 If MyNode.Parent <> nil then
   GetIndexAry(MyNode.Parent, Ary)
 Else
   SetLength(Ary, 0);

 SetLength(Ary, Length(Ary) + 1);
 Ary[High(Ary)] := MyNode.Index;

end;


begin

//  ShowMessage(IntToStr(TreeView1.Selected.Index));
  GetIndexAry(TreeView1.Selected, MyAry);
  ListBox1.Items.Clear;
  For i := low(MyAry) To High(MyAry) Do
  Begin
    ListBox1.Items.Add(inttostr(MyAry[i]));
  End;



end;

end.
use absoluteIndex instead index to get an unique index
Avatar of Raymond

ASKER

kretzschmar:

Could you please give me an example on how to use the "AbsoluteIndex" ?

raymng
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Raymond

ASKER

Thanks.