List Collection Example 1

This example shows how to use a list collection to drag and drop items from one form to another.

List collections provide an ordered collection of components. List collection components are positional in reference to a given index or to the beginning or end of the list. Indexing is always relative to 1.

In this example employee number and salary information for selected employees is stored in a list collection at the start of a drag and drop operation and at the end of the operation this information is retrieved from the collection to the other form.

Define the Collection

The list collection is defined dynamically in the StartDrag event of the list of employees.  In this way the scope of the list collection is limited to the drag operation. The #Employee_Number variable is created to store employee numbers of selected employees. Because the collection is dynamic, it has to be explicitly created with a SET_REF command:

Define_Com Class(#Prim_LCOL<#Prim_ALPH>) Name(#PayLoadList) Reference(*Dynamic)

Define_Com Class(#Prim_ALPH) Name(#EmployeeNumber) Reference(*Dynamic)

Set_Ref Com(#PayLoadList) To(*Create_as #Prim_LCOL<#Prim_ALPH>)

Add Items to the Collection

Selected employees are added to the collection in a SELECTLIST loop. The #Employee_Number variable is assigned the employee number of selected employees and then used to identify the item added in the INSERT method to the collection. The list collection is then assigned as the payload or the drag and drop operation:

 

Selectlist Named(#DRAG_LIST)

Continue If('#Drag_List.Currentitem.Selected *ne True')

Set_Ref Com(#EmployeeNumber) To(*Create_as #Prim_ALPH)

Set Com(#EmployeeNumber) Value(#EmpNo)

Invoke Method(#PayLoadList.Insert) Item(#EmployeeNumber)

Endselect

Set_Ref Com(#PayLoad) To(#PayLoadList)

Retrieve Items from the Collection

When the selected employees are dragged to the other form, the DragOver event of the list on this form is used to test that the payload to be dropped is a list collection of alphas:

 

Evtroutine Handling(#Drop_List.DragOver) Acceptdrop(#AcceptDrop) Payload(#PayLoad)

If_Ref Com(#PayLoad) Is(*Instance_of #Prim_LCol<#Prim_alph>)

Set Com(#AcceptDrop) Value(True)

Else

Set Com(#AcceptDrop) Value(False)

Endif

Endroutine

Another list collection is defined in the DragDrop event of the list and then the payload is cast to the collection:

Define_Com Class(#Prim_LCOL<#Prim_ALPH>) Name(#PayLoadList) Reference(*Dynamic)

* Cast the payload as a list colelction of alphas ....

Set_Ref Com(#PayLoadList) To(*Dynamic #PayLoad)

The employees are retrieved from the collection to the list in a FOR/ENDFOR loop:

For Each(#EmployeeNumber) In(#PayLoadList)

Change Field(#EMPNO) To('#EMPLOYEENUMBER.VALUE')

Add_Entry To_List(#DROP_LIST)

Endfor

Source Code for List Collection Example 1

Source for the first form

Function Options(*DIRECT)

Begin_Com Role(*EXTENDS #PRIM_FORM) Clientheight(319) Clientwidth(247) Dragstyle(Automatic) Height(346) Left(255) Top(111) Width(255)

* The Drag List Definition. Note the use of the DragStyle(Automatic) property

Define_Com Class(#PRIM_LTVW) Name(#DRAG_LIST) Displayposition(1) Dragstyle(Automatic) Fullrowselect(True) Height(285) Left(8) Parent(#COM_OWNER) Tabposition(1) Top(8) Width(217)

Define_Com Class(#PRIM_LVCL) Name(#LVCL_1) Displayposition(1) Parent(#DRAG_LIST) Source(#EMPNO) Width(53)

Define_Com Class(#PRIM_LVCL) Name(#LVCL_2) Displayposition(2) Parent(#DRAG_LIST) Source(#SALARY) Width(45) Widthtype(Remainder)

* Define the drop form (change this name to the name you give to the second form)

Define_Com Class(#LCOL00004)

* ===============================

* Handle main form initialization

* ===============================

Evtroutine Handling(#com_owner.Initialize)

* Fill the drag list with employee details

Select Fields(#DRAG_LIST) From_File(PSLMST)

Add_Entry To_List(#DRAG_LIST)

Endselect

* Show the drop list form (change #LCOL00004 to the name you give to the second form)

Invoke Method(#LCOL00004.ShowForm)

Endroutine

* ==================================

* Handle starting the drag operation

* ==================================

Evtroutine Handling(#Drag_List.StartDrag) Draglist(#DragList) Continue(#Continue) Payload(#PayLoad)

Define_Com Class(#Prim_LCOL<#Prim_ALPH>) Name(#PayLoadList) Reference(*Dynamic)

Define_Com Class(#Prim_ALPH) Name(#EmployeeNumber) Reference(*Dynamic)

* Dynamically create the list collection that is to to be the "payload"

* for this drag operation

Set_Ref Com(#PayLoadList) To(*Create_as #Prim_LCOL<#Prim_ALPH>)

* Now fill the list collection with the employee numbers of all

* the currently selected employees

Selectlist Named(#DRAG_LIST)

Continue If('#Drag_List.Currentitem.Selected *ne True')

Set_Ref Com(#EmployeeNumber) To(*Create_as #Prim_ALPH)

Set Com(#EmployeeNumber) Value(#EmpNo)

Invoke Method(#PayLoadList.Insert) Item(#EmployeeNumber)

Endselect

* Finished

Set Com(#Continue) Value(True)

Set_Ref Com(#PayLoad) To(#PayLoadList)

Set Com(#DragList) Dragliststyle(Selection)

Endroutine

End_Com

Source for the second form

 

Function Options(*DIRECT)

Begin_Com Role(*EXTENDS #PRIM_FORM) Clientheight(310) Clientwidth(137) Dragstyle(Automatic) Formstyle(StayOnTopChild) Height(337) Left(577) Top(120) Width(145)

* The Drop List Definition

Define_Com Class(#PRIM_LTVW) Name(#DROP_LIST) Displayposition(1) Fullrowselect(True) Height(285) Left(16) Parent(#COM_OWNER) Tabposition(1) Top(8) Width(105)

Define_Com Class(#PRIM_LVCL) Name(#LVCL_1) Displayposition(1) Parent(#DROP_LIST) Source(#EMPNO) Width(53) Widthtype(Remainder)

* ===============================

* Handle Drag Over the drop list

* ===============================

Evtroutine Handling(#Drop_List.DragOver) Acceptdrop(#AcceptDrop) Payload(#PayLoad)

* If the payload to be dropped is a list collection of alphas

* accept it otherwise reject it .....

If_Ref Com(#PayLoad) Is(*Instance_of #Prim_LCol<#Prim_alph>)

Set Com(#AcceptDrop) Value(True)

Else

Set Com(#AcceptDrop) Value(False)

Endif

Endroutine

* =========================================

* Handle the actual drop into the drop list

* =========================================

Evtroutine Handling(#Drop_List.DragDrop) Payload(#PayLoad)

Define_Com Class(#Prim_LCOL<#Prim_ALPH>) Name(#PayLoadList) Reference(*Dynamic)

* Cast the payload as a list colelction of alphas ....

Set_Ref Com(#PayLoadList) To(*Dynamic #PayLoad)

* Clear the drop list and fill it will all the employee numbers in the payload ...

Clr_List Named(#DROP_LIST)

For Each(#EmployeeNumber) In(#PayLoadList)

Change Field(#EMPNO) To('#EMPLOYEENUMBER.VALUE')

Add_Entry To_List(#DROP_LIST)

Endfor

Endroutine

End_Com