Solved

Add items to a TreeView.

Posted on 2006-07-11
10
462 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:
ciuly 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:ciuly
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

13 Experts available now in Live!

Get 1:1 Help Now