Contained WithinFind More DocumentationFeatured Support Resources | Download this book in PDF (1223 KB)
Chapter 12 FormsIn many respects, the structure of StarOffice-forms corresponds to the dialogs discussed in the previous chapter. There are, however, a few key differences:
Users who want to provide their forms with their own methods for event handling, should refer to Chapter 11, Dialogs. The mechanisms explained there are identical to those for forms. Working with FormsStarOffice forms may contain text fields, list boxes, radio buttons, and a range of other control elements, which are inserted directly in a text or spreadsheet. The Form FunctionsToolbar is used for editing forms. A StarOffice form may adopt one of two modes: the draft mode and the display mode. In draft mode, the position of control elements can be changed and their properties can be edited using a properties window. The Form Functions Toolbar is also used to switch between modes. Determining Object FormsStarOffice positions the control elements of a form at drawing object level. The actual object form can be accessed through the Forms list at drawing level. The objects are accessed as follows in text documents: Dim Doc As Object Dim DrawPage As Object Dim Form As Object Doc = StarDesktop.CurrentComponent DrawPage = Doc.DrawPage Form = DrawPage.Forms.GetByIndex(0) The GetByIndex method returns the form with the index number 0. When working with spreadsheets, an intermediate stage is needed the Sheets list because the drawing levels are not located directly in the document but in the individual sheets: Dim Doc As Object Dim Sheet As Object Dim DrawPage As Object Dim Form As Object Doc = StarDesktop.CurrentComponent Sheet = Doc.Sheets.GetByIndex(0) DrawPage = Sheet.DrawPage Form = DrawPage.Forms.GetByIndex(0) As is already suggested by the GetByIndex method name, a document may contain several forms. This is useful, for example, if the contents of different databases are displayed within one document, or if a 1:n database relationship is displayed within a form. The option of creating sub-forms is also provided for this purpose. The Three Aspects of a Control Element FormA control element of a form has three aspects:
Accessing the Model of Control Element FormsThe models of the control elements of a form are available through the GetByName method of the Object form: Dim Doc As Object
Dim Form As Object
Dim Ctl As Object
Doc = StarDesktop.CurrentComponent
Form = Doc.DrawPage.Forms.GetByIndex(0)
Ctl = Form.getByName("MyListBox")
The example determines the model of the MyListBox control element, which is located in the first form of the text document currently open. If you is not sure of the form of a control element, you can use the option for searching through all forms for the control element required: Dim Doc As Object
Dim Forms As Object
Dim Form As Object
Dim Ctl As Object
Dim I as Integer
Doc = StarDesktop.CurrentComponent
Forms = Doc.Drawpage.Forms
For I = 0 To Forms.Count - 1
Form = Forms.GetbyIndex(I)
If Form.HasByName("MyListBox") Then
Ctl = Form.GetbyName("MyListBox")
Exit Function
End If
Next I
The example uses the HasByName method to check all forms of a text document to determine whether they contain a control element model called MyListBox. If a corresponding model is found, then a reference to this is saved in the Ctl variable and the search is terminated. Accessing the View of Control Element FormsTo access the view of a control element form, the associated model is first needed. The view of the control element can then be determined with the assistance of the model and using the document controller. Dim Doc As Object Dim DocCrl As Object Dim Forms As Object Dim Form As Object Dim Ctl As Object Dim CtlView As Object Dim I as Integer Doc = StarDesktop.CurrentComponent DocCrl = Doc.getCurrentControler() Forms = Doc.Drawpage.Forms For I = 0 To Forms.Count - 1 Form = Forms.GetbyIndex(I) If Form.HasByName("MyListBox") Then Ctl = Form.GetbyName("MyListBox") CtlView = DocCrl.GetControl(Ctl) Exit Function End If Next I The code listed in the example is very similar to the code listed in the previous example for determining a control element model. It uses not only the Doc document object but also the DocCrl document controller object which makes reference to the current document window. With the help of this controller object and the model of the control element, it then uses the GetControl method to determine the view (CtlView variable) of the control element form. Accessing the Shape Object of Control Element FormsThe method for accessing the shape objects of a control element also uses the corresponding drawing level of the document. To determine a special control element, all drawing elements of the drawing level must be searched through. Dim Doc As Object Dim Shape as Object Dim I as integer Doc = StarDesktop.CurrentComponent For i = 0 to Doc.DrawPage.Count - 1 Shape = Doc.DrawPage(i) If HasUnoInterfaces(Shape, _ "com.sun.star.drawing.XControlShape") Then If Shape.Control.Name = "MyListBox" Then Exit Function End If End If Next The example checks all drawing elements to determine whether they support the com.sun.star.drawing.XControlShape interface needed for control element forms. If this is the case, the Control.Name property then checks whether the name of the control element is MyListBox. If this is true, the function ends the search. Determining the Size and Position of Control ElementsAs already mentioned, the size and position of control elements can be determined using the associated shape object. The control element shape, like all other shape objects, provides the Size and Position properties for this purpose:
The following example shows how the position and size of a control element can be set using the associated shape object: Dim Shape As Object Point.x = 1000 Point.y = 1000 Size.Width = 10000 Size.Height = 10000 Shape.Size = Size Shape.Position = Point The shape object of the control element must already be known if the code is to function. If this is not the case, it must be determined using the preceding code. Control Element Forms in DetailThe control elements available in forms are similar to those of dialogs. The selection ranges from simple text fields through list and combo boxes to various buttons. Below, you will find a list of the most important properties for control element forms. All properties form part of the associated model objects. In addition to the standard control elements, a table control element is also available for forms, which enables the complete incorporation of database tables. This is described in the Database Forms section in Chapter 12, Forms. ButtonsThe model object of a form button provides the following properties:
Through the ButtonType property, you have the opportunity to define an action that is automatically performed when the button is pressed. The associated com.sun.star.form.FormButtonType group of constants provides the following values:
The OK and Cancel button types provided in dialogs are not supported in forms. Option ButtonsThe following properties of an option button are available through its model object:
The mechanism for grouping option buttons distinguishes between the control elements for dialogs and forms. Whereas control elements appearing one after another in dialogs are automatically combined to form a group, grouping in forms is performed on the basis of names. To do this, all option buttons of a group must contain the same name. StarOffice combines the grouped control elements into an array so that the individual buttons of a StarOffice Basic program can be reached in the same way as previously. The following example shows how the model of a control element group can be determined. Dim Doc As Object
Dim Forms As Object
Dim Form As Object
Dim Ctl As Object
Dim I as Integer
Doc = StarDesktop.CurrentComponent
Forms = Doc.Drawpage.Forms
For I = 0 To Forms.Count - 1
Form = Forms.GetbyIndex(I)
If Form.HasByName("MyOptions") Then
Ctl = Form. GetGroupbyName("MyOptions")
Exit Function
End If
Next I
The code corresponds to the previous example for determining a simple control element model. It searches through all forms in the current text document in a loop and uses the HasByName method to check whether the corresponding form contains an element with the MyOptions name it is searching for. If this is the case, then the model array is accessed using the GetGroupByName method (rather than the GetByName method to determine simple models). CheckboxesThe model object of a checkbox form provides the following properties:
Text FieldsThe model objects of text field forms offer the following properties:
List BoxesThe model object of the list box forms provides the following properties:
Note – Through their ValueItemList property, list box forms provide a counterpart to the VBA property, ItemData, through which you can administer additional information for individual list entries. Furthermore, the following methods are provided though the view object of the list box:
Database FormsStarOffice forms can be directly linked to a database. The forms created in this way provide all the functions of a full database front end without requiring independent programming work. The user has the option of paging through and searching the selected tables and queries, as well as changing data records and inserting new data records. StarOffice automatically ensures that the relevant data is retrieved from the database, and that any changes made are written back to the database. A database form basically corresponds to a standard StarOffice form. In addition to the standard properties, the following database-specific properties must also be set in the form:
The com.sun.star.sdb.CommandType enumeration covers the following values:
The database fields are assigned to the individual control elements through this property:
TablesAnother control element is provided for work with databases: the table control element. This represents the content of a complete database table or query. In the simplest scenario, a table control element is linked to a database using the autopilot form, which links all columns with the relevant database fields in accordance with the user specifications. Because the associated API is relatively complex, we shall not provide a complete description of the API at this point. |