why is this Edit control event handler not working

I need to create this dialog programatically because it will have a variable number of controls depending on the client. (The naming conventions are sloppy right now because i am in the middle adapting someone else’s code.) The code chokes when the focusGained sub is entered (see below).

I have tried many things but especially of note is: if i change the relevant lines to deal with the textChanged event instead, it all works as expected.

Sub main

    Dim dlgmodel As Variant
    Dim oComponents As Variant
    Dim oDoc As Variant

    dlgmodel = CreateUnoService("com.sun.star.awt.UnoControlDialogModel")
    With dlgmodel
        .Name = "checkwriter"
        .Title = "check writer"
        .PositionX = 170
        .PositionY = 70
        .Width = 190
        .Height = 100
        .DesktopAsParent = false ' or true, does not affect problem
    End With

    Dim oModel As Variant

    oModel = dlgmodel.createInstance("com.sun.star.awt.UnoControlGroupBoxModel")
    omodel.name = "rbgroup"
    dlgmodel.insertByName(oModel.Name, oModel)

    dim j%

    for j = 0 to 3         ' 3 is for example
        oModel = dlgmodel.createInstance("com.sun.star.awt.UnoControlRadioButtonModel")
        With oModel
            .Name = "rb" & j
            .PositionX = 10
            .PositionY = 6 + j * 15
            .Width = 12
            .Height = 12
            .groupname = "rbgroup"
        End With
        dlgmodel.insertByName(oModel.Name, oModel)

        oModel = dlgmodel.createInstance("com.sun.star.awt.UnoControlEditModel")
        with omodel
            .Name = "txt" & j
            .PositionX = 40
            .PositionY = 6 + j * 15
            .Width = 40
            .Height = 12
        end with
        dlgmodel.insertByName(oModel.Name, oModel)

    Dim oDlg As Variant
    oDlg = CreateUnoService("com.sun.star.awt.UnoControlDialog")

    Dim oControl As Variant
    oListener = CreateUnoListener("txt_", "com.sun.star.awt.XFocusListener")

    oControl = oDlg.getControl("txt0")           ' testing one single edit control

    Dim oWindow As Variant
    oWindow = CreateUnoService("com.sun.star.awt.Toolkit")

    oDlg.createPeer(oWindow, null)  
End Sub

'entering focusGained() causes
' "BASIC runtime error. Property or method not found: $(ARG1)."
' after clearing that, the print statement executes.
' ***warning*** without the print statement the dialog will become uncloseable.

sub txt_focusGained(event as object)
    print "txt1" 
end sub


Please note: it is not good manners to cross post a question without noting it and listing the link. Please do this in the other post! This can cause needless duplicate effort if solved elsewhere.

Also posted here → why is this libreoffice Edit control event handler not working - Stack Overflow


Have gotten this to work. com.sun.star.awt.XFocusListener has three events: focusGained; focusLost; disposing.

If both focusGained and focusLost are not defined you get the error. Don’t need to contain anything, just defined. disposing doesn’t seem to matter but an empty sub is what I usually provide.

Now testing with a Print or MsgBox statement presents another problem. When the message appears, do not close but rather select the dialog again then hit the enter key. The message closes and you can enter int the field on the dialog.

For a better test I simply moved some text into the field. Made oDlg local instead of in the sub and used these:

sub txt_focusGained(event)
  REM    MsgBox "txtGained" 
    oControl = oDlg.getControl("txt0")
    oControl.Text = "Hello"
end sub

sub txt_focusLost(event)
 REM    MsgBox "txtLost" 
   oControl = oDlg.getControl("txt0")
   oControl.Text = "Good-Bye"
end sub

sub txt_disposing(event)
end sub

Also changed the text again when losing focus.

Edit 2019-11-26:

Just recalled seeing this and it may be part of the reasoning behind all this. Bug tdf#128988

Possibly the event is called twice causing both the gained and lost events. Just a thought.

looks promising, thanks. will follow up tonight or tomorrow

solution works for me. many thanks.
i had defined txt_focusLost() etc but i think i forgot to add the prefix to the sub name so it didn’t fix anything and then i erased them.

its probably worth mentioning for posterity that you can get reference to the control in the handler sub thusly:

sub txt_focusGained(event as object)
control = oDlg.getcontrol(event.source.model.name)

end sub

may be useful if you have multiple controls using the same handler logic