Adding and arranging controls
The real power of forms comes from the controls that you place on them. To construct a robust Outlook 2013 forms solution, you need to plan carefully what the form is supposed to do; what pieces of information it will display, modify, save, or send; which controls will display these information units; and how the controls will be laid out. You can put two types of controls on a form: a control that is bound to an Outlook 2013 field and a control that is not. This section looks first at field-bound controls. Field-bound controls are bound to specific control types, such as drop-down lists, text boxes, command buttons, labels, or check boxes.
To display a field on your form, follow these steps:
Display the Field Chooser, and then select a field category in the drop-down list.
In the scrollable list in the Field Chooser, select the field that you want, and then drag it onto the form.
Format the control as needed.
You can resize, move, or rename a control, and you can change a number of its properties. To resize the control, select the control by clicking it, and position the mouse pointer over one of the control handles, which are represented by small boxes. When a small arrow appears, you can drag the handles in the appropriate direction to resize the control.
To move a control to a new location, simply drag it. Notice that the form’s canvas is covered with a grid. Each point on the grid is a possible location for a corner or other relevant point on a control. You can choose to have controls snap to the grid points by right-clicking the grid and selecting Snap To Grid. You can define the distances between the points on this grid. This is important because the greater the scale of the grid (the greater the distance between points on the grid), the fewer places you can locate a control on your form. Conversely, the smaller the scale, the more you can refine the positioning of your controls.
To change the grid, follow these steps:
In the Arrange group, click Align.
Click Set Grid Size.
Type a value (in pixels) for the height and width spacing.
The smaller the number that you use for spacing, the smaller the scale. This means that more points on the grid will appear, and you can have more control over where your objects fit on the grid. The default is 8, but 3 is a good number to choose for greater positioning control.
Controls have a number of properties that you can view and modify. To find out what these properties are, right-click a control and then choose Properties on the shortcut menu to display the Properties dialog box. Figure 27-9 shows a Properties dialog box for a text box control.
Figure 27-9 You can use the Properties dialog box to modify the properties of a control.
The Display tab of a control’s Properties dialog box (a text box example is shown in Figure 27-9) lists the most commonly used properties of the particular control. Changing the setting of a property in this dialog box enables the Apply button; clicking Apply or OK sets the value of that property for the selected control.
The default names of controls are rather generic, such as TextBox1 or CheckBox1. You’ll want to change these to names that are more descriptive for your solution, such as txtFirstName or chkHasVacation.
You can learn more about naming conventions for controls by visiting the Microsoft MSDN website at http://msdn.microsoft.com and searching for “Visual Basic Coding Conventions Overview.”
The Layout tab in the Properties dialog box lets you set the position of the field within the form. The position settings are set in pixels offset from the top and left side of the form. You can specify the height and width of the field as displayed in the form by setting the Height and Width values. You can also configure the field to resize itself automatically as the form size is being changed by selecting the Resize With Form check box in the Automatic Layout area, as shown in Figure 27-10.
Figure 27-10 Use the Layout tab to set the position and size of a control.
The Value tab in the Properties dialog box, shown in Figure 27-11, contains a number of settings that relate to the field value that the control represents. As mentioned, each control in the Field Chooser list is bound to an Outlook 2013 field. When you modify the properties of a control, you can change the field to which the control is bound.
Figure 27-11 Use the options on the Value tab to set the field and format for a control.
To change the bound-field property, click Choose Field and then select the field to which you want to bind the control in the drop-down list. Make sure that the field value is bound to the correct property of your control. Normally, the field value is tied to the control’s Value property; this is rarely changed. However, you can change this setting so that, for example, the value of a field is tied to your control’s Enabled property. In this case, if the value of the field is True, the control is enabled; if the value is False, the control is not enabled.
You can also set the initial value of your control to display a default value. Set the Initial Value Of This Field To check box, and then type an initial value in the text box. This value doesn’t have to be predetermined—you can have it correspond to a dynamic value, such as the current day or the concatenation of the Subject field and the current date. To make the initial value more dynamic, click Edit to open the Initial Value For dialog box; an example is shown in Figure 27-12.
Figure 27-12 Use this dialog box to customize the initial value for a control.
In this dialog box, you establish a formula for the initial value of your control. For example, you can simply insert a built-in function, such as Date( ), for the formula.
To insert a built-in function—the Date( ) function, in this example—follow these steps:
Click Date/Time, and then click Date( ).
The function appears in the Formula text box.
Click OK, and then click OK again to close the Properties dialog box.
When you run the form, the text box control will contain the current date as its initial value. Your users can always change the control’s initial value unless you set the control to Read Only (on the Display tab).
The Validation tab in the Properties dialog box allows you to set certain properties that relate to how (or whether) the value of the control is validated. For example, if you create a form for a purchase order, you might want to ensure that users indicate the quantity of parts that they want to order. The order processing staff will send you many thanks for requiring certain values before the purchase order gets to them because it reduces the amount of information traffic and busywork needed to process an order.
Suppose that you’ve added a control to your form that requires a value for a text box, and that value is required to be less than or equal to 10 characters. If the user fails to enter a valid value, Outlook 2013 will display a message that prompts the user to enter a correct value.
To set the properties on the Validation tab that will be necessary for this example, as shown in Figure 27-13, follow these steps:
Display the Properties dialog box, and then click the Validation tab.
Select the A Value Is Required For This Field check box.
Select the Validate This Field Before Closing The Form check box.
Click the Edit button located to the right of the Validation Formula text box.
Click Text, Len( string ), and then click OK. The Len( string ) function appears in the Validation Formula text box.
In the Validation Formula text box, type <=10 after the function, and then click OK.
In the Display This Message If The Validation Fails text box, type the following text (including the quotation marks):
“Please enter a value between 1 and 10 characters in length.”
Alternatively, you can click Edit, type the message without quotation marks, and then click OK.
Click OK to close the Properties dialog box.
Figure 27-13 Use the Validation tab to require and verify the value entered in a control.
In the example exercise, when a user works with your form, the text box that requires validation must contain a value, and the value must be less than or equal to 10 characters. If the value the user enters is 11 characters or more, Outlook 2013 will display a message box containing the validation text that you provided when the user tries to send the form. The user can then make the appropriate changes to the text box value and attempt to resend the form.
This chapter has thus far concentrated on controls that are bound to Outlook 2013 fields and that appear in the Field Chooser. However, these aren’t the only controls that you can add to a form. This section takes a brief look at some of the standard controls that are available in Outlook 2013, as well as controls that come as part of the Office system.
Controls appear in a Control Toolbox, which is a small, resizable window made visible when you click the button next to the Field Chooser button on the form. Figure 27-14 shows the Toolbox.
Figure 27-14 The Control Toolbox allows you to add controls to your form.
As you hold the mouse pointer over the control icons in the Toolbox, the name of each control appears. To add one of these controls to your form, drag the control icon onto the form. You can then resize and reposition the control or set its properties, as discussed earlier.
These standard controls are useful but limited. As your skills in developing Outlook 2013–based solutions progress, you’ll find that you need functionality that transcends the abilities of the standard controls provided in the Toolbox. Fortunately, you can add other controls and make them accessible via the Toolbox window. For example, if you design a number of forms that need the Outlook Date Control to enable the user to pick a date, you can add that control to the Control Toolbox.
Follow these steps to add the Outlook Date control to the Toolbox:
Right-click an empty area of the Toolbox window.
Choose Custom Controls.
Scroll down the Available Controls list, and then click the box next to Microsoft Outlook Date Control.
Click OK. The control appears in the Toolbox.
You can now add this control to a form and work with its specific properties and behaviors just as you did for the standard controls.
Custom controls can make your Outlook 2013 solution extremely robust and powerful. However, be aware that the control you’re using might not exist on the computer of the person receiving the message. In other words, although you might have a particular control on your computer, the person who uses your form to compose a message or receives a message composed on your form might not have that control installed. For your solution to work, you need to ensure that the custom controls you use are distributed to and installed on other users’ computers properly.
After creating your form, you can test it to see what it looks like when it is run. With the new form open, choose Run This Form in the Form group. This won’t cause the form to close or disappear; instead, Outlook 2013 produces a new form based on the form that you’ve just created. The newly created form is an actual running form that you can send and read, and any included functions or scripts are also run when the form is opened.