So far we have set attributes and called methods of the control. In order to react to user actions, e.g. a mouse click, we need to configure the control in a way that causes our own functions to be called when a user action takes place. This is known as "event handling".

The interface description of a control lists all possible events together with their parameters. Here is an example, taken from the "VBSEdit" tool:

In order to tell the control that one of our functions is to be called when a particular event is raised, we use the function

guixt.SetEventHandler(obj1, obj2)

Its two parameters are obj1, the object the events of which we want to handle, and obj2, the object that handles the events. For example, if the event "ColumnClick" is raised in  obj1,  the function "OnColumnClick" of obj2 will be called, passing the event parameters. As naming convention,  each event name is prefixed with "On" to obtain the name of the function to be called. If no function with this name exists, no function call is performed.

We continue the list view example of tutorial 4. The first object obj1 is the list view control, referenced by variable lh. How can we create the event handling object obj2 in VBScript? To this purpose we define a new class, using the Class ... End Class  statement. Any class name can be used, e.g. va03_list_events:

Class va03_list_events

  Function OnDblClick        
         
        
  EndFunction
    

  Function OnColumnClick(ColumnHeader)
        
       
  EndFunction
    
    
EndClass

After initializing the list view control we create a new object of this class and declare it as event handler object for our list control:

Function va03_init(lv, date_from)
...

  ' handle  events
    Set e = New va03_list_events
    Call guixt.SetEventHandler(lv, e)  
    
EndFunction
  

As soon as an event handler object has been assigned, you can switch on the GuiXT "Trace" in order to trace all events that are raised in the list control, with their parameters:

It is often useful to store a reference of the object that raises the event into the object that handles the events. We can then easily access all object attributes in the event handler functions:

   

Function va03_init(lv, date_from)
...


    ' handle  events
    Set e = New va03_list_events
    Set e.ListView = lv
    Call guixt.SetEventHandler(lv, e)  
    
EndFunction


Class va03_list_events
    
    Dim ListView
    
    ' double click    
    Function OnDblClick
        
  
        
    EndFunction
    
    
      
    Function OnColumnClick(ColumnHeader)
        
   
        
    EndFunction
    
    
    
EndClass


We now implement the desired event handler functions within this framework. When the user clicks on a column title, we  sort the list control rows by this column, switching between ascending and descending order. When the user doubleclicks on a list view row, we  display the customer order in transaction VA03, using the function  guixt.input(...)  to enter values into SAP input fields and to perform SAP actions.

In total, our VBScript looks as follows; the unchanged parts from tutorial 4 are in grey:

' Utility function: cut or enlarge a string to the specified length
Function xlen(f,n)
    xlen = Mid(f,1,n)
    If Len(xlen) < n Then
        xlen = xlen + Space(n-Len(xlen))
    End If
    
End Function


' Utility function:  build up a select option line for SAP RFCs
Function set_selopt(name,field,sign,opt,val1,val2)
    set_selopt = xlen(name,30) & xlen(field,30) & xlen(sign,1) & xlen(opt,2) & xlen(val1,45) & xlen(val2,45)   
End Function


' Utility function: pixel to twips ("TWentieth of an Inch Point")
' screen.deviceXDPI - Retrieves the actual number of horizontal dots per inch (DPI)
' screen.deviceYDPI - Retrieves the actual number of vertical dots per inch (DPI) 
Function Twips_x(x)
    Twips_x = x * (1440 / screen.deviceXDPI)
EndFunction

Function Twips_y(y)
    Twips_y = y * (1440 / screen.deviceYDPI)
EndFunction


Function va03_init(lv, date_from)
    
    If IsEmpty(lv) Then
        Set lv = CreateObject("ComCtl.ListViewCtrl")
    End If
    
    
    ' font
    lv.Font.Name = "Arial"
    lv.Font.Size = 10
    
    ' color
    lv.BackColor = RGB(240,240,240)  
    
    
    lv.View = 3    ' 3=lvwReport (from MS documentation)
    
    ' no label editing
    lv.LabelEdit = 1  ' 1=manually  (from MS documentation) 
    
    
    ' column headers       
    Call lv.ColumnHeaders.Add(,,guixt.get("coltitle_VBELN"), 1500)
    Call lv.ColumnHeaders.Add(,,guixt.get("coltitle_BSTDK"), 2000)
    Call lv.ColumnHeaders.Add(,,guixt.get("coltitle_KUNNR"), 2000)
    Call lv.ColumnHeaders.Add(,,guixt.get("coltitle_BSTKD"), 3000)
    
    
    
    ' array of select options
    Dim selopt(0)
    
    
    selopt(0) = set_selopt("VMVAA","BSTDK", "I", "GE",  date_from, "")
    
    ' array of orders
    Dim orders
    
    Call guixt.Rfc("Z_S10_SEARCHHELP", "in.SEARCHHELP", "VMVAA",  "in.COLUMNS",  "VBELN(10),BSTDK(10),KUNNR(10),BSTKD(20)", "table.SELOPT", selopt, "table.DATA(WIDTH:50)", orders) 
    
    
    For Each Order In  orders
        
        Dim itmX
        Set itmX = lv.ListItems.Add(,,Mid(Order,1,10))
        
        itmX.SubItems(1) = Mid(Order,11,10)
        itmX.SubItems(2) = Mid(Order,21,10)
        itmX.SubItems(3) = Mid(Order,31,20)
                       
    Next  
    
    
    ' handle  events
    Set e = New va03_list_events
    Set e.ListView = lv
    Call guixt.SetEventHandler(lv, e)  
    
EndFunction


Class va03_list_events
    
    Dim ListView
    
    
      
    Private ascending(4)
    
    Function OnColumnClick(ColumnHeader)
        
        Dim colno
        colno = ColumnHeader.Index - 1
        
        IfIsEmpty(ascending(colno)) Then
            ascending(colno) = 1
        EndIf
        
        ascending(colno) = 1 - ascending(colno)
        
        ListView.SortKey = colno
        ListView.SortOrder = ascending(colno)
        ListView.Sorted = True
        
        
    EndFunction
    
    
    
    
    ' mouse coordinates of last click
    Dim click_x
    Dim click_y
    
    
    ' Mouse down
    Function OnMouseDown(Button, Shift, x, y)
        
        ' save pixel coordinates 
        click_x = x
        click_y = y
        
    EndFunction
    
    
    
    
    ' double click    
    Function OnDblClick
        
        Dim item
        
        
        ' get clicked list item
             
        '   Remark:
        '   a simple way to get the clicked list item, without the need to 

        '  
capture the MouseDown coordinates, is to use 
        '     item = ListView.selectedItem
        '   but then the user has to double-click the order number column, 

        '  
not one of the other columns
        '   this is why we use mouse coordinates and HitTest
        
        ' HitTest requires coordinates in twips, not pixels !
        ' we use x-coordinate = 1 so that the user may click 

        '
anywhere in the list row
           
        Set item = ListView.HitTest(1, Twips_y(click_y))  
        
               
        IfNot item IsNothingthen
            
            guixt.input("U[VBELN]:" & item.Text)
            guixt.input("OK:/0,process=va03_enter.txt")
            
        EndIf    
        
        
        
    EndFunction
    
    
EndClass



The InputScript "va03_enter.txt" sets the order number and performs an "Enter":

Parameter VBELN
Set F[VBAK-VBELN] "&U[VBELN]"
Enter

To the user, application appears as follows:

A click on e.g. "PO date" sorts the list by PO date, here in descending order.

 

A double click on a row will display the selected customer order in transaction VA03.