Board index » delphi » TreeView problem->Setting images when click

TreeView problem->Setting images when click

Hi!
I want to make my treeview show an open folder when I select the node
etc...
How do I do that? I have placed images in a imageList, but the nodes will
only show item 0 in the imagelist.

Can someone help me with an example on how I can get this to work?

-Geir Bratlie-
 ro...@sn.no

 

Re:TreeView problem->Setting images when click


Geir Bratlie <ro...@sn.no> wrote in article
<01bbe964$5c9e4be0$ae5f8fc2@royal>...

Quote
> I want to make my treeview show an open folder when I select the node
> etc...
> How do I do that? I have placed images in a imageList, but the nodes will
> only show item 0 in the imagelist.

> Can someone help me with an example on how I can get this to work?

Hei Geir,

If you only want to show another image when the node is selected, you may
set the SelectedIndex and the ImageIndex property of the TreeNode
appropriately.
However, if you want to have different images depending upon the node being
open/expanded, I think you'll have to implement the OnGetImageIndex and
OnGetSelectedIndex events of the TTreeView.
In a setting when all the nodes in the tree are of the same type, I have
used code similiar to the below to do this.
If your TreeView have lots of different types of nodes, I'd prefer a more
"Object Oriented" way (but requiring more coding upfront).

procedure TfrmSGMLDocument.TreeView1GetImageIndex(Sender: TObject;
        Node: TTreeNode);
var anElement: TSGML_Element;
begin
// typically do an assertion here.
        anElement:=TSGML_Element(Node.Data);
        if not anElement.HasSubTags then
                node.ImageIndex:=2
        else Begin
                if node.Expanded then
                        node.ImageIndex:=1
                else
                        node.ImageIndex:=0
        End;
end;

procedure TfrmSGMLDocument.TreeView1GetSelectedIndex(Sender: TObject;
        Node: TTreeNode);
var anElement: TSGML_Element;
begin
// typically do an assertion here.
        anElement:=TSGML_Element(Node.Data);
        if not anElement.HasSubTags then
                node.SelectedIndex:=2
        else Begin
                if node.Expanded then
                        node.SelectedIndex:=1
                else
                        node.SelectedIndex:=0
        End;
end;

Regards,

Jarle stabell

Re:TreeView problem->Setting images when click


Thanx a lot! :)

-Geir Bratlie-
 ro...@sn.no

Other Threads