Solved

TreeView items clear..

Posted on 2006-07-13
20
440 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
  • 12
  • 8
20 Comments
 
LVL 28

Expert Comment

by:ciuly
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
 
LVL 28

Expert Comment

by:ciuly
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:ciuly
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:ciuly
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:ciuly
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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:ciuly
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:ciuly
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:
ciuly earned 250 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

707 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now