【问题标题】:Iterate over Controls with LibreOffice Basic使用 LibreOffice Basic 迭代控件
【发布时间】:2015-04-15 14:10:51
【问题描述】:

我想用 Basic 迭代 LibreOffice 表单中的控件。

基本上,我想做这段代码在 VBA 中所做的事情。

Sub parcours_controles_formulaire()
    Dim cControl As Control
    Dim sLog As String
    sLog = "List of controls : " & vbCrLf 
    For Each cControl In FrmExemplesControles.Controls
        sLog = sLog & _
        cControl.Name & _
        " of type " & _
        TypeName(cControl) & vbCrLf
    Next cControl
    MsgBox sLog
End Sub

编辑:这是我在 Lyrl 的帮助下找到的。这还不完全正确。我无法获取控件的标签。

Sub iterate_forms_controls()
    Dim Dlg As Object
    Dim Controls As Object
    Dim cControl As Object
    Dim I As Integer
    Dim A As String

    DialogLibraries.LoadLibrary("Standard")
    Dlg = CreateUnoDialog(DialogLibraries.Standard.BoiteDeDialogue1)

    Controls = Dlg.Controls

    I = 0
    A = ""
    For Each cControl In Controls
        I = I + 1
        A = A & cControl.getImplementationName()
        'A = A & cControl  ' How to get back the label of cControl here ?
    Next cControl

    MsgBox "There is " & i & " controls in that form !" & A
End Sub

【问题讨论】:

    标签: vba controls libreoffice basic libreoffice-basic


    【解决方案1】:

    注意:此代码已在 OpenOffice 中测试。我相信它在 LibreOffice 中也会以同样的方式工作。

    控件位于绘图页上。如果可能存在非控制绘图对象(箭头、形状、图像等)并且您只想对控件进行操作,则必须遍历所有绘图对象并测试每个对象是否为控件:

    Sub iterate_forms_controls()
        Dim oDP As Object : oDP = ThisComponent.drawpage
        Dim cControl As Object
        Dim i As Integer
    
        REM The oDP assignment above is for a standard form (a Writer document).
        REM If you are using a Calc document as a form you would instead write:
        REM oDP = ThisComponent.Sheets.getByName("SheetName").drawpage
    
        For i = 0 To oDP.Count - 1
        cControl = oDP.getByIndex(i)
            If cControl.supportsService("com.sun.star.drawing.ControlShape") Then
                'Do something
            End If
        Next i
    End Sub
    

    编辑:我用 XRay 查看了对象“cControl”。浏览了属性,没有任何有用的东西。然后我去了方法,找到了一个方法“getModel”。双击 getModel 到 XRay 那个方法,然后找到“标签”,它的名称是我在复选框中指定的名称。哇! (我使用过其他具有某些属性只能通过“模型”访问的对象;这不是一个直观的查看位置。)

    所以试试这个:

    For Each cControl In Controls
        I = I + 1
        A = A & cControl.getModel.Label
    Next cControl
    

    【讨论】:

    • 谢谢@Lyrl,但这并不是我的意思。它帮助我进步,现在这是我的代码。只有两行仍然错误。
    • 我不熟悉标签的 API 名称。我建议使用对象检查工具:他们检查您提供给它的对象(在这里您将提供 cControl)并返回所有对象的属性和方法。您可以滚动浏览属性列表并找到标签的 API 名称。 Open/LibreOffice 的两个好方法是 Xray (link) 和 MRI (link) 另请参阅此 MRI 教程:link
    • 我尝试过使用 Xray。它没有帮助:(。我稍后会尝试 MRI。谢谢。
    • 我已经用一些附加信息编辑了我的原始答案。
    • 太棒了。我们可以在拥有标签的控件上使用cControl.getModel().Label,在所有控件上使用cControl.getModel().Name!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    相关资源
    最近更新 更多