Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add items to a TreeView.

Posted on 2006-07-11
10
Medium Priority
?
478 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

715 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