TreeView items clear..

Hi.

I have a TreeView with several items. Some of them added dynamically.

In case something changes i'd like to add a refresh option and recalculate everything.
BUT i dont want to clear all the items ... Just the ones that were added dynamically.

How can i do that ?

Example:
~-~-~-~-~-~-~-~-~-~-~-~-~-~-
A (should stay here)
   |
   A1 (Should stay here)
     |
    value (Should go)
 
B (should stay here)
       |
       B1 (Should go)
       |
       B2 (Should go)
       |
       B3 (Should go)
       |
       B4 (Should go)
~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Thanks in advance.
LVL 16
CodedKAsked:
Who is Participating?
 
2266180Connect With a Mentor Commented:
no, that is not the problem. I teste the while thing and it works fine
0
 
2266180Commented:
Hi CodedK,

easiest way Ii can thing of:
when creating dinamically, set the TAG property to a non-zero value. when doing the refresh, iterate through the imtems and only remove the ones that have this tag set. :)

Cheers!
0
 
CodedKAuthor Commented:
I thought that too but i didnt know if treeview has tags like popup menu.
I'll try that :)

(I'll keep this open till i finish)
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
CodedKAuthor Commented:
:/
I'm using russells proc to add the childs and i cant insert a tag property in the function !
0
 
2266180Commented:
I just checked. it doesn't. sorry.

but you can use the Data property (pointer) if you don't use it for something else. you can set it to treeview instance (which is always defined) and at refresh free only those that are not nil
0
 
CodedKAuthor Commented:
Ok...
After this command :


For 0 to the end of values of i array do
   TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My Computer'), s[i]);
    HERE... how can i tag the existing just created child ?
0
 
2266180Commented:
can you post that code? or link to the question having the code.
0
 
CodedKAuthor Commented:
Yes ...

//-----------------------------------|
//    Add values to node by caption  |
//-----------------------------------|
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;

//-----------------------------------|
//    Add values to node by caption  |
//-----------------------------------|
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;
0
 
2266180Commented:
like this:

var t:ttreenode;
begin
  t:=TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My Computer'), s[i]);
  t.data:=TreeView1;
...

0
 
CodedKAuthor Commented:
I can understand

t:=TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My Computer'), s[i]);

this will mark the current (i) child...
but   "t.data:=TreeView1" ?

and afterwards how can i use this in a for statement to clen the tree ?
0
 
2266180Commented:
"t.data:=TreeView1" can be anything. you set the data property to something that is NOT nil.

then, you iterate through the items:
var i:integer;
begin
  i:=1;
  while i<=TreeView1.items.count do
  begin
    if TreeView1.Items.Item[i-1].Data<>nil then
      TreeView1.Items.Item[i-1].Delete     else
      inc(i);
  end;
0
 
CodedKAuthor Commented:
:O

This keeps everything there ! Nothing goes.
Since all of the created nodes are childs, we tag them we something and try to delete everything thats nil
but nothing is nil except the parent nodes (i want to keep) and they cant be deleted since childs stay...
0
 
CodedKAuthor Commented:
Sorry... we try to delete everything thats NOT nil... but they dont go !
0
 
2266180Commented:
that is because you didn't set the data property. I made a small test example and it worked like a charm.

make sure that you have added code to change the data property of teh dinamically added nodes to something that is not nil (treeview1 is not nil for sure that is why I used it)
0
 
CodedKAuthor Commented:
Of course i did ... :)

I'll recheck...
0
 
CodedKAuthor Commented:
Ok in every proc that i add a child i added these 2 lines :
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My Computer'), s[i]);
--->   t:=TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My Computer'), s[i]);
---->  t.data:=TreeView1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\hard\My Floppy'), s[i]);
--->   t:=TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\hard\My Floppy'), s[i]);
---->  t.data:=TreeView1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My HD'), s[i]);
--->   t:=TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My HD'), s[i]);
---->  t.data:=TreeView1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now i have a refresh label (OnClick):

Memo1.Clear;
Memo2.Clear;
  i:=1;
  while i<=TreeView1.items.count do
  begin
    if TreeView1.Items.Item[i-1].Data<>nil then
      TreeView1.Items.Item[i-1].Delete     else
      inc(i);
  end;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

But nothing works !!!
0
 
2266180Commented:
better just post the entire code or make a separate small app that reproduces the issue. I cannot see where the problem is
0
 
CodedKAuthor Commented:
I think that the while statement should have the t variable not the
TreeView1.Items.Item[i-1].Data

but something like
t.items.data

Is this the problem ?
0
 
CodedKAuthor Commented:
I'll try to create a small app with a portion of the code ...
0
 
CodedKAuthor Commented:
It works Ciuly...

The error was obvious :P (NOT)

This was what i did :

        TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My HD'), s[i]);
--->   t:=TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My HD'), s[i]);
---->  t.data:=TreeView1;

This is what i should do :
////////     TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My HD'), s[i]);  <---- Obsolete
--->   t:=TreeView1.Items.AddChild(ItemByPath(TreeView1, 'Sections\My HD'), s[i]);
---->  t.data:=TreeView1;


So thank you again and again :) :) :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.