Events raised by dialogs, documents, forms or graphical controls can be linked to macros, which is referred to as event-driven programming. The most common method to relate events to macros are the Events tab in menu and the Dialog Editor Control properties pane from menu.Graphical artifacts, keyboard inputs, mouse moves and other man/machine interactions can be controlled using UNO listeners that watch for the user’s behaviour. Listeners are dynamic program code alternatives to macro assignments. One may create as many UNO listeners as events to watch for. A single listener can also handle multiple user interface controls.
Creating an event listener
Listeners get attached to controls held in dialogs, as well as to document or form events. Listeners are also used when creating runtime dialogs or when adding controls to a dialog on the fly.This example creates a listener for Button1 control of Dialog1 dialog in Standard library.
Option ExplicitConst MY_LIBRARY = "Standard", MY_DIALOG = "Dialog1", MY_BUTTON = "Button1"Const MY_LABEL = "Basic listens.."Dim count As IntegerSub Main Dim libr As Object ' com.sun.star.script.XLibraryContainer Dim dlg As Object Dim ui As Object ' stardiv.Toolkit.UnoDialogControl Dim ctl As Object ' stardiv.Toolkit.UnoButtonControl Dim act As Object ' com.sun.star.awt.XActionListener Dim rc As Object : rc = com.sun.star.ui.dialogs.ExecutableDialogResults BasicLibraries.LoadLibrary(MY_LIBRARY) libr = DialogLibraries.GetByName(MY_LIBRARY) dlg = libr.GetByName(MY_DIALOG) ui = CreateUnoDialog(dlg) ui.Title = "Basic X[any]Listener example" count = 0 ctl = ui.GetControl(MY_BUTTON) ctl.Model.Label = MY_LABEL act = CreateUnoListener("awt_", "com.sun.star.awt.XActionListener") ctl.addActionListener(act) Select Case ui.Execute Case rc.OK : MsgBox "The user acknowledged the dialog.",, "Basic" Case rc.CANCEL : MsgBox "The user canceled the dialog.",, "Basic" End Select ui.dispose ' ui.endExecute() ctl.removeActionListener(act)End SubPrivate Sub awt_actionPerformed(evt As com.sun.star.awt.ActionEvent) ''' Listen to & count button clicks ''' With evt.Source.Model If .Name = MY_BUTTON Then count = count + 1 .Label = MY_LABEL+Cstr(count) End If End WithEnd Sub ' awt_actionPerformedPrivate Sub awt_disposing(evt As com.sun.star.lang.EventObject) ' mandatory Sub ' your code goes hereEnd Sub ' awt_disposing
Other Event Listeners
Listeners are usually coded along with dialog opening. Numerous listener approaches are possible such as event handlers for dialogs or event monitors for documents or forms.CreateUnoListener FunctionEvents mapping to objectsSee also Document events, Form events.