Page 1 of 1

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

Posted: Thu Oct 16, 2003 4:54 pm
by Psychophanta
Does anyone know the way to know the itemindex of a node (parent) item knowing the itemindex of another item (child)?

Thanx :)

Posted: Thu Oct 16, 2003 5:07 pm
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

Posted: Thu Oct 16, 2003 5:22 pm
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

Posted: Thu Mar 08, 2007 4:52 pm
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

Posted: Thu Mar 08, 2007 5:05 pm
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.

Posted: Thu Mar 08, 2007 5:05 pm
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

Posted: Thu Mar 08, 2007 5:56 pm
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

Posted: Thu Mar 08, 2007 5:59 pm
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