为什么这个libreoffice Edit控件事件处理程序不起作用

问题描述 投票:0回答:2

我需要以编程方式创建此对话框,因为根据客户端的不同,它将具有可变数量的控件。 (现在的命名约定很草率,因为我正在中间调整其他人的代码。)当输入focusGained子代码时,代码会阻塞(请参阅下文)。

我已经尝试了很多事情,但是特别需要注意的是:如果我更改相关行来代替处理textChanged事件,则所有这些工作都可以按预期进行。

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

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


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

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

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

    oDlg.createPeer(oWindow, null)

    oDlg.execute()
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
events controls libreoffice calc uno
2个回答
0
投票

接口com.sun.star.awt.XFocusListener需要两种方法。您仅实现了其中之一,这就是发生错误的原因。

要修复它,请添加以下内容:

sub txt_focusLost(event as object)
    print "txt2" 
end sub

但是,您确定要焦点监听器吗?正如您将看到的那样,修改后的代码将导致无限循环。焦点通常比较棘手,并且根据操作系统的不同而有所不同。通常我改用textChanged


© www.soinside.com 2019 - 2024. All rights reserved.