Understanding selection and loss of selection in list views

This example uses three list views. At start up all employees are added to the All Employees and Not Selected Employees list views. As employees are selected and deselected from the All Employees list they are added to and removed from the Selected Employees and Not Selected Employees list.  

To see how the example works, copy this code and paste it to a form component:

FUNCTION options(*DIRECT)

BEGIN_COM role(*EXTENDS #PRIM_FORM) HEIGHT(378) LEFT(345) TOP(196) WIDTH(473)

DEFINE_COM class(#PRIM_LTVW) name(#ALL_LIST) DISPLAYPOSITION(1) FULLROWSELECT(True) HEIGHT(313) LEFT(8) PARENT(#GPBX_ALL) TABPOSITION(1) TOP(16) WIDTH(129)

DEFINE_COM class(#PRIM_LTVW) name(#SEL_LIST) DISPLAYPOSITION(1) FULLROWSELECT(True) HEIGHT(313) LEFT(8) PARENT(#GPBX_SEL) TABPOSITION(1) TOP(16) WIDTH(129)

DEFINE_COM class(#PRIM_LTVW) name(#NOT_LIST) DISPLAYPOSITION(1) FULLROWSELECT(True) HEIGHT(313) LEFT(8) PARENT(#GPBX_NOT) TABPOSITION(1) TOP(16) WIDTH(129)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_1) DISPLAYPOSITION(1) PARENT(#ALL_LIST) SORTPOSITION(1) SOURCE(#EMPNO) WIDTH(54) WIDTHTYPE(Remainder)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_2) DISPLAYPOSITION(1) PARENT(#SEL_LIST) SORTPOSITION(1) SOURCE(#EMPNO) WIDTH(54) WIDTHTYPE(Remainder)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_3) DISPLAYPOSITION(1) PARENT(#NOT_LIST) SORTPOSITION(1) SOURCE(#EMPNO) WIDTH(54) WIDTHTYPE(Remainder)

DEFINE_COM class(#PRIM_GPBX) name(#GPBX_ALL) CAPTION('All Employees') DISPLAYPOSITION(3) HEIGHT(337) LEFT(8) PARENT(#COM_OWNER) TABPOSITION(3) TABSTOP(False) TOP(8) WIDTH(145)

DEFINE_COM class(#PRIM_GPBX) name(#GPBX_SEL) CAPTION('Selected Employees') DISPLAYPOSITION(2) HEIGHT(338) LEFT(156) PARENT(#COM_OWNER) TABPOSITION(2) TABSTOP(False) TOP(6) WIDTH(145)

DEFINE_COM class(#PRIM_GPBX) name(#GPBX_NOT) CAPTION('NOT Selected Employees') DISPLAYPOSITION(1) HEIGHT(339) LEFT(304) PARENT(#COM_OWNER) TABPOSITION(1) TABSTOP(False) TOP(5) WIDTH(145)

DEFINE field(#SEL_EMPNO) reffld(#EMPNO)

EVTROUTINE handling(#com_owner.Initialize)

* Add all employees to the all and not selected lists

SELECT fields(#ALL_LIST) from_file(PSLMST)

ADD_ENTRY to_list(#ALL_LIST)

ADD_ENTRY to_list(#NOT_LIST)

ENDSELECT

ENDROUTINE

EVTROUTINE handling(#ALL_List.ItemGotSelection)

* Save employee number value

CHANGE field(#SEL_EMPNO) to(#EMPNO)

* Add to Selected list

ADD_ENTRY to_list(#SEL_LIST)

* Remove from the not selected list

SELECTLIST named(#NOT_LIST)

IF cond('#EMPNO = #SEL_EMPNO')

DLT_ENTRY from_list(#NOT_LIST)

LEAVE

ENDIF

ENDSELECT

ENDROUTINE

EVTROUTINE handling(#ALL_List.ItemLostSelection)

* Save employee number value

CHANGE field(#SEL_EMPNO) to(#EMPNO)

* Add to not Selected list

ADD_ENTRY to_list(#NOT_LIST)

* Remove from the selected list

SELECTLIST named(#SEL_LIST)

IF cond('#EMPNO = #SEL_EMPNO')

DLT_ENTRY from_list(#SEL_LIST)

LEAVE

ENDIF

ENDSELECT

ENDROUTINE

END_COM