I'm a treeview item; who's my parent?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

I'm a treeview item; who's my parent?

Post by Psychophanta »

Does anyone know the way to know the itemindex of a node (parent) item knowing the itemindex of another item (child)?

Thanx :)
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Code: Select all

hItem.l = GadgetItemID(#Tree, ChildNumber)
hParent.l = SendMessage_(GadgetID(#Tree), #TVM_GETNEXTITEM, #TVGN_PARENT, hItem)
PArentNumber = TreeGadgetItemNumber(#Tree, hParent)
Timo
quidquid Latine dictum sit altum videtur
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

I see TVM_GETNEXTITEM is very versatile TV message, but i was not able to find it. Now it is solved :D


One hundred of thanks again, Timo :D
Onkel-Till
New User
New User
Posts: 1
Joined: Thu Mar 08, 2007 4:49 pm

Post by Onkel-Till »

Hi,

since the v4.0 there is no TreeGadgetItemNumber() function :-(
Does anyone know a nice workaround for a item/parent search
in a treeview gadget?




g
till
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Afraid I haven't time to conjure up any examples, but two options come to mind.

API method: use the #TVM_GETNEXTITEM message with wParam set to #TVGN_PARENT.

Non-api: get the required item's sub-level by using GetGadgetItemAttribute() with the #PB_Tree_SubLevel attribute. Now search through the items of the tree in reverse, starting from the item in question, looking for the first item with a sublevel less than that already obtained etc.
I may look like a mule, but I'm not a complete ass.
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Just compare the sublevel of the items. The first one with a lower sublevel
than the current one is the parent:

Code: Select all

CurrentLevel = GetGadgetItemAttribute(#TreeGadget, CurrentItem, #PB_Tree_SubLevel)

For index = CurrentItem-1 To 0 Step -1
  If GetGadgetItemAttribute(#TreeGadget, index, #PB_Tree_SubLevel) < CurrentLevel
    ; index is the parent of CurrentItem
  EndIf
Next index
quidquid Latine dictum sit altum videtur
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Here ya go! Image

Code: Select all

OpenWindow(0,0,0,320,370,"untitled",#WS_OVERLAPPEDWINDOW | 1)
CreateGadgetList(WindowID(0))
TreeGadget(0,5,5,310,360)

For i=0 To 20
	AddGadgetItem(0,-1,"Tree-View Item #" + RSet(Str(i+1),2,"0"),0,Random(2))
	SetGadgetState(0,i)
Next

Repeat
	EventID = WaitWindowEvent()
	
	If EventID = #PB_Event_Gadget And EventType() = #PB_EventType_LeftClick 
		CurrentItem = GetGadgetState(0)
		
		CurrentLevel = GetGadgetItemAttribute(0,CurrentItem,#PB_Tree_SubLevel)

		If CurrentLevel > 0				
			For i = CurrentItem-1 To 0 Step -1			
				If GetGadgetItemAttribute(0,i,#PB_Tree_SubLevel) < CurrentLevel
					Debug "ITEM PARENT ID: #" + RSet(Str(i+1),2,"0")
					Break
				EndIf				
			Next		
		Else
			Debug "ITEM DOESN'T HAVE A PARENT"			
		EndIf
	EndIf
Until EventID = 16
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Also you can recreate TreeGadgetItemNumber() function if you don't want to change too much your old code:

Code: Select all

Procedure.l TreeGadgetItemNumber(Gadget.l,ItemID.l)
  Protected Count.l = CountGadgetItems(Gadget.l)
  While Count.l
    If GadgetItemID(Gadget.l,Count.l)=ItemID.l:ProcedureReturn Count:EndIf
    Count.l-1
  Wend
  ProcedureReturn -1
EndProcedure
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply