Solved

Add items to a TreeView.

Posted on 2006-07-11
10
469 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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