Solved

Add items to a TreeView.

Posted on 2006-07-11
10
466 Views
Last Modified: 2010-04-05
Hi.

I want to add some child items to a TreeView.

The TreeView looks like this :
--------------------------
My Pc
 |- C1
 |- C2
 |- C3
 |- C4
--------------------------

I add items using :

For example to add some string in C2
TreeView1.Items.Addchild(TreeView1.Items[2],text)

But how can i know the index when it changes dynamically ?
Is there a way to add childs by the caption of the node ?

For example :

TreeView1.Items.Addchild( C1 , Some child name)
TreeView1.Items.Addchild( C2 , Other child name)

Thanks in advance.
0
Comment
Question by:CodedK
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 250 total points
ID: 17081324
you can use search for it

for i:=1 to treeview1.Items.count do
  if treeview1.Items[i-1].text=searchedcaption then c:=treeview1.Items[i-1];

then
TreeView1.Items.Addchild( c , Some child name);

you can also search by the itemid (or use getnode(itemid) but you will have to store the idemid somewhere
0
 
LVL 16

Author Comment

by:CodedK
ID: 17081357
:) Thanks ciuly... again...

I just found another way... I could add the items from the bottom to the top.

Anyway could you PLEASE tell me how can i get the selected node caption ?

Its the first time i work with treevies and i'll flood EE with stupid quetions :P
0
 
LVL 28

Expert Comment

by:2266180
ID: 17081370
treeview1.Selected.Text ;)
0
 
LVL 16

Author Comment

by:CodedK
ID: 17081373
Ok Lol thanks... :P
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17081421
The function ItemByPath below can be used to pass a path, like "My PC\C1" and return the matching node. That way, if you have nodes with same captions, but different levels, the correct item still gets returned.

Regards,
Russell


function GetPathString(var Str: PChar): String;
var  lpszScan:      PChar;
begin

  // Check passed string
  if (Str = nil) or (Str^ = #0) then
     // Empty string
     SetLength(result, 0)
  else
  begin
     // Scan for path seperator
     lpszScan:=StrScan(Str, '\');
     // Check sep char
     if Assigned(lpszScan) then
     begin
        // Get path string
        SetString(result, Str, lpszScan-Str);
        // Set to sep char position
        Str:=lpszScan;
        // Push next
        Inc(Str);
     end
     else
     begin
        // Return whole string
        SetString(result, Str, StrLen(Str));
        // Set to end of string
        Str:=StrEnd(Str);
     end;
  end;

end;

function ItemByPath(Tree: TTreeView; Path: String): TTreeNode;
var  tnChild:       TTreeNode;
     lpszPath:      PChar;
     szPath:        String;
begin

  // Check path and tree item count
  if (Length(Path) = 0) or (Tree.Items.Count = 0) then
     // No item to return
     result:=nil
  else
  begin
     // Cast path
     lpszPath:=Pointer(Path);
     // Check against root
     if (CompareText(GetPathString(lpszPath), Tree.Items[0].Text) = 0) then
     begin
        // Get root item
        result:=Tree.Items[0];
        // While path is not null and node has children
        while (lpszPath^ > #0) and result.HasChildren do
        begin
           // Get next item path
           szPath:=GetPathString(lpszPath);
           // Get first child
           tnChild:=result.getFirstChild;
           // Walk the children
           while Assigned(tnChild) do
           begin
              // Check text, break if found
              if (CompareText(szPath, tnChild.Text) = 0) then break;
              // Next child
              tnChild:=tnChild.getNextSibling;
           end;
           // Break if no child
           if (tnChild = nil) then break;
           // Update result
           result:=tnChild;
        end;
     end;
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  TreeView1.Items.AddChild(ItemByPath(TreeView1, 'My PC\C1'), 'Some child name');
  TreeView1.Items.AddChild(ItemByPath(TreeView1, 'My PC\C2'), 'Some child name');

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 26

Expert Comment

by:Russell Libby
ID: 17081429
Never mind...
0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17081447
When working with TreeViews...get out of the idea of thinking about Index...matter of fact I would do my best not to use this property, other than finding ...
if you need to tranverse the Nodes...use Get and GetNext...if you need to transverse the Parents use GetNextSibling

aRootNode := TTreeView.Items[0]
aParentNode := TTreeView.Items.GetNextSibling;
if Assigned(aParentNode) then
begin
  aChildNode := aParentNode.GetFirstChild;
  if Assigned(aChildNode) then
  //do my code here...




0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17081454
should be

other than finding ...the Root Node;
0
 
LVL 16

Author Comment

by:CodedK
ID: 17081486
:)

Thanks for all the suggestions...

Can anyone tell me please how can i insert an OnClick event to subchild
when i dont know the index nor the name ?
0
 
LVL 16

Author Comment

by:CodedK
ID: 17081507
Nevermind i got it i think.
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

Title # Comments Views Activity
loading dll from external path 1 213
Printing problem 2 81
Delphi XE10 Round Image 2 79
Delphi inherited method 6 63
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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…

863 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

20 Experts available now in Live!

Get 1:1 Help Now