Ask Your Question

why is this Edit control event handler not working

asked 2019-11-26 20:38:07 +0200

dave jordan gravatar image

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("")
    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("") = "rbgroup"
    dlgmodel.insertByName(oModel.Name, oModel)

    dim j%

    for j = 0 to 3         ' 3 is for example
        oModel = dlgmodel.createInstance("")
        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("")
        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("")

    Dim oControl As Variant
    oListener = CreateUnoListener("txt_", "")

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

    Dim oWindow As Variant
    oWindow = CreateUnoService("")

    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
edit retag flag offensive close merge delete



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 ->

Ratslinger gravatar imageRatslinger ( 2019-11-26 23:06:22 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2019-11-26 23:00:44 +0200

Ratslinger gravatar image

updated 2019-11-27 01:39:18 +0200


Have gotten this to work. 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.

edit flag offensive delete link more


looks promising, thanks. will follow up tonight or tomorrow

dave jordan gravatar imagedave jordan ( 2019-11-27 13:26:38 +0200 )edit

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( ... end sub

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

dave jordan gravatar imagedave jordan ( 2019-11-28 01:59:34 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-11-26 20:38:07 +0200

Seen: 79 times

Last updated: Nov 27 '19