Difference between revisions of "Documentation/BASIC Guide/Working With Forms"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (changed getCurrentControler to getCurrentController (added an L))
m
 
(5 intermediate revisions by 4 users not shown)
Line 8: Line 8:
 
{{DISPLAYTITLE:Working with Forms}}
 
{{DISPLAYTITLE:Working with Forms}}
 
__NOTOC__  
 
__NOTOC__  
{{OOo}} 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 Functions Toolbar is used for editing forms.
+
{{AOo}} 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 Functions Toolbar is used for editing forms.
  
A {{OOo}} 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.
+
A {{AOo}} 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.
 
The Form Functions Toolbar is also used to switch between modes.
Line 16: Line 16:
 
== Determining Object Forms ==
 
== Determining Object Forms ==
  
{{OOo}} positions the control elements of a form at drawing object level. The actual object form can be accessed through the Forms list at the drawing level. The objects are accessed as follows in text documents:
+
{{AOo}} positions the control elements of a form at drawing object level. The actual object form can be accessed through the Forms list at the drawing level. The objects are accessed as follows in text documents:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim Doc As Object
 
Dim Doc As Object
 
Dim DrawPage As Object
 
Dim DrawPage As Object
 
Dim Form As Object
 
Dim Form As Object
  
Doc = StarDesktop.CurrentComponent
+
Doc = ThisComponent
 
DrawPage = Doc.DrawPage
 
DrawPage = Doc.DrawPage
 
Form = DrawPage.Forms.GetByIndex(0)
 
Form = DrawPage.Forms.GetByIndex(0)
</source>
+
</syntaxhighlight>
  
 
The <tt>GetByIndex</tt> method returns the form with the index number 0.
 
The <tt>GetByIndex</tt> method returns the form with the index number 0.
Line 32: Line 32:
 
When working with spreadsheets, an intermediate stage is needed for the Sheets list because the drawing levels are not located directly in the document but in the individual sheets:
 
When working with spreadsheets, an intermediate stage is needed for the Sheets list because the drawing levels are not located directly in the document but in the individual sheets:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim Doc As Object
 
Dim Doc As Object
 
Dim Sheet As Object
 
Dim Sheet As Object
Line 38: Line 38:
 
Dim Form As Object
 
Dim Form As Object
  
Doc = StarDesktop.CurrentComponent
+
Doc = ThisComponent
 
Sheet = Doc.Sheets.GetByIndex(0)
 
Sheet = Doc.Sheets.GetByIndex(0)
 
DrawPage = Sheet.DrawPage
 
DrawPage = Sheet.DrawPage
 
Form = DrawPage.Forms.GetByIndex(0)
 
Form = DrawPage.Forms.GetByIndex(0)
</source>
+
</syntaxhighlight>
  
 
As is already suggested by the <tt>GetByIndex</tt> 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.
 
As is already suggested by the <tt>GetByIndex</tt> 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.
Line 50: Line 50:
 
A control element of a form has three aspects:
 
A control element of a form has three aspects:
  
* The '''Model''' of the control element is the key object for the {{OOo}} Basic-programmer when working with control element forms.
+
* The '''Model''' of the control element is the key object for the {{AOo}} Basic-programmer when working with control element forms.
 
* The counterpart to this is the '''View''' of the control element, which administers the display information.
 
* The counterpart to this is the '''View''' of the control element, which administers the display information.
 
* Since control element forms within the documents are administered like a special drawing element, there is also a '''Shape object''' which reflects the drawing element-specific properties of the control element (in particular its position and size).
 
* Since control element forms within the documents are administered like a special drawing element, there is also a '''Shape object''' which reflects the drawing element-specific properties of the control element (in particular its position and size).
Line 58: Line 58:
 
The models of the control elements of a form are available through the <tt>GetByName</tt> method of the Object form:
 
The models of the control elements of a form are available through the <tt>GetByName</tt> method of the Object form:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim Doc As Object
 
Dim Doc As Object
 
Dim Form As Object
 
Dim Form As Object
 
Dim Ctl As Object
 
Dim Ctl As Object
  
Doc = StarDesktop.CurrentComponent
+
Doc = ThisComponent
 
Form = Doc.DrawPage.Forms.GetByIndex(0)
 
Form = Doc.DrawPage.Forms.GetByIndex(0)
 
Ctl = Form.getByName("MyListBox")
 
Ctl = Form.getByName("MyListBox")
</source>
+
</syntaxhighlight>
  
 
The example determines the model of the <tt>MyListBox</tt> control element, which is located in the first form of the text document currently open.
 
The example determines the model of the <tt>MyListBox</tt> control element, which is located in the first form of the text document currently open.
  
If you are not sure of the form of a control element, you can use the option for searching through all forms for the control element required:
+
If you are not sure of the form of a control element, you can use the option for searching through all forms for the control element required.
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim Doc As Object
 
Dim Doc As Object
 
Dim Forms As Object
 
Dim Forms As Object
Line 79: Line 79:
 
Dim I as Integer
 
Dim I as Integer
  
Doc = StarDesktop.CurrentComponent
+
Doc = ThisComponent
 
Forms = Doc.Drawpage.Forms
 
Forms = Doc.Drawpage.Forms
  
Line 89: Line 89:
 
   End If
 
   End If
 
Next I
 
Next I
</source>
+
</syntaxhighlight>
  
