Solved

Index of TTreeView's SubItem

Posted on 2003-11-11
7
294 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:raymng
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

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

Author Comment

by:raymng
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:raymng
ID: 9721541
Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

896 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now