TreeGadget() no visible selection

Just starting out? Need help? Post your questions and find answers here.
User avatar
HeX0R
Addict
Addict
Posts: 1218
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

TreeGadget() no visible selection

Post by HeX0R »

Run this code and press the button to see the effect.

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "", #PB_Window_SystemMenu)
TreeGadget(0, 5, 5, 290, 260)
ButtonGadget(1, 25, 270, 120, 24, "Add Item")

AddGadgetItem(0, -1, "Item 1", 0, 0)
AddGadgetItem(0, -1, "Item 2", 0, 0)
AddGadgetItem(0, -1, "Item 3", 0, 1)
AddGadgetItem(0, -1, "Item 4", 0, 1)

SetGadgetItemState(0, 1, #PB_Tree_Expanded)

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			Break
		Case #PB_Event_Gadget
			Select EventGadget()
				Case 1
					AddGadgetItem(0, 1, "Added #1", 0, 0)
					AddGadgetItem(0, 2, "Added #2", 0, 1)
					AddGadgetItem(0, 3, "Added #3", 0, 1)
					SetGadgetState(0, 1)
					SetGadgetItemState(0, 1, #PB_Tree_Expanded)
					If GetGadgetState(0) = 1
						Debug "Added #1 is selected, but you don't see it!"
						Debug "Try to select Added #1 with the mouse, you will not see anything"
					EndIf
			EndSelect
	EndSelect
ForEver
User avatar
Bisonte
Addict
Addict
Posts: 1320
Joined: Tue Oct 09, 2007 2:15 am

Re: TreeGadget() no visible selection

Post by Bisonte »

Confirmed on Win 10 x64 with PB 5.62 (x86/x64)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: TreeGadget() no visible selection

Post by Demivec »

I don't think it's a bug, though I can't rule that out.

I think the more proper code may be to write it this way (note the two changed and commented lines in 'Case 1'):

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "", #PB_Window_SystemMenu)
TreeGadget(0, 5, 5, 290, 260)
ButtonGadget(1, 25, 270, 120, 24, "Add Item")

AddGadgetItem(0, -1, "Item 1", 0, 0)
AddGadgetItem(0, -1, "Item 2", 0, 0)
AddGadgetItem(0, -1, "Item 3", 0, 1)
AddGadgetItem(0, -1, "Item 4", 0, 1)

SetGadgetItemState(0, 1, #PB_Tree_Expanded)

Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
         Break
      Case #PB_Event_Gadget
         Select EventGadget()
            Case 1
               AddGadgetItem(0, 1, "Added #1", 0, 0)
               AddGadgetItem(0, 2, "Added #2", 0, 1)
               AddGadgetItem(0, 3, "Added #3", 0, 1)
               SetGadgetState(0, 1)
               SetGadgetItemState(0, 1, #PB_Tree_Selected | #PB_Tree_Expanded) ;Formally select the item
               SetActiveGadget(0) ;and make the gadget active to show the selection
               If GetGadgetState(0) = 1
                  Debug "Added #1 is selected, but you don't see it!"
                  Debug "Try to select Added #1 with the mouse, you will not see anything"
               EndIf
         EndSelect
   EndSelect
ForEver
User avatar
HeX0R
Addict
Addict
Posts: 1218
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: TreeGadget() no visible selection

Post by HeX0R »

Maybe it's the more proper way, anyway I would say it shouldn't happen at all, that you click on a tree item and it will not be (visual) selected, don't you think?
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: TreeGadget() no visible selection

Post by mk-soft »

I think it's a problem with the order the commands go to control.
Besides that a flag is missing

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "", #PB_Window_SystemMenu)
TreeGadget(0, 5, 5, 290, 260, #PB_Tree_AlwaysShowSelection)
ButtonGadget(1, 25, 270, 120, 24, "Add Item")

AddGadgetItem(0, -1, "Item 1", 0, 0)
AddGadgetItem(0, -1, "Item 2", 0, 0)
AddGadgetItem(0, -1, "Item 3", 0, 1)
AddGadgetItem(0, -1, "Item 4", 0, 1)

SetGadgetItemState(0, 1, #PB_Tree_Expanded)

;SetWindowTheme_(GadgetID(0), @"Explorer", 0)
    
Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
         Break
      Case #PB_Event_Gadget
         Select EventGadget()
            Case 1
               AddGadgetItem(0, 1, "Added #1", 0, 0)
               AddGadgetItem(0, 2, "Added #2", 0, 1)
               AddGadgetItem(0, 3, "Added #3", 0, 1)
               SetGadgetItemState(0, 1, #PB_Tree_Expanded)
               SetGadgetState(0, 1)
               If GetGadgetState(0) = 1
                  Debug "Added #1 is selected, but you don't see it!"
                  Debug "Try to select Added #1 with the mouse, you will not see anything"
               EndIf
         EndSelect
   EndSelect
ForEver
So works with Window 7 and 10

P.S. Same on Linux and Mac
Works only if first expanded tree, then set item
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: TreeGadget() no visible selection

Post by Fred »

Not a bug, you need to use SetGadgetItemState(0, 1, #PB_Tree_Expanded | #PB_Tree_Selected) as SetGadgetItemState() changes the selected state as well. Also #PB_Tree_AlwaysShowSelection needs to be specified if the selection needs to be seen when the gadget doesn't have the focus.
User avatar
HeX0R
Addict
Addict
Posts: 1218
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: TreeGadget() no visible selection

Post by HeX0R »

Boah, can't remember that one :lol:
Post Reply