Raymond
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
| |__ 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
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(TreeV iew1.Selec ted.Index) );
GetIndexStr(TreeView1.Sele cted, MyStr);
ShowMessage(MyStr);
end;
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
Var
MyStr: String;
Procedure GetIndexStr(MyNode: TTreeNode; Var MyIndexStr:String);
begin
If MyNode.Parent <> nil then
GetIndexStr(MyNode.Parent,
Else
MyIndexStr := '';
MyIndexStr := MyIndexStr +','+inttostr(MyNode.index
end;
begin
ShowMessage(IntToStr(TreeV
GetIndexStr(TreeView1.Sele
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(TreeV iew1.Selec ted.Index) );
GetIndexAry(TreeView1.Sele cted, MyAry);
ListBox1.Items.Clear;
For i := low(MyAry) To High(MyAry) Do
Begin
ListBox1.Items.Add(inttost r(MyAry[i] ));
End;
end;
end.
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
Var
MyStr: String;
MyAry: AOI;
i: Integer;
Procedure GetIndexAry(MyNode: TTreeNode; Var Ary: AOI);
begin
If MyNode.Parent <> nil then
GetIndexAry(MyNode.Parent,
Else
SetLength(Ary, 0);
SetLength(Ary, Length(Ary) + 1);
Ary[High(Ary)] := MyNode.Index;
end;
begin
// ShowMessage(IntToStr(TreeV
GetIndexAry(TreeView1.Sele
ListBox1.Items.Clear;
For i := low(MyAry) To High(MyAry) Do
Begin
ListBox1.Items.Add(inttost
End;
end;
end.
use absoluteIndex instead index to get an unique index
ASKER
kretzschmar:
Could you please give me an example on how to use the "AbsoluteIndex" ?
raymng
Could you please give me an example on how to use the "AbsoluteIndex" ?
raymng
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.
how do you do evaluate the index