Get Widget Handle By Name

Posted by Admin on 19-Sep-2006 15:56

Hi,

I've got a table that contains columns for "controlName" and "controlValue", both are CHARACTER type.

I'd like to iterate through the table using a FOR EACH and set SCREEN:VALUES for widgets on my form based on values in my table. Is there a way to get a handle to a Widget at runtime via the value I have in the "controlName" column? Something like:

DEFINE VARIABLE myControl AS WIDGET-HANDLE.

FOR EACH ...

myControl = GET-WIDGET-BY-NAME(controlName).

myControl:SCREEN-VALUE = controlValue.

END.

Thanks in advance,

Rob

All Replies

Posted by Admin on 19-Sep-2006 20:26

Try this:

DEFINE VARIABLE myControl AS WIDGET-HANDLE.

FOR EACH ...

myControl = getWidgetByName(frame :handle, controlName).

myControl:SCREEN-VALUE = controlValue.

END.

...

FUNCTION getWidgetByName returns widget-handle

( phParent as widget-handle,

pcWidgetName as character ) :

define variable lhScanWidget as widget-handle no-undo.

define variable lhTmpWidget as widget-handle no-undo.

define variable llFound as logical no-undo init no.

if valid-handle(phParent) then assign

lhScanWidget = phParent:first-child.

do while valid-handle(lhScanWidget) and (not llFound):

if lhScanWidget:type = "FIELD-GROUP" then do:

/* Scan children */

lhTmpWidget = getWidgetByName(lhScanWidget,pcWidgetName).

if valid-handle(lhTmpWidget) then assign

llFound = yes

lhScanWidget = lhTmpWidget.

end. else do:

/* Check item */

llFound = (lhScanWidget:name = pcWidgetName).

end.

if not llFound then lhScanWidget = lhScanWidget:next-sibling.

end.

return (lhScanWidget).

end function.

The function above scans all widgets in the specified frame and returns one with the specified name (or ? - if the widget is not found).

This should work, however if you want to update many widgets and you have many widgets in your frame then it would be better (for performance reasons) to build list of all widgets in temp table or set of arrays and use them to speed up the function.

Regards,

Marian

Posted by Admin on 20-Sep-2006 11:43

Thanks Marian, works perfectly!

This thread is closed