5.4. Designing Forms

5.4.1. Most important terms

5.4.2. Forms versus tables

In chapter 5.2 you learned about how to enter data directly into tables using their data sheet view. However, in many cases forms are better suited for data entry:

5.4.3. Working with form design

As with table or query design, you are able to use Data View and Design View. Form designing is performed in Design View. We will ofter refer to form design window as to Form Designer.

  1. To create a new empty form, select Insert -> Form from the Menubar. Optionally, you can use New Form command from drop-down button on the Project Navigator's toolbar.
  2. A new window with empty rectangular from surface will appear. Beyond the form surface outer area is placed, separated from the surface using borders. You can move the borders to resize the form surface. The surface is covered with a grid painted using dots which simplifies accurate widgets positioning.



    A window with design of a new form

As with table design, Form Designer provides Property pane. To save some space on the screen, the pane has been splitted with three tabs related to the currently selected form:

  1. Property tab containing a list of properties for the currently selected widgets
  2. Data source tab containing properties related specifically to data source of the currently selected widget or the form itself.
  3. Widgets tab containing a hierarchy of all widgets of the form. The list simplifies widgets lookup by name and navigation between them.
There is information about currently selected widget's name and type displayed on the first and second tab.

Additional toolbars are also available:

5.4.4. Using the "Widgets" tab

The "Widgets" tab in the Property pane provides a list of form widgets and their hierarchy. Each widget is presented within the hierarchy beside other widgets being on the same level (the same parent container). Child widgets (inside containers) are presented using indented names.

On the picture below, the form (a container) contains two widgets: "groupBox2" and "options" command button. In turn, "groupBox2" (being a container itself) contains two check box widgets.


Using the "Widgets" tab

Each widget has displayed its name and type. The type has also an icon displayed - the same as the one displayed on the toolbar used while form designing is performed.

Notes

5.4.5. Inserting widgets - text fields

Let's create a form providing information about persons, i.e. a form connected it with Persons table.

If the form being designed should present data obtained from the database, you need to place appropriate fields on it. To do this, use Widgets toolbar containing a set of togglable buttons. Each button corresponds with a single widget type.

  1. Click Text Box button on the Widgets toolbar.
  2. Click on the form surface with &LMB;. New text box widget will be placed in the point where you clicked. Before you release &LMB; you can drag your mouse to specify a desired size for the widget.
  3. If needed, move the inserted widget using drag & drop to a desired position. You can resize the widget afterwards by dragging one of the small boxes appearing near its corners. Note that the boxes are only visible when the widget is selected. If you select another widget or the form surface, the boxes disappear.
  4. Click Text Box toolbar button again and click on the form surface to insert another widget. Repeat this action once again until you get three text boxes inserted in your form. For sake of simplicity we will limit ourselves to three data fields.

Notes

5.4.6. Assigning data sources

The fields you inserted have no data source assigned yet, so these are not able to display information from the database. To assign data source, Data Source tab of the Property pane.

The very first step is to specify the form's data source, i.e. a place the displayed data will be fetched from. As mentioned above, you will use table persons as a data source for your new form.

  1. Click on the form's surface, as you will alter its properties.
  2. Switch to the Data Source tab and enter persons table name in the Form's data source drop down list. Alternatively, you can select this name from the drop down list.


    Entering form's data source name

You have assigned form's data source. Now you need to do specify field widget's data source.

  1. Click the first text field widget at the top of the form.
  2. In the Data Source tab of the property pane enter field name name in the data source drop down list. Alternatively, you can select this name from the drop down list.


    Entering field's data source "name"

  3. Click next text field widget and enter surname as the data source.
  4. Enter data sources for street, house_number and city text fields in a similar way.

You can now save the form's design (this is not mandatory to test the form in action). To save, click the Save object changes toolbar button or use the File -> Save menu command. Upon saving you will be asked for entering the form's name. Enter Persons as caption and click OK button. Form's name will be filled automatically.

It is right moment for testing your form. Click the Switch to data view toolbar button. Unless you made a mistake when while entering data sources, you should see form's fields filled with data from the persons table.


The Persons form in data view after inserting text fields and assigning data sources

Notes

5.4.7. Inserting text labels

To make it easier for the form's user to identify meaning of every field widget, these should have added text labels with appropriate titles. To create text labels Label widget is used.

Insert three text label widgets onto the form, placing them on the left hand of the text fields (or on the right hand if your operating system uses right-to-left layout). On inserting every new label, a text cursor appears inside where you can enter desired title. Enter consecutively: Name, Surname and Street. Additionally, on the top of the form insert another label displaying name of the form, i.e. "Persons". Enlarge this label's size and set larger font using Format -> Font menu command.



Ready to use form after adding text labels

5.4.8. Actions

Action is a single activity isolates in the application, available for user to execute. It can be also executed automatically as an reaction for a given event (e.g. after opening a form).

Assigning actions to form buttons

Most actions can be assigned to form button. Assigned action is executed after button is clicked.

To assign action:

  1. Switch to form's design view if you have not done yet.
  2. Select the existing button widget by clicking on it or put a new button widget onto the form. If you inserted a new button, enter its title and press Enter key.
  3. Click the button widget with the &RMB; to display context menu.
  4. From the context menu select Assign action... command.
  5. An Assigning Action to Command Button dialog window will appear presenting a list of available actions. One of the actions is selected if the widget already has action assigned. Otherwise the Action type drop down list has No type item selected.
  6. From the Action type drop down list select Application item. Available application-wide actions will be listed.
  7. Select one of the actions on the list (e.g. "Delete Row").
  8. Click OK button or press Enter key to accept your selection.


Assigning "Delete Row" action to a form's button

After switching to the form's data view you can try whether the action works. For example, if you assigned "Delete Row" action, clicking the button, the current database row will be deleted, similarly to executing Edit > Delete Row menu command (depending on your settings you may be asked to confirm the removal).

Notes

5.4.9. Widget layouts

In most cases form widgets should be reasonable placed and aligned. Positioning, aligning and resizing widgets by hand is not easy and these parameters are not adjusted when the user resizes the form. In fact the situation is even worse because you cannot assume a given form requires a given space because users have different font sizes and display resolutions.

The following example presents a form where text fields and labels were placed by hand. Some of them cannot fit in the form's window.

An example form with widgets that cannot not fit in the window

Using special tool called widget layouts can help to automatically lay out the form widgets. Widget layout is an action of grouping two or more widgets so these are well positioned and have appropriate sizes.

Using layout in this form improves alignment. Moreover, its space is better developed. Text fields are closer each other, spacing is constant.


Example form with layout used

There are two methods to create widget layout.

In each of these cases you can also use Format -> Layout Widgets menu.


Selecting widgets that will be put into a layout


Four widgets are selected




Using the context menu for putting the widgets into a grid layout

Widget layout is presented in the design view using a blue, green or red box drawn with broken line. This line is displayed only in the form's design view.




Widgets within a grid layout

Besides the grid type, there are other widget layout types.

Springs in widget layouts

A spring in widget layouts is a special, invisible element allowing to adjust widget's position and size within layouts. Such a spring stretches or squeezes a widget on the right, top, bottom or left hand, so it can have desired size and position.

To use a spring:

  1. Select spring icon on the Widgets toolbar.
  2. Click on a selected point of the form to insert the spring.

For the following example, the spring has been inserted on the left hand of the text label "Persons". The label is thus displayed on the right hand of the form. To make the spring work, it has been put into a common horizontal layout with the label.

Horizontal layout containing a spring and a text label

To make springs work you need to create a global widget layout i.e. a layout for the form itself. Then, springs can use edges of the form as a boundary for expanding.

Advanced widget layouts

Widget layouts can be combined (or nested). On the following example you can identify two nested layouts:

  1. horizontal layout with a spring, aligning the "Persons" text label to the right
  2. grid layout grouping widgets on the whole form


Two widget layouts combined: horizontal layout inside of a grid layout

The horizontal layout is treat in the example as a single widget by the grid layout - it takes exactly one "cell" of the grid. After opening a form designed this way in the data view, you can notice (by resizing the form) that:


The form using the two layouts displayed in data view

Removing widget layouts

To remove widget layout without removing widgets, perform one of these actions:

Notes

Removing widget layout using the Break Layout command will not remove widgets contained in the layout. If you want to remove the widgets as well, just select the layout by clicking on its border and press Delete key or use Edit -> Delete menu command or context menu command.

Size policies for widgets within a layout

Instead of setting a fixed size for your widgets, in &kexi; you can choose between various widget's size policies. A size policy is a flexible strategy for controlling how a widget is stretched (or shrunk) depending on other neighbouring widgets and space available within the form.

After putting widgets into a layout, typically each widget gets a proportional (Preferred) size policy. These widgets will be automatically resized with preferred settings, depending on their type and size of the entire layout itself. For example, three buttons put into the horizontal layout will be resized to fit their visible text.

For each widget inserted into the form, there are settings for size policy available in the Property Editor. The settings are presented as a group of properties called Size Policy.

A group of properties for defining a widget's size policy

This group of properties contains:

Values of size policies

There are following values available on the drop down list for Horizontal Size Policy and Vertical Size Policy properties visible in the Property Editor:

Different widget types have various default size policies; for example, button widgets have default size policy set to Minimum (in both directions), while text field widgets have vertical size policy set to Fixed.

The most frequently used size policies are Preferred, Minimum and Maximum.

Vertical and horizontal stretch

Vertical Stretch and Horizontal Stretch properties accept integer values greater or equal to 0. These properties allow to fine-tune the behavior of size policies. Default value for the properties is 0. Greater value of the stretch means that the widget will be expanded more than other widgets having smaller stretch value set. For example, the following image presents two buttons where the first button has Vertical Stretch set to 0 and the second button has Vertical Stretch set to 1.

Size of button widgets affected by setting Vertical Stretch property of the second button to 1

5.4.10. Setting widgets size and position by hand

In case when your form has no main layout set for auto-positioning and auto-resizing its widgets, you will probably want to align widget's position and size so the form can look cleaner and be easier to use. The &kexi; form designer simplifies this task by offering the following groups of commands:

There are also additional commands available: Bring Widget to Front (i.e. above all other widgets) and Send Widget to Back (i.e. below all other widgets). These two commands are rarely used, as it is not common to place one widget on top of other (with an exception when a container widget contains other widget inside). Also note that clicking a widget with mouse button is enough to bring the widget to front.

5.4.11. Setting the tab order

Widget's focus determines widget's activity available using keyboard. Focus is related to widgets displayed in form's data view. Exactly one form widget can have focus at the same time. Most frequent use of focus is text entry (when a given text field is active, i.e. it is focused). Other example is a button widget - when focused, it is possible to "press" it using the Enter or Space key instead of a mouse button.

There are a few methods of making the widgets active (moving the focus to the widget): clicking with a mouse button, rotating the mouse wheel over the widget, or using the Tab key. The latter method is often used because of it's speed and convenience for users. Availability of the focusing methods is controlled by Focus Policy property of a given widget.

There is relationship between focusing (activating) widgets using Tab key and tab order setting of a form. After pressing the Tab key, the next widget should be focused, so the form should know about the tab order.

To alter table order for a form's widget:

  1. switch to design view of the form
  2. execute Edit -> Edit Tab Order menu command. Edit Tab Order will appear with settings for this form.

    A window for editing tab order for a form

    The window contains a list with two columns: the first column displays widget names, the second - types of the widgets. To make it easier to recognize meaning of the names and types for the user, icons related to the types are also displayed. The list contains only widgets having focus policy allowing to use the Tab key. The window allows you to change tab order or set the automatic tab order.

  3. To change tab order, either:

  4. Click the OK button to accept the changes or Cancel button to dismiss the changes.