The example uses the <tt>HasByName</tt> method to check all forms of a text document to determine whether they contain a control element model called <tt>MyListBox</tt>. If a corresponding model is found, then a reference to this is saved in the <tt>Ctl</tt> variable and the search is terminated.
+
The example uses the <tt>HasByName</tt> method to check all forms of a text document to determine whether they contain a control element model called <tt>MyListBox</tt>. If a corresponding model is found, then a reference to this is saved in the <tt>Ctl</tt> variable and the search is terminated. The example is shown as in-line code, for clarity; in practice, the search would be inside a function, called with the name as a string parameter, and include error processing.
  
 
== Accessing the View of Control Element Forms ==
 
== Accessing the View of Control Element Forms ==
Line 97: Line 97:
 
To access the view of a control element form, you need the associated model. The view of the control element can then be determined with the assistance of the model and using the document controller.
 
To access the view of a control element form, you need the associated model. The view of the control element can then be determined with the assistance of the model and using the document controller.
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim Doc As Object
 
Dim Doc As Object
 
Dim DocCrl As Object
 
Dim DocCrl As Object
Line 106: Line 106:
 
Dim I as Integer
 
Dim I as Integer
  
Doc = StarDesktop.CurrentComponent
+
Doc = ThisComponent
 
DocCrl = Doc.getCurrentController()
 
DocCrl = Doc.getCurrentController()
 
Forms = Doc.Drawpage.Forms
 
Forms = Doc.Drawpage.Forms
Line 118: Line 118:
 
   End If
 
   End If
 
Next I
 
Next I
</source>
+
</syntaxhighlight>
  
 
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 <tt>Doc</tt> document object but also the <tt>DocCrl</tt> 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 <tt>GetControl</tt> method to determine the view (<tt>CtlView</tt> variable) of the control element form.
 
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 <tt>Doc</tt> document object but also the <tt>DocCrl</tt> 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 <tt>GetControl</tt> method to determine the view (<tt>CtlView</tt> variable) of the control element form.
Line 126: Line 126:
 
The 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.
 
The 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.
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim Doc As Object
 
Dim Doc As Object
 
Dim Shape as Object
 
Dim Shape as Object
 
Dim I as integer
 
Dim I as integer
  
Doc = StarDesktop.CurrentComponent
+
Doc = ThisComponent
  
 
For i = 0 to Doc.DrawPage.Count - 1
 
For i = 0 to Doc.DrawPage.Count - 1
Line 141: Line 141:
 
   End If
 
   End If
 
Next
 
Next
</source>
+
</syntaxhighlight>
  
 
The example checks all drawing elements to determine whether they support the <idl>com.sun.star.drawing.XControlShape</idl> interface needed for control element forms. If this is the case, the <tt>Control.Name</tt> property then checks whether the name of the control element is <tt>MyListBox</tt>. If this is true, the function ends the search.
 
The example checks all drawing elements to determine whether they support the <idl>com.sun.star.drawing.XControlShape</idl> interface needed for control element forms. If this is the case, the <tt>Control.Name</tt> property then checks whether the name of the control element is <tt>MyListBox</tt>. If this is true, the function ends the search.
Line 154: Line 154:
 
The following example shows how the position and size of a control element can be set using the associated shape object:
 
The following example shows how the position and size of a control element can be set using the associated shape object:
  
<source lang="oobas">
+
<syntaxhighlight lang="oobas">
 
Dim Shape As Object
 
Dim Shape As Object
 
Dim Point As New com.sun.star.awt.Point
 
Dim Point As New com.sun.star.awt.Point
Line 168: Line 168:
 
Shape.Size = Size
 
Shape.Size = Size
 
Shape.Position = Point
 
Shape.Position = Point
</source>
+
</syntaxhighlight>
  
 
The <tt>shape</tt> 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.
 
The <tt>shape</tt> 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.

Latest revision as of 14:30, 30 January 2021


Apache OpenOffice 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 Functions Toolbar is used for editing forms.

A Apache OpenOffice 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 Forms

Apache OpenOffice positions the control elements of a form at drawing object level. The actual object form can be accessed through the Forms list at the drawing level. The objects are accessed as follows in text documents:

Dim Doc As Object
Dim DrawPage As Object
Dim Form As Object
 
Doc = ThisComponent
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 for 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 = ThisComponent
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 Form

A control element of a form has three aspects:

  • The Model of the control element is the key object for the Apache OpenOffice Basic-programmer when working with control element forms.
  • The counterpart to this is the View of the control element, which administers the display information.
  • Since control element forms within the documents are administered like a special drawing element, there is also a Shape object which reflects the drawing element-specific properties of the control element (in particular its position and size).

Accessing the Model of Control Element Forms

The 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 = ThisComponent
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 are 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 = ThisComponent
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. The example is shown as in-line code, for clarity; in practice, the search would be inside a function, called with the name as a string parameter, and include error processing.

Accessing the View of Control Element Forms

To access the view of a control element form, you need the associated model. 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 = ThisComponent
DocCrl = Doc.getCurrentController()
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 Forms

The 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 = ThisComponent
 
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 Elements

As 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:

Size (struct)
size of control element (com.sun.star.awt.Size data structure)
Position (struct)
position of control element (com.sun.star.awt.Point data structure)

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
Dim Point As New com.sun.star.awt.Point
Dim Size As New com.sun.star.awt.Size
 
' ... Initialize Shape object, as previously shown ...
 
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.


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