简体   繁体   English

使用LibreOffice Basic遍历控件

[英]Iterate over Controls with LibreOffice Basic

I'd like to iterate over Controls in a LibreOffice form with Basic. 我想使用Basic遍历LibreOffice表单中的控件。

Basically, I'd like to do what this code does in VBA. 基本上,我想做这段代码在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

EDIT : Here is what I found with Lyrl's help. 编辑:这是我在Lyrl的帮助下发现的。 That's not totally correct yet. 这还不完全正确。 I'm not able to get the labels of controls. 我无法获得控件的标签。

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

Note: this code was tested in OpenOffice. 注意:此代码已在OpenOffice中测试。 I believe it will work the same way in LibreOffice. 我相信它将在LibreOffice中以相同的方式工作。

Controls are located on the drawpage. 控件位于图纸上。 If there might be non-control drawpage objects (arrows, shapes, images, etc.) and you want to operate just on controls, you have to iterate through all the draw objects and test each one for being a control: 如果可能存在非控件绘制对象(箭头,形状,图像等),并且您想只对控件进行操作,则必须遍历所有绘制对象并测试每个控件是否为控件:

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

Edit: I looked at the object "cControl" with XRay. 编辑:我用XRay看了对象“ cControl”。 Looked through the properties, and nothing looked useful. 浏览属性,看起来没有任何用处。 I then went to methods and found a method "getModel". 然后,我转到方法并找到方法“ getModel”。 Double-clicked on getModel to XRay that method, and found "Label" with the name I'd given the checkbox. 双击getModel以对该方法进行XRay处理,并找到名为“ Label”的名称,我将其命名为复选框。 Woot! 活泉! (I've worked with other objects that have certain properties only accessible through a "model"; it's not an intuitive place to look.) (我与其他具有某些属性的对象一起使用,这些对象只能通过“模型”访问;这不是直观的地方。)

So try this: 所以试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM