Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TreeView items clear..

Posted on 2006-07-13
20
Medium Priority
?
452 Views
Last Modified: 2012-05-05
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.
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
  • 12
  • 8
20 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17098063
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
 
LVL 16

Author Comment

by:CodedK
ID: 17098069
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
 
LVL 16

Author Comment

by:CodedK
ID: 17098083
:/
I'm using russells proc to add the childs and i cant insert a tag property in the function !
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 28

Expert Comment

by:2266180
ID: 17098090
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
 
LVL 16

Author Comment

by:CodedK
ID: 17098094
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
 
LVL 28

Expert Comment

by:2266180
ID: 17098095
can you post that code? or link to the question having the code.
0
 
LVL 16

Author Comment

by:CodedK
ID: 17098100
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
 
LVL 28

Expert Comment

by:2266180
ID: 17098101
like this:

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

0
 
LVL 16

Author Comment

by:CodedK
ID: 17098113
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
 
LVL 28

Expert Comment

by:2266180
ID: 17098146
"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
 
LVL 16

Author Comment

by:CodedK
ID: 17098183
: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
 
LVL 16

Author Comment

by:CodedK
ID: 17098189
Sorry... we try to delete everything thats NOT nil... but they dont go !
0
 
LVL 28

Expert Comment

by:2266180
ID: 17098198
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
 
LVL 16

Author Comment

by:CodedK
ID: 17098201
Of course i did ... :)

I'll recheck...
0
 
LVL 16

Author Comment

by:CodedK
ID: 17098258
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
 
LVL 28

Expert Comment

by:2266180
ID: 17098269
better just post the entire code or make a separate small app that reproduces the issue. I cannot see where the problem is
0
 
LVL 16

Author Comment

by:CodedK
ID: 17098277
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
 
LVL 16

Author Comment

by:CodedK
ID: 17098290
I'll try to create a small app with a portion of the code ...
0
 
LVL 28

Accepted Solution

by:
2266180 earned 1000 total points
ID: 17098315
no, that is not the problem. I teste the while thing and it works fine
0
 
LVL 16

Author Comment

by:CodedK
ID: 17098497
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

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

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…
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 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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

609 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