Documentation/BASIC Guide/Working With Dialogs

From Apache OpenOffice Wiki
< Documentation‎ | BASIC Guide
Revision as of 13:43, 28 September 2007 by Fpe (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Working With Dialogs

Apache OpenOffice Basic dialogs consist of a dialog window that can contain text fields, list boxes, radio buttons, and other control elements.

Creating Dialogs

You can create and structure dialogs using the Apache OpenOffice dialog editor that you can use in the same way as a Apache OpenOffice Draw:

Documentation basicguide dlg 01.gif

Essentially, you drag the control elements that you want from the design pallet (right) into the dialog area where you can define their position and size.

The example shows a dialog that contains a label and a list box.

Documentation basicguide dlg 04.gif

You can open a dialog with the following code:

Dim Dlg As Object

DialogLibraries.LoadLibrary("Standard")
Dlg = CreateUnoDialog(DialogLibraries.Standard.DlgDef)
Dlg.Execute()
Dlg.dispose()

CreateUnoDialog creates an object called Dlg that references the associated dialog. Before you can create the dialog, you must ensure that the library it uses (in this example, the Standard library) is loaded. If not, the LoadLibrary method performs this task.

Once the Dlg dialog object has been initialized, you can use the Execute method to display the dialog. Dialogs such as this one are described as modal because they do not permit any other program action until they are closed. While this dialog is open, the program remains in the Execute call.

The dispose method at the end of the code approves the resources used by the dialog once the program ends.

Closing Dialogs

Closing With OK or Cancel

If a dialog contains an OK or a Cancel button, the dialog is automatically closed when you press one of these buttons. More information about working with these buttons are discussed in this Dialog Control Elements in Detail section of this chapter.

If you close a dialog by clicking the OK button, the Execute-method returns a return value of 1, otherwise a value of 0 is returned.

Dim Dlg As Object

DialogLibraries.LoadLibrary("Standard")
Dlg = CreateUnoDialog(DialogLibraries.Standard.MyDialog)
Select Case Dlg.Execute() 
Case 1
  MsgBox "Ok pressed"
Case 0 
  MsgBox "Cancel pressed"
End Select

Closing With the Close Button in the Title Bar

If you want, you can close a dialog by clicking the close button on the title bar of the dialog window. In this instance, the Execute method of the dialog returns the value 0, the same as when you press the Cancel button.

Closing With an Explicit Program Call

You can also close an open dialog window with the endExecute method:

Dlg.endExecute()

Access to Individual Control Elements

A dialog can contain any number of control elements. You can access these elements through the getControl method that returns the name of the control element.

Dim Ctl As Object

Ctl = Dlg.getControl("MyButton")
Ctl.Label = "New Label"

This code determines the object for the MyButton control element and then initializes the Ctl object variable with a reference to the element. Finally the code sets the Label property of the control element to the New Label value.

Template:Documentation/Note

Working With the Model of Dialogs and Control Elements

The division between visible program elements (View) and the data or documents behind them (Model) occurs at many places in Apache OpenOffice API. In addition to the methods and properties of control elements, both dialog and control element objects have a subordinate Model object. This object allows you to directly access the content of a dialog or control element.

In dialogs, the distinction between data and depiction is not always as clear as in other API areas of Apache OpenOffice. Elements of the API are available through both the View and the Model.

The Model property provides program-controlled access to the model of dialog and control element objects.

Dim cmdNext As Object

cmdNext = Dlg.getControl("cmdNext")
cmdNext.Model.Enabled = False

This example deactivates the cmdNtext button in the Dlg dialog with the aid of the model object from cmdNtext.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools