Making use of the new EditorChanged event in a Grid component

Date:6th March 2008
Product/Release:Visual LANSA 11.3
Abstract:The EditorChanged event of a grid uses Variants which has special considerations
Submitted By:LANSA Technical Support

Description:

Visual LANSA V11.3 introduced a new event into the Grid component called EditorChanged. Prior to this the only way to check the contents of a grid that has been updated was to wait for a user to tab, or click away from the field being edited. The EditorChanged event is fired whenever a user types into a write-capable cell in a grid. Effectively this is similar to a keypress event for a Grid cell.

However there is one important difference. The Value parameter of this Event is of type Variant, which must be handled differently to other field types.

Explanation:

Variants cannot be used in a similar way to Strings or numeric fields because LANSA has no idea what it is, or what methods/properties it has until runtime. If you try to use an intrinsic function on a Variant, or assign the value to a String or Alpha field type, the code will fail at runtime.

The documentation contains information about Variants (see Variant Handling in the Technical Reference Guide), but in essence there are 2 options when dealing with Variants:

Option 1: Import the Variant libraries.

Directly under the DEFINE_COM, include the following line:

FUNCTION OPTIONS(*DIRECT)
Import Libraries(#PRIM_LIBV)
BEGIN_COM Role(… etc

Doing this gives you access to the Variant handling functions such as VarAsString(). Then it is possible to do something like this in the EditorChanged Handler:

EVTROUTINE HANDLING(#GRID_1.EditorChanged) OPTIONS(*NOCLEARMESSAGES *NOCLEARERRORS) Value(#NewValue)
Case Of_Field(#Grid_1.FocusCell.Column.Position)
When Value_Is('*EQ 3')
#W_GIVENAME := VarAsString(#NewValue)

If (#W_GIVENAME.Trim.Cursize = 3)
Select Fields(#GIVENAME) From_File(PSLMST)
If ((#W_GIVENAME.Substring(1 3) = #GIVENAME.Substring(1 3)) AND (#GIVENAME.Cursize > 3))
Upd_Entry In_List(#Grid_1)
Leave
Endif
EndSelect
Endif
endcase
ENDROUTINE

Note: The new function call which becomes available when you import the variant libraries.

Option 2: Put the EditorChanged Variant into a statically defined Variant of class #Prim_Var

The second option is to assign the value to a statically declared Variant, which can then be assigned to other field types.

EVTROUTINE HANDLING(#GRID_1.EditorChanged) OPTIONS(*NOCLEARMESSAGES *NOCLEARERRORS) Value(#NewValue)
Define_Com Class(#PRIM_VAR) Name(#W_Value)
Set Com(#W_Value) Value(#NewValue)

Case Of_Field(#Grid_1.FocusCell.Column.Position)
When Value_Is('*EQ 3')
#XGIVENAME := #W_Value

Select Fields(#GIVENAME) From_File(PSLMST)
If ((#XGIVENAME.Substring(1 3) = #GIVENAME.Substring(1 3)) AND (#GIVENAME.Cursize > 3))
Upd_Entry In_List(#Grid_1)
Leave
Endif
EndSelect
EndCase
ENDROUTINE

This method does not require you to import #PRIM_LIBV, and could potentially be used in RDML forms.