Board index » delphi » Please help for TTreeNode

Please help for TTreeNode

Hello all

I create a TreeView and add some nodes of String into it then save it into
file by this code:

=========================================
    TreeNode1 := Add(nil, 'RootNode1');
    AddChild(TreeNode1,'StringNode1);
    AddChild(TreeNode1,'StringNode1');

    TreeNode2 := Add(TreeNode1, 'RootNode2');
    AddChild(TreeNode2,'String1');
    AddChild(TreeNode2,'String2');
    AddChild(TreeNode2,'String3');
    AddChild(TreeNode2,'String4');
    AddChild(TreeNode2,'String5');
 ....
TreeView1.SaveToFile('c:\node.dat');
==========================================

Then the my file will have structure as:

RootNode1
   StringNode1
   StringNode2
RootNode2
   String1
   String2
   String3
   String4
   String5

Now I have a string called myString. What I have to do is if myString is
equal with Strings in RootNode1, do action 1. If myString is equal with any
of String in RootNode2, do action 2.

Could you please help me some Guides do that. Do I have open the file and
have to use search function here ???

Thank you for your help
Nhuyen

http://www.zfree.co.nz

 

Re:Please help for TTreeNode


You can search the TreeView with:

const
  MyString = 'RootNode2';
var
  i: Integer;
begin
  // for all nodes do
  for i := 0 to TreeView1.Items.Count - 1 do
    // if same caption and root node
    if (TreeView1.Items[i].Caption = MyString) and
      (TreeView1.Items[i].Level = 0) then
    begin
      // do something with node
      Break;
    end;
end;

Quote
"MeAgain" <sonphu...@zfree.co.nz> wrote in message

news:3b70d2b2$1@zfree.co.nz...
Quote

> Hello all

> I create a TreeView and add some nodes of String into it then save it into
> file by this code:

> =========================================
>     TreeNode1 := Add(nil, 'RootNode1');
>     AddChild(TreeNode1,'StringNode1);
>     AddChild(TreeNode1,'StringNode1');

>     TreeNode2 := Add(TreeNode1, 'RootNode2');
>     AddChild(TreeNode2,'String1');
>     AddChild(TreeNode2,'String2');
>     AddChild(TreeNode2,'String3');
>     AddChild(TreeNode2,'String4');
>     AddChild(TreeNode2,'String5');
>  ....
> TreeView1.SaveToFile('c:\node.dat');
> ==========================================

> Then the my file will have structure as:

> RootNode1
>    StringNode1
>    StringNode2
> RootNode2
>    String1
>    String2
>    String3
>    String4
>    String5

> Now I have a string called myString. What I have to do is if myString is
> equal with Strings in RootNode1, do action 1. If myString is equal with
any
> of String in RootNode2, do action 2.

> Could you please help me some Guides do that. Do I have open the file and
> have to use search function here ???

> Thank you for your help
> Nhuyen

> http://www.zfree.co.nz

Re:Please help for TTreeNode


Thank you for your help

But your method can not recognize a child node is one of RootNode2.
Where is myString so that I can compare ???

Quote
"M.H. Avegaart" <avega...@NOSPAMmccomm.nl> wrote:
>You can search the TreeView with:

>const
>  MyString = 'RootNode2';
>var
>  i: Integer;
>begin
>  // for all nodes do
>  for i := 0 to TreeView1.Items.Count - 1 do
>    // if same caption and root node
>    if (TreeView1.Items[i].Caption = MyString) and
>      (TreeView1.Items[i].Level = 0) then
>    begin
>      // do something with node
>      Break;
>    end;
>end;

>"MeAgain" <sonphu...@zfree.co.nz> wrote in message
>news:3b70d2b2$1@zfree.co.nz...

>> Hello all

>> I create a TreeView and add some nodes of String into it then save it
into
>> file by this code:

>> =========================================
>>     TreeNode1 := Add(nil, 'RootNode1');
>>     AddChild(TreeNode1,'StringNode1);
>>     AddChild(TreeNode1,'StringNode1');

>>     TreeNode2 := Add(TreeNode1, 'RootNode2');
>>     AddChild(TreeNode2,'String1');
>>     AddChild(TreeNode2,'String2');
>>     AddChild(TreeNode2,'String3');
>>     AddChild(TreeNode2,'String4');
>>     AddChild(TreeNode2,'String5');
>>  ....
>> TreeView1.SaveToFile('c:\node.dat');
>> ==========================================

>> Then the my file will have structure as:

>> RootNode1
>>    StringNode1
>>    StringNode2
>> RootNode2
>>    String1
>>    String2
>>    String3
>>    String4
>>    String5

>> Now I have a string called myString. What I have to do is if myString
is
>> equal with Strings in RootNode1, do action 1. If myString is equal with
>any
>> of String in RootNode2, do action 2.

>> Could you please help me some Guides do that. Do I have open the file
and
>> have to use search function here ???

>> Thank you for your help
>> Nhuyen

>> http://www.zfree.co.nz

http://www.zfree.co.nz

Re:Please help for TTreeNode


Quote
In article <3b70d2b...@zfree.co.nz>, "MeAgain" <sonphu...@zfree.co.nz> writes:
>Now I have a string called myString. What I have to do is if myString is
>equal with Strings in RootNode1, do action 1. If myString is equal with any
>of String in RootNode2, do action 2.

You can get the root node as follows ...

function TForm1.GetFoundParent(Caption : string) : TTreeNode;
{returns parent of found node which has Caption as Caption, or nil if not
found}
var
  TN : TTreeNode;
begin
  Result := nil;
  {get the first node}
  TN := TreeView1.Items.GetFirstNode;
  while TN <> nil do  // while not at end of TreeNodes list
    if (TN.Text = Caption) and (TN.Level = 1) then
      Break
    else
      TN := TN.GetNext; // get the next node
  if TN <> nil then
    Result := TN.Parent;
end;

Then use it like ...

var
  TN : TTreeNode;
begin
  TN := GetFoundParent('This String')
  if TN <> nil then
    if (TN.Text = 'RootNode1') then
      // do RootNode1 stuff
    else
      // do RootNode2 stuff

Note that using TTreeView.Items.GetFirstNode and TTreeNode.GetNext is much
quicker than using TTreeView.Items[i].

Alan Lloyd
alangll...@aol.com

Re:Please help for TTreeNode


There exists a new improved TreeView component named "AczTreeView"
with powerful saving and loading procedures. Using AczTreeView, you
can save not only the treenodes of a tree but also all other
information attatched to every tree node by yourself.

      AczTreeView is a data-aware TreeView component for Delphi. It
will automatically attach a unique ID of integer type to every tree
node.

      A tree node's ID is useful for accessing detailed information
about the tree node in one of your own database.

      AczTreeView provides special procedures to save the entire tree
with all tree nodes' unique IDs, expanded status ,imageindex and
selectedindex to a specified file and to load the entire tree from a
file as same as it was saved.

      AcztreeView allows you to get any tree node's ID and its
parent's ID in the AczTreeView. It also allows you to get the entire
path from the root of any tree node .

      You can download AczTreeView component at
http://www.qwerks.com/Product.asp?ProductID=3623

Other Threads