Recent

Author Topic: How to show node image when a TTreeView item is selected  (Read 450 times)

tintinux

  • Sr. Member
  • ****
  • Posts: 325
    • Gestinux
How to show node image when a TTreeView item is selected
« on: October 04, 2022, 01:58:24 pm »
Hi

I display node images, using the property "Images" of my TreeView.

When a node is selected the image disappear on this node.
Is it possible to prevent this ?
Thanks
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

d7_2_laz

  • Sr. Member
  • ****
  • Posts: 312
Re: How to show node image when a TTreeView item is selected
« Reply #1 on: October 04, 2022, 03:02:16 pm »
Probably via property "SelectedIndex"   ... eg.
Code: Pascal  [Select][+][-]
  1.        aNode.ImageIndex := 4;
  2.        aNode.SelectedIndex := 4;     // or:  aNode.SelectedIndex := aNode.ImageIndex;

tintinux

  • Sr. Member
  • ****
  • Posts: 325
    • Gestinux
Re: How to show node image when a TTreeView item is selected
« Reply #2 on: October 04, 2022, 03:42:37 pm »
No, this is not a solution.
I was probably not clear enough.
The images are correctly displayed on all tree nodes, but when a node is selected (either by code or by UI), the image is not visible.
 
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

ASerge

  • Hero Member
  • *****
  • Posts: 2033
Re: How to show node image when a TTreeView item is selected
« Reply #3 on: October 04, 2022, 04:46:24 pm »
The images are correctly displayed on all tree nodes, but when a node is selected (either by code or by UI), the image is not visible.
The solution from @d7_2_laz is just for such a case.

d7_2_laz

  • Sr. Member
  • ****
  • Posts: 312
Re: How to show node image when a TTreeView item is selected
« Reply #4 on: October 04, 2022, 05:05:00 pm »
tintinux, i don't know with which platform and Lazarus version you are working ..
I can speak for Windows only - no such problem here -; and in this situation i would try something like:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.TreeView1SelectionChanged(Sender: TObject);
  2. begin
  3.    ShowMessage(inttostr(TreeView1.Selected.SelectedIndex));
  4. end;

Now, here either you see a "-1" = no image assigned = nothing will be displayed
or can inspect the image the SelectedIndex does point to within the ImageList assigned. Something special with this image?

tintinux

  • Sr. Member
  • ****
  • Posts: 325
    • Gestinux
Re: How to show node image when a TTreeView item is selected
« Reply #5 on: November 16, 2022, 06:09:05 pm »
Hi

I'm on Linux Ubuntu.

My issue is only about the displayed image : when the node is selected, the image is not shown, when the node is not selected the image is shown.

I have no problem with SelectedIndex.

I have made a small example attached. Maybe you will see easier the issue.

Thanks
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

tintinux

  • Sr. Member
  • ****
  • Posts: 325
    • Gestinux
Re: How to show node image when a TTreeView item is selected
« Reply #6 on: November 16, 2022, 06:14:10 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.TreeView1SelectionChanged(Sender: TObject);
  2. begin
  3.    ShowMessage(inttostr(TreeView1.Selected.SelectedIndex));
  4. end;

Now, here either you see a "-1" = no image assigned = nothing will be displayed

You are wrong, I think. Selected.ImageIndex (instead of Selected.SelectedIndex) returns the image associated to the node. In my case it doesn't change after node creation.
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

wp

  • Hero Member
  • *****
  • Posts: 10286
Re: How to show node image when a TTreeView item is selected
« Reply #7 on: November 16, 2022, 06:32:37 pm »
Completed your code so that it is working:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2.  
  3. begin
  4.   TreeView1.Items.AddChild(TreeView1.Items.Add(nil, 'Node 1'), 'Node 2');
  5.   Treeview1.Items[0].ImageIndex:= 0;
  6.   Treeview1.Items[1].ImageIndex:= 0;
  7.   TreeView1.Items[0].SelectedIndex := 0;
  8.   TreeView1.Items[1].SelectedIndex := 0;
  9. end;

See: https://lazarus-ccr.sourceforge.io/docs/lcl/comctrls/ttreenode.selectedindex.html, or https://docwiki.embarcadero.com/Libraries/Sydney/en/Vcl.ComCtrls.TTreeNode.SelectedIndex

tintinux

  • Sr. Member
  • ****
  • Posts: 325
    • Gestinux
Re: How to show node image when a TTreeView item is selected
« Reply #8 on: November 16, 2022, 06:36:45 pm »
Thanks !
Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

 

TinyPortal © 2005-2018