Programming Examples for Controls in the Dialog Editor

以下示例用於一個名為「Dialog1」的新對話方塊。在對話方塊編輯器中,使用 [工具箱] 列中的工具建立對話方塊,並在其中增加以下控制項:[核取方塊](名為「CheckBox1」)、[標籤欄位](名為「Label1」)、[指令按鈕](名為「CommandButton1」) 以及 [清單方塊](名為「ListBox1」)。

警告圖示

將控制項附加到物件型變數時,請使字母大小寫保持一致。


用於載入對話方塊的全域函式


Function LoadDialog(Libname as String, DialogName as String, Optional oLibContainer)
Dim oLib as Object ' com.sun.star.script.XLibraryContainer
Dim oLibDialog as Object
Dim oRuntimeDialog as Object
    If IsMissing(oLibContainer) Then
        oLibContainer = DialogLibraries
    End If
    oLibContainer.LoadLibrary(LibName)
    oLib = oLibContainer.GetByName(Libname)
    oLibDialog = oLib.GetByName(DialogName)
    oRuntimeDialog = CreateUnoDialog(oLibDialog)
    LoadDialog() = oRuntimeDialog
End Function

LoadDialog function is stored in Tools.ModuleControls available from LibreOffice Macros and Dialogs.

顯示對話方塊


rem 變數的全域定義
Dim oDialog1 AS Object
Sub StartDialog1
    With GlobalScope.BasicLibraries
       If Not .IsLibraryLoaded("Tools") Then .LoadLibrary("Tools")
    End With
    oDialog1 = Tools.ModuleControls.LoadDialog("Standard", "Dialog1")
    oDialog1.Execute()
End Sub

在程式中讀取或編輯控制項的屬性


Sub Sample1
    With GlobalScope.Basiclibraries
       If Not .IsLibraryLoaded("Tools") Then .LoadLibrary("Tools")
    End With
    oDialog1 = Tools.LoadDialog("Standard", "Dialog1")
    REM 獲取對話方塊模型
    oDialog1Model = oDialog1.Model
    REM 顯示 Label1 的文字
    oLabel1 = oDialog1.GetControl("Label1")
    MsgBox oLabel1.Text
    REM 為控制項 Label1 設定新文字
    oLabel1.Text = "New Files"
    REM 顯示控制項 CheckBox1 的模型屬性
    oCheckBox1Model = oDialog1Model.CheckBox1
    MsgBox oCheckBox1Model.Dbg_Properties
    REM 為 CheckBox1 的控制項模型設定新狀態
    oCheckBox1Model.State = 1
    REM 顯示控制項 CommandButton1 的模型屬性
    oCMD1Model = oDialog1Model.CommandButton1
    MsgBox oCMD1Model.Dbg_Properties
    REM 顯示控制項 CommandButton1 的屬性
    oCMD1 = oDialog1.GetControl("CommandButton1")
    MsgBox oCMD1.Dbg_Properties
    REM 執行對話方塊
    oDialog1.Execute()
End Sub

將條目加入 ListBox 中


Sub AddEntry
    With GlobalScope.Basiclibraries
       If Not .IsLibraryLoaded("Tools") Then .LoadLibrary("Tools")
    End With
    oDialog1 = ModuleControls.LoadDialog("Standard", "Dialog1")
    REM 將條目加入 ListBox 中
    oDialog1Model = oDialog1.Model
    oListBox = oDialog1.GetControl("ListBox1")
    Dim iCount as integer
    iCount = oListbox.ItemCount
    oListbox.additem("New Item" & iCount,0)
End Sub

移除 ListBox 中的條目


Sub RemoveEntry
    With GlobalScope.Basiclibraries
       If Not .IsLibraryLoaded("Tools") Then .LoadLibrary("Tools")
    End With
    oDialog1 = Tools.ModuleControls.LoadDialogLoadDialog("Standard", "Dialog1")
    REM 移除 ListBox 中的第一個條目
    oDialog1Model = oDialog1.Model
    oListBox = oDialog1.GetControl("ListBox1")
    oListbox.removeitems(0,1)
End Sub

Please support us!