Solved

Index of TTreeView's SubItem

Posted on 2003-11-11
7
296 Views
Last Modified: 2010-04-05
+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
0
Comment
Question by:Raymond
  • 3
  • 2
  • 2
7 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9721033
this could not be

how do you do evaluate the index
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9721132
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;
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9721169
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.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 9721172
use absoluteIndex instead index to get an unique index
0
 

Author Comment

by:Raymond
ID: 9721278
kretzschmar:

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

raymng
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 150 total points
ID: 9721299
well, its simple

procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage(inttostr(treeview1.selected.absoluteindex));
end;

meikl ;-)
0
 

Author Comment

by:Raymond
ID: 9721541
Thanks.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question