2010. december 25., szombat

How to compare the items in a TStringList with the items in the child nodes of a selected node in a TTreeView


Problem/Question/Abstract:

I would like to compare the items in a TStringList with the child nodes of the selected node in a TTreeView and instead of deleting the matching nodes, change the image of the node to one from a TImageList component.

Answer:

Solve 1:

Something like:

var
  T: TTreeNode;
begin
  {Point at the first child of the selected node}
  T := TreeView.Selected.GetFirstChild;
  {Loop over all children of this node}
  while Assigned(T) do
  begin
    {Compare T.Text against contents of a listbox, or whatever...}
    {T set to nil if Selected has no more children}
    T := TreeView.Selected.GetNextChild(T);
  end;
end;

Note this only works with direct children of the Selected node; if you may have to deal with deeper levels in the tree then it gets (marginally) more complex.


Solve 2:

Try one of these:

{ ... }
for i := 0 to TreeView1.Selected.Count - 1 do
  if ListBox1.Items.IndexOf(TreeView1.Selected.Item[i].Text) >= 0 then
    TreeView1.Selected.Item[i].ImageIndex := 4;
{ ... }

var
  child: TTreeNode;
  { ... }
  child := TreeView1.Selected.GetFirstChild;
  while Assigned(child) do
  begin
    if ListBox1.Items.IndexOf(child.Text) >= 0 then
      child.ImageIndex := 4;
    child := child.GetNextSibling
  end;

Nincsenek megjegyzések:

Megjegyzés küldése