【问题标题】:why is this libreoffice Edit control event handler not working为什么这个 libreoffice 编辑控件事件处理程序不起作用
【发布时间】:2020-03-21 02:55:15
【问题描述】:

我需要以编程方式创建此对话框,因为它的控件数量取决于客户端。 (现在的命名约定很草率,因为我在中间改编别人的代码。)当输入 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


【解决方案1】:

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

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

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

但是,您确定要使用焦点侦听器吗?正如您在运行它时所看到的,修改后的代码会导致无限循环。焦点通常很棘手,并且根据操作系统的不同而工作方式不同。通常我会使用textChanged

【讨论】:

  • 缺少 focusLost() 绝对是问题的主要部分
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 2018-06-09
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多