Enumerating child objects << Back



There is no built-in function for retrieving a list of child objects of any given object. However by using Extension Functions, we can mimic the functionality easily.

Use UI

Procedure ChildObjectRegister For cObject Handle[] ByRef hCollection
	Move Self to hCollection[SizeOfArray(hCollection)]
End_Procedure

Function ChildObjects For cObject Boolean bRecursive Returns Handle[]
	Handle[] hCollection
	If (bRecursive) Broadcast Recursive Send ChildObjectRegister (&hCollection)
	Else Broadcast Send ChildObjectRegister (&hCollection)
	Function_Return hCollection
End_Function

Full-blown example

Use DfAllEnt

Procedure ChildObjectRegister For cObject Handle[] ByRef hCollection
	Move Self to hCollection[SizeOfArray(hCollection)]
End_Procedure

Function ChildObjects For cObject Boolean bRecursive Returns Handle[]
	Handle[] hCollection
	If (bRecursive) Broadcast Recursive Send ChildObjectRegister (&hCollection)
	Else Broadcast Send ChildObjectRegister (&hCollection)
	Function_Return hCollection
End_Function

// ================= Example starts here ============\

Object oPanel is a ModalPanel
	Object o1 is a Button
	End_Object
	Object o2 is a Button
	End_Object
	Object o3 is a Container
		Object o4 is a Button
		End_Object
	End_Object
End_Object

Procedure Display String sMsg Handle[] hCollection
	Integer iIndex iCount
	Showln sMsg
	Move (SizeOfArray(hCollection)-1) to iCount
	For iIndex From 0 to iCount
		Showln (Name(hCollection[iIndex]))
	Loop
End_Procedure

Procedure Test
	Handle[] hImmediateChildren hAllChildren
	Get ChildObjects of oPanel False to hImmediateChildren
	Get ChildObjects of oPanel True to hAllChildren
	Send Display "Immediate Children" hImmediateChildren
	Send Display "All Children" hAllChildren
	InKey FieldIndex
End_Procedure

Send Test
Free Web Hosting