Page 1 of 1
tree -is it possible to change image?
Posted: Fri Feb 03, 2012 3:32 pm
by mol
Hi!
I want to build a tree for selecting some options - like filtering in Excel 2007 - when you pressed once on item - picture changes to "selected" - second click on this item changes image to "non selected".
But - images are accessible only while definition time...
has sb. any idea to change image of item?
Re: tree -is it possible to change image?
Posted: Tue Apr 25, 2017 12:06 pm
by giuper63
Hi to all,
same problem of mol!
Is there a way to change the images of a tree after it's definition?
I have found the instruction TreeItemSetImageIndex ("Tree_1", "Main", Main.Tree_1.value,{2,2} ) where I put the image of the item n. 2 to the actual item, but it not solve the problem.
I could use hidden items bat it's not the correct way.
Can someone help us?
Re: tree -is it possible to change image?
Posted: Tue Apr 25, 2017 1:15 pm
by edk
Hi Giuseppe and Marek.
giuper63 wrote: ↑Tue Apr 25, 2017 12:06 pm
Is there a way to change the images of a tree after it's definition?
Here is an example:
Code: Select all
// by Andrés González López, September 2014
#include "hmg.ch"
FUNCTION Main()
DEFINE WINDOW Form_1;
AT 0,0;
WIDTH 800;
HEIGHT 600;
NOSIZE;
NOMAXIMIZE;
ON GOTFOCUS Form_1.Tree_1.SetFocus;
MAIN
ON KEY F1 OF Form_1 ACTION OnKeyFx ( 2 )
ON KEY F2 OF Form_1 ACTION OnKeyFx ( 5 )
ON KEY F3 OF Form_1 ACTION OnKeyFx ( 8 )
ON KEY F4 OF Form_1 ACTION OnKeyFx ( 12 )
ON KEY F5 OF Form_1 ACTION OnKeyFx ( 22 )
ON KEY F6 OF Form_1 ACTION Form_1.Tree_1.ImageIndex(Form_1.Tree_1.Value):={4 , 5}
ON KEY F9 OF Form_1 ACTION ( Form_1.Tree_1.Value:= 1,;
IF ( Form_1.Tree_1.IsExpand (Form_1.Tree_1.Value) == .T.,;
Form_1.Tree_1.Collapse (Form_1.Tree_1.Value, .T.),; // Collapse All
Form_1.Tree_1.Expand (Form_1.Tree_1.Value, .T.))) // Expand All
DEFINE TREE Tree_1;
AT 10,10;
WIDTH 250;
HEIGHT 550;
VALUE 1;
FONT "Calibri" SIZE 11;
ON CHANGE OnChangeTree (This.Value);
NODEIMAGES { "folder.gif", "folder_page.gif" };
ITEMIMAGES { "Page.gif", "page_next.gif" };
NOROOTBUTTON
NODE 'MY APP '
NODE 'BOOK [F1]'
TREEITEM 'Science'
TREEITEM 'Literature'
END NODE
NODE 'MUSIC [F2]'
TREEITEM 'Rock'
TREEITEM 'Classic'
END NODE
NODE 'VIDEOS [F3]'
TREEITEM 'Documentary'
TREEITEM 'Series'
TREEITEM 'Sports'
END NODE
NODE 'EMAIL [F4]'
TREEITEM 'Send'
TREEITEM 'Inbox'
TREEITEM 'Outbox'
TREEITEM 'Drafts'
TREEITEM 'Spam'
TREEITEM 'Sent Items'
NODE 'OTHER'
TREEITEM 'Backup'
TREEITEM 'Calendar'
END NODE
END NODE
NODE 'ABOUT... [F5]'
END NODE
END NODE
END TREE
Form_1.Tree_1.AddImage:="Page2.gif"
Form_1.Tree_1.AddImage:="page_next2.gif"
Form_1.Tree_1.Expand (1)
Form_1.Tree_1.NodeFlag (22) := .T.
Form_1.Tree_1.FontSize := 12 // The control must have a size greater than or equal to font size of DynamicFont so that the text does not appear cut
Form_1.Tree_1.DynamicFont := {|| TreeFont() }
Form_1.Tree_1.DynamicBackColor := {|| IF (This.TreeItemValue == Form_1.Tree_1.Value, WHITE, NIL ) }
Form_1.Tree_1.DynamicForeColor := {|| IF (This.TreeItemValue == Form_1.Tree_1.Value, BLUE, NIL ) }
END WINDOW
CENTER WINDOW Form_1
ACTIVATE WINDOW Form_1
Return nil
Function TreeFont
LOCAL aFont
IF Form_1.Tree_1.NodeFlag (This.TreeItemValue) == .T. // for default: True Node always has the NodeFlag() == .T.
aFont := ARRAY FONT "Calibri" SIZE 12 BOLD
ELSE
aFont := ARRAY FONT "Calibri" SIZE 11
ENDIF
Return aFont
FUNCTION OnKeyFx ( nItem )
OnChangeTree( nItem )
Form_1.Tree_1.Value:= nItem
IF Form_1.Tree_1.IsExpand( Form_1.Tree_1.Value )
Form_1.Tree_1.Collapse( Form_1.Tree_1.Value )
ELSE
Form_1.Tree_1.Expand( Form_1.Tree_1.Value )
ENDIF
RETURN nil
FUNCTION OnChangeTree ( nItem )
DO CASE
CASE nItem = 1
CASE nItem = 2
CASE nItem = 3
// CASE nOption 4 to 22 ....
ENDCASE
Form_1.TITLE := "#"+HB_NTOS (nItem) + " --> " + HB_VALTOEXP ( Form_1.Tree_1.GetPathName (nItem) )
RETURN nil
You need to add new image files:
Form_1.Tree_1.AddImage:="Page2.gif" //will be No. 4
Form_1.Tree_1.AddImage:="page_next2.gif" //will be No. 5
Then assign to the item:
Form_1.Tree_1.ImageIndex(Form_1.Tree_1.Value):={4 , 5}
In example above you can change images by F6 key.
Re: tree -is it possible to change image?
Posted: Tue Apr 25, 2017 1:52 pm
by Rathinagiri
Thanks a lot!
I think we need a sample for implementation of hImageList also.
Re: tree -is it possible to change image?
Posted: Wed Apr 26, 2017 2:05 pm
by Pablo César
Rathinagiri wrote: ↑Tue Apr 25, 2017 1:52 pm
I think we need a sample for implementation of hImageList also.
It would be great !
But I think it would nothing happen if the TREE event is malfunctioning.
I do not know if it happens only with me or it's a bug to recognize the "Selected item ID" (Tree Value property) at Expand and Collapse event.
When I click to "+" it opens the subitems or clicking to "-" but I can not changes the image even if the images are predefined for the item.
@ Dr. Claudio Soto:
I saw the source code and I'm confused in this:

- Screen198.png (131 KiB) Viewed 6499 times
I'm confused because I believe that in TVE_EXPAND and TVE_COLLAPSE events, it should update the TreeItem Value.
Would you confirm if there is any reason for that or it's was just a mistake to change the value for Nil ?
Re: tree -is it possible to change image?
Posted: Wed Apr 26, 2017 9:10 pm
by giuper63
Solved!
For me the problem was to understand the image system of trees.
When you define a tree you MUST give 2 images to NODEIMAGES and 2 images to ITEMIMAGES.
Now you can add more images with AddImages.
In the example of edk there are 6 images: "folder.gif", "folder_page.gif", "Page.gif", "page_next.gif" , "Page2.gif" and "page_next2.gif" in this order.
Using "Form_1.Tree_1.ImageIndex(Form_1.Tree_1.Value):={5,5}" you give the image n. 5 (Page2.gif) to the item.
Thanks to all!
Re: tree -is it possible to change image?
Posted: Thu Apr 27, 2017 10:33 pm
by Pablo César
I found a bug and fully reported at:
viewtopic.php?p=51101#p51101