Attach macro to Date Field's Text modified event in basic.

Hello. I want to attach my macro to “Text modified” event of Date Field in Calc spreadsheet. Example code looks like this:

Sub test()
    oDoc = ThisComponent
    oSheet = oDoc.Sheets(0)
    oForm = oSheet.drawPage.getForms().getByIndex(0)
    oDateField = oForm.getByName("DateField1")
    ' Help me here!
    ' I need to attach runMe() to "text modified" event of oDateField
End Sub

Sub runMe()
    MsgBox "Hurray!"
End Sub

I don’t really know where to start with this. I tried reading docs on listeners, but tbh, the whole concept remains very unclear to me.

I’ve got to the point of something like this:

Sub test()
    oDoc = ThisComponent
    oSheet = oDoc.Sheets(0)
    oForm = oSheet.drawPage.getForms().getByIndex(0)
    oDateField = oForm.getByName("DateField1")
    oListener = createUnoListener("Event_", "com.sun.star.beans.XPropertyChangeListener")
    oDateField.addPropertyChangeListener("", oListener)
End Sub

Sub Event_propertyChange(oEvent)
    Globalscope.BasicLibraries.LoadLibrary( "MRILib" )
    Mri oEvent
End Sub

And it works fine! :slight_smile:

Now I just would like to know a way to list/detach listeners from object.

Hello,

First, it is not apparent why you just don’t use the Text modified event in the properties of the date control. Just point directly to your macro.

However, if you are intent on creating a listener, hopefully you have reviewed the topic in OOME (by Pitonyak). If not, you can get PDF here → OOME PDF. See section 10.12. UNO listeners on page 252 for info. Also, in my answer on this post → How to properly code a broadcaster and listener in LO Basic, I have copied some necessary code from OOME & also created a Key Handler.

Your listener is missing a few key elements. Using the Pitonyak code produces the following methods for com.sun.star.beans.XPropertyChangeListener:

SbxEMPTY queryInterface ( SbxOBJECT )
SbxVOID disposing ( SbxOBJECT )
SbxVOID propertyChange ( SbxOBJECT ) 

queryInterface can be ignored but the other two are necessary along with a method to start (this you have) and a method to stop the listener. All necessary methods should be included even if they contain nothing. You had the propertyChange method but not the disposing method. Another missing element is for the listener to be in a global variable to verify if it is active or not.

Here are the working methods I have created based upon your code:

 global oListener as object

Sub test()
    oDoc = ThisComponent
    oSheet = oDoc.Sheets(0)
    oForm = oSheet.drawPage.getForms().getByIndex(0)
    oDateField = oForm.getByName("DateField1")
    oListener = createUnoListener("Event_", "com.sun.star.beans.XPropertyChangeListener")
 REM Changed to just look for modified Text
    oDateField.addPropertyChangeListener("Text",oListener)
End Sub

Sub sStopTest
    if not IsNull(oListener) then 'only if still running'
        oDoc = ThisComponent
        oSheet = oDoc.Sheets(0)
        oForm = oSheet.drawPage.getForms().getByIndex(0)
        oDateField = oForm.getByName("DateField1")
        oDateField.removePropertyChangeListener("Text",oListener)
        oListener = Nothing    'To know later this handler has stopped'
    end if
End Sub

Sub Event_disposing(oEvent)
End Sub

Sub Event_propertyChange(oEvent)
    Globalscope.BasicLibraries.LoadLibrary( "MRILib" )
    Mri oEvent
End Sub

These methods are only for this listener. If you create other listeners they must include the methods required for the particular listener.

Edit:

It is not certain this is what you need to accomplish your task since you never stated just what you are attempting to do.