CreateUnoListener Function

Kreas ekzempleron de Listener (aŭskultanto)

Many Uno objects let you register listeners with dedicated listener interfaces. This allows to listen for specific events and call up the appropriate listener method. The CreateUnoListener function sets a listener interface associated to an UNO object. The listener interface is then bound to its associated object.


    CreateUnoListener( Prefix As String, Typename As String) As Object


Prefix: A text prefix used in BASIC subroutines that handle events.

Typename: A fully qualified UNO listener interface name.

Return value:

The UNO service corresponding to the Typename listener interface name, Null value otherwise.


The following example listens to events occurring for a BASIC library object.

Dim oListener As Object
oListener = CreateUnoListener( "ContListener_","" )

The CreateUnoListener method requires two parameters. The first is Prefix and is explained in detail below. Typename second parameter is the fully qualified name of the listener interface.

Every listener must be registered to LibreOffice broadcaster feature. This is performed by binding each listener to its associated object. Bind methods always follow the pattern 'addFooListener', where 'Foo' is the object type of the listener interface, without the 'X'. In this example, the addContainerListener method is called to register the XContainerListener:

Dim oLib As Object
oLib = BasicLibraries.Library1 ' Library1 devas ekzisti!
oLib.addContainerListener( oListener ) ' Registras la aŭskultanton

The listener is now registered. When an event occurs, the active listener calls the appropriate method defined in interface.

Event-driven registered BASIC subroutines require to use a defined Prefix. The BASIC run-time system searches for subroutines or functions that have the name 'Prefix+ListenerMethod' and calls them when found. Otherwise, a run-time error occurs.

In this example, interface defines the following methods:



disposing base interface for all Listener Interfaces


Method of the interface


Method of the interface


Method of the interface

'ContListener_' used in this example implies that the following subroutines must be implemented in BASIC:

Every listener interface defines a set of controlled event names associated to Uno objects. When an event occurs, it is sent to the method as a parameter. BASIC event methods can also call one another, as long as the appropriate parameter is passed in the Sub declaration. For example:

Sub ContListener_disposing( oEvent As )
    MsgBox "disposing"
End Sub
Sub ContListener_elementInserted( oEvent As )
    MsgBox oEvent.Source.' "elementInserted"
End Sub
Sub ContListener_elementRemoved( oEvent As )
    MsgBox "elementRemoved"
End Sub
Sub ContListener_elementReplaced( oEvent As )
    MsgBox "elementReplaced"
End Sub

Not need to include the event object parameter when not used:

' Minimuma realigo de subproceduro disposing
Sub ContListener_disposing
End Sub

Listener methods must always be implemented to avoid BASIC run-time errors.


Use ScriptForge library console when the BASIC IDE is not easily accessible, that is during events processing. Use the DebugPrint method to add any relevant information to the console. Console entries can be dumped to a text file or visualized in a dialog window. Use Trace module of Access2Base library as an alternative

   Sub SF_Trace
      svc = CreateScriptService("ScriptForge.Exception")
      svc.Console modal:=False
      svc.DebugPrint("Lorem", "Ipsum", "...")
   End Sub ' SF_Trace
   Sub A2B_Trace
      Access2Base.Trace.DebugPrint("Lorem", "Ipsum", "...")
   End Sub ' A2B_Trace

Bonvolu subteni nin!