OO-Snippets: set event dynamically

Commons

KeywordsButton, Event, Dynamic, Persistant
LanguageOOBasic
ApplicationOffice
AuthorsOliver Brinzing (initial)
Laurent Godard
Tom Schindl
Supported Versions
Supported OS
Question I have a lot of controls (250 buttons) in a document and want to set their "when intiating" events dynamically

I want to do this only once on building the document so that, when the document is reloaded the events are still there.

(like if i do it manually with right click on the control and assign manually)

Answer

thanks to Oliver Brinzing

More than one vent can be registered even if the UI in design mode shows the last one

Code-Snippet-Listing (snippet-source)

Option Explicit

Sub SetEvent

    Dim oDocument as Object
    Dim oView as Object
    Dim oDrawPage as Object
    Dim oForm as Object
    Dim oEvents(0) as New com.sun.star.script.ScriptEventDescriptor

    oDocument = StarDesktop.getCurrentComponent
    oView = oDocument.CurrentController
    oDrawPage = oView.getActiveSheet.DrawPage

    ' get the first form
    oForm = oDrawPage.getForms.getByIndex(0)

    oEvents(0).ListenerType = "XActionListener"
    oEvents(0).EventMethod  = "actionPerformed"
    oEvents(0).AddListenerParam = ""
    oEvents(0).ScriptType = "StarBasic"

    oEvents(0).ScriptCode = "document:Standard.Module1.Test"
    oForm.registerScriptEvent(0, oEvents(0))
End Sub


Sub Test(oEvt)
    Print oEvt.Source.Model.Name
End Sub

Changelog

DateUserModification
2004-06-22tomsontomModified to match new snippet-DTD
0000-00-00oliInitial version

and