Syntax
*UserData = WidgetGetUserData(Widget)
Description
Retrieves the user-defined data pointer associated with the specified widget.
Parameters
Widget
The handle of the ProGUI widget whose user data is to be retrieved.
Return Value
Returns the pointer (*UserData) that was previously associated with the widget using WidgetSetUserData() or during creation with CreateWidget(). Returns #Null if no user data was set or
if the widget handle is invalid.
Remarks
User data allows developers to attach arbitrary application-specific data (like pointers to structures or object instances) to a widget, often used within event handlers to access related information without relying on global variables.
Example
IncludeFile "ProGUI_PB.pbi"
StartProGUI()
Structure MyWidgetInfo
ID.i
Label$
EndStructure
; Event handler using UserData
Procedure WidgetInfoHandler(Widget, EventType, *EventData, *UserData)
If EventType = #PG_Event_MouseLeftButtonDown
*widgetUserData.MyWidgetInfo = WidgetGetUserData(Widget)
If *widgetUserData ; Check if *widgetUserData is valid
Debug "Widget Clicked!"
Debug "ID: " + Str(*widgetUserData\ID)
Debug "Label: " + *widgetUserData\Label$
EndIf
EndIf
EndProcedure
MyWindow = CreateWindow(0, 0, 300, 200, "User Data Example", #PG_Window_Default | #PG_Window_LayoutFlex)
RootLayout = WindowGetLayout(MyWindow)
If MyWindow
; Allocate memory for user data
*Info1.MyWidgetInfo = AllocateStructure(MyWidgetInfo)
*Info1\ID = 101
*Info1\Label$ = "First Button"
*Info2.MyWidgetInfo = AllocateStructure(MyWidgetInfo)
*Info2\ID = 102
*Info2\Label$ = "Second Button"
; Create widgets and set user data
Widget1 = CreateWidget(0, 0, 100, 30, *Info1)
WidgetSetClass(Widget1, "button")
AddEventHandler(Widget1, #PG_Event_MouseLeftButtonDown, @WidgetInfoHandler())
Widget2 = CreateWidget(0, 0, 100, 30)
WidgetSetUserData(Widget2, *Info2) ; Set data after creation
WidgetSetClass(Widget2, "button")
AddEventHandler(Widget2, #PG_Event_MouseLeftButtonDown, @WidgetInfoHandler())
; Retrieve user data later
*RetrievedInfo = WidgetGetUserData(Widget1)
If *RetrievedInfo = *Info1
Debug "Retrieved UserData for Widget1 matches."
EndIf
WindowShow(MyWindow, #True, #PG_Window_ScreenCentered)
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
; Clean up allocated memory
FreeStructure(*Info1)
FreeStructure(*Info2)
EndIf
StopProGUI()
See Also
WidgetSetUserData, CreateWidget, AddEventHandler
Supported OS
Windows, Linux