2.23.3 Example 2: Move Employees between Two Forms

In this example employees are dragged from one form and dropped to another form.

This is the source form:

And this is the target form:

If the employees are dragged without holding the Ctrl key down, they are moved from the source to the target form. If the Ctrl key is held down, the employees are copied from the source form to the target form.

Cursors

To visually differentiate between the move and copy operations, this example users two cursor components. Therefore, if you want to execute this example you need to create two components of type Cursor:

DD_CURMOV   

This cursor will be displayed when you copy employees

DD_CURCPY

This cursor will be displayed when you move employees

When you create these components, you need to specify the name of an existing .cur file as the value of the FileName property. You should be able to find .cur files in your Windows directory. It does not matter what cursor image you choose.

To execute this example, you need Source for the DD_EMPPL Payload compiled in a reusable part, the two cursors mentioned in the previous section and the compiled Source for the Source Form and Source for the Target Form.

Execute the source form. The target form will be displayed automatically.

Source for the Source Form

Copy and paste this code to a form and compile and execute it:

FUNCTION options(*DIRECT)

BEGIN_COM role(*EXTENDS #PRIM_FORM) HEIGHT(258) LAYOUTMANAGER(#ATLM_1) LEFT(302) TOP(108) VISUALSTYLE(#VS_NORM) WIDTH(662)

DEFINE_COM class(#PRIM_ATLM) name(#ATLM_1)

DEFINE_COM class(#PRIM_STBR) name(#STBR_1) DISPLAYPOSITION(1) HEIGHT(24) LEFT(0) MESSAGEPOSITION(1) PARENT(#COM_OWNER) TABPOSITION(1) TABSTOP(False) TOP(207) WIDTH(654)

DEFINE_COM class(#PRIM_ATLI) name(#ATLI_1) ATTACHMENT(Bottom) MANAGE(#STBR_1) PARENT(#ATLM_1)

DEFINE_COM class(#PRIM_LTVW) name(#LTVW_1) DISPLAYPOSITION(2) DRAGSTYLE(Automatic) FULLROWSELECT(True) HEIGHT(207) LEFT(0) PARENT(#COM_OWNER) TABPOSITION(2) TOP(0) WIDTH(654)

DEFINE_COM class(#PRIM_ATLI) name(#ATLI_2) ATTACHMENT(Center) MANAGE(#LTVW_1) PARENT(#ATLM_1)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_1) DISPLAYPOSITION(1) PARENT(#LTVW_1) SOURCE(#EMPNO) WIDTH(16)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_2) DISPLAYPOSITION(2) PARENT(#LTVW_1) SOURCE(#SURNAME) WIDTH(20)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_3) DISPLAYPOSITION(3) PARENT(#LTVW_1) SOURCE(#GIVENAME) WIDTH(20)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_4) DISPLAYPOSITION(4) PARENT(#LTVW_1) SOURCE(#DEPTMENT) WIDTH(20)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_5) DISPLAYPOSITION(5) PARENT(#LTVW_1) SOURCE(#SECTION) WIDTH(20)

DEFINE_COM class(#DD_TARGET) name(#DD_TARGET)

EVTROUTINE handling(#com_owner.CreateInstance)

SET com(#com_owner) CAPTION(*component_desc)

SELECT fields(#LTVW_1) from_file(PSLMST)

ADD_ENTRY to_list(#LTVW_1)

ENDSELECT

INVOKE method(#dd_target.showform)

INVOKE method(#com_owner.activateform)

ENDROUTINE

EVTROUTINE handling(#LTVW_1.StartDrag) PAYLOAD(#Payload)

DEFINE_COM class(#dd_emppl) name(#Local_Payload)

* Create payload

SELECTLIST named(#LTVW_1)

CONTINUE if('#ltvw_1.currentitem.selected = False')

INVOKE method(#Local_Payload.Add_to_payload) EMPLOYEE_ID(#empno) EMPLOYEE_SURNAME(#surname) EMPLOYEE_GIVENAME(#givename) EMPLOYEE_DEPARTMENT(#deptment) EMPLOYEE_SECTION(#section)

ENDSELECT

* Move temporary payload to Drag Manager.

* Temporary Payload is destroyed at the end of the routine

SET_REF com(#payload) to(#Local_Payload)

ENDROUTINE

* Check Drag result

EVTROUTINE handling(#LTVW_1.EndDrag) PAYLOAD(#payload) DRAGRESULT(#DragResult)

DEFINE_COM class(#dd_emppl) name(#Local_Payload) reference(*dynamic)

SET_REF com(#Local_payload) to(*dynamic #Payload)

CHANGE field(#STD_OBJ) to('#DRAGRESULT.VALUE')

IF cond('#DragResult.value = Accepted')

* Control key down then copying

IF cond('#sys_keybd.ControlkeyDown = False')

SELECTLIST named(#LTVW_1)

CONTINUE if('#ltvw_1.currentitem.selected = False')

DLT_ENTRY from_list(#LTVW_1)

ENDSELECT

ENDIF

ENDIF

ENDROUTINE

EVTROUTINE handling(#COM_OWNER.Closing) options(*NOCLEARMESSAGES *NOCLEARERRORS)

INVOKE method(#dd_target.closeform)

ENDROUTINE

END_COM

Source for the Target Form

Copy and paste this code to a form and compile it. This form is displayed automatically with the source form.

FUNCTION options(*DIRECT)

BEGIN_COM role(*EXTENDS #PRIM_FORM) HEIGHT(256) LAYOUTMANAGER(#ATLM_1) LEFT(314) TOP(356) VISUALSTYLE(#VS_NORM) WIDTH(644)

DEFINE_COM class(#PRIM_ATLM) name(#ATLM_1)

DEFINE_COM class(#PRIM_STBR) name(#STBR_1) DISPLAYPOSITION(1) HEIGHT(24) LEFT(0) MESSAGEPOSITION(1) PARENT(#COM_OWNER) TABPOSITION(1) TABSTOP(False) TOP(205) WIDTH(636)

DEFINE_COM class(#PRIM_ATLI) name(#ATLI_1) ATTACHMENT(Bottom) MANAGE(#STBR_1) PARENT(#ATLM_1)

DEFINE_COM class(#PRIM_LTVW) name(#LTVW_1) DISPLAYPOSITION(2) FULLROWSELECT(True) HEIGHT(205) LEFT(0) PARENT(#COM_OWNER) TABPOSITION(2) TOP(0) WIDTH(636)

DEFINE_COM class(#PRIM_ATLI) name(#ATLI_2) ATTACHMENT(Center) MANAGE(#LTVW_1) PARENT(#ATLM_1)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_1) DISPLAYPOSITION(1) PARENT(#LTVW_1) SOURCE(#EMPNO) WIDTH(16)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_2) DISPLAYPOSITION(2) PARENT(#LTVW_1) SOURCE(#SURNAME) WIDTH(20)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_3) DISPLAYPOSITION(3) PARENT(#LTVW_1) SOURCE(#GIVENAME) WIDTH(20)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_4) DISPLAYPOSITION(4) PARENT(#LTVW_1) SOURCE(#DEPTMENT) WIDTH(20)

DEFINE_COM class(#PRIM_LVCL) name(#LVCL_5) DISPLAYPOSITION(5) PARENT(#LTVW_1) SOURCE(#SECTION) WIDTH(20)

EVTROUTINE handling(#com_owner.CreateInstance)

SET com(#com_owner) CAPTION(*component_desc)

ENDROUTINE

EVTROUTINE handling(#ltvw_1.DragOver) PAYLOAD(#payload) ACCEPTDROP(#AcceptDrop) DRAGCURSOR(#Cursor)

* If an instance of the Employee "Payload"

SET com(#Acceptdrop) VALUE(true)

IF cond('#sys_keybd.ControlkeyDown = False')

SET_REF com(#cursor) to(#dd_curmov)

ELSE

SET_REF com(#cursor) to(#dd_curcpy)

ENDIF

ENDROUTINE

EVTROUTINE handling(#ltvw_1.DragDrop) PAYLOAD(#payload)

DEFINE_COM class(#dd_emppl) name(#Local_Payload) reference(*dynamic)

SET_REF com(#Local_Payload) to(*dynamic #Payload)

* Get the number of items from the payload

CHANGE field(#STD_NUM) to('#Local_Payload.Payload_Items')

* Get the number of items data from the payload

BEGIN_LOOP using(#LISTENTRY) to(#STD_NUM)

INVOKE method(#Local_Payload.Get_Payload_Item) PAYLOAD_ITEM(#listentry) EMPLOYEE_ID(#empno) EMPLOYEE_SURNAME(#surname) EMPLOYEE_GIVENAME(#givename) EMPLOYEE_DEPARTMENT(#deptment) EMPLOYEE_SECTION(#section)

ADD_ENTRY to_list(#LTVW_1)

END_LOOP

SET_REF com(#Local_Payload) to(*null)

ENDROUTINE

END_COM