Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Add items to a TreeView.

Posted on 2006-07-11
10
Medium Priority
?
482 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

963 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