Controls Collection Object Properties

The Controls collection object provides only the Count property. With the Count property, you can use the For...Next statement to loop through the controls in a collection. The following example returns the collection of controls on the Controls collection object page. It then displays the name of each control contained on the page.

Sub ShowtheControlsCollectionCount_Click Set Page = ltem.Getlnspector.ModifiedFormPages("Control Object") Set MyControls = Page.Controls For i = 0 to MyControls.Count -1

MsgBox MyControls(i).Name, vblnformation Next End Sub

Notice that the For...Next statement starts at 0. You must use 0 to reference the first item in the collection. However, Outlook returns a Count of 3. Therefore, you must specify Count -1 as the upper limit in the argument of the For...Next statement.

The Control Object

In the Visual Basic Forms Object Model, the control objects are generically represented by a Control object. Further, they are individually represented by the objects that represent the names of the individual control components, such as TextBox, CheckBox, and so on. As discussed in the preceding section, to reference a control, use the Controls property to return the Controls collection from a Page object, and then use Controls (index), where index is the name or index number, to return a control from a Controls collection.

The Control object and the individual control objects, such as CheckBox and TextBox, are contained in the Microsoft Forms 2.0 Object Library. To view the methods and properties for this library, use the VBA Object Browser and select MSForms in the library drop-down list box.

Unlike controls on VBA or Visual Basic forms, the control object in VBScript must be explicitly instantiated before you can access the properties and methods of the control. You must explicitly reference the control object regardless of whether the control is an intrinsic Outlook Forms control or an extrinsic custom or third-party ActiveX control. The following line of code shows you how to set a reference to a control object:

Set TextBoxl = ltem.Getlnspector.ModifiedFormPages("P.2").Controls("TextBox1") A shorter alternative syntax is as follows:

Set TextBoxl = Getlnspector.ModifiedFormPages("P.2").TextBoxl

Setting Control References at the Script Level

If you inspect the code behind the Control object example form, you'll see that some control objects have been dimensioned as script-level variables. The ltem_Open procedure then sets a reference to each control dimensioned as a script-level variable, and the control object has a script-level lifetime. If you follow this convention, you do not have to instantiate a control object reference in every procedure where you want to get or set control properties and call control methods. Dimension the controls as script-level variables using the same name as the control, and then instantiate those control variables in the form's ltem_Open procedure. Don't use an alias for the control object. Name the control object on the left side of the Set statement with the same name as the actual control. This practice makes your code more readable because the name of the control in VBScript code is the same as the control name in Design mode.

Properties of Control Objects

The properties available for a control will differ for each type of control. This section covers the Value property for the CheckBox control and the Enabled property for the TextBox and Label controls.

Setting the Enabled Property of a Control

The following example uses a CheckBox control called EnableTextBox and a TextBox control called TextBoxl. When the EnableTextBox control is clicked, the procedure evaluates the state of EnableTextBox. If the value of EnableTextBox is True, then TextBoxl and LabeM are enabled; otherwise, they are not enabled.

Sub EnableTextBox_Click

Set MyPage = Item.Getlnspector.ModifiedFormPagesfControl Object") Set MyControls = MyPage.Controls If MyControlsfEnableTextBox").Value = True then MyControls("Label1").Enabled = True MyControls("TextBox1"). Enabled = True

This document was created by an unregistered ChmMagic, please go to to register it. Thanks Else

MyControls("TextBox1"). Enabled = False MyControlsfLabeH"). Enabled = False End If End Sub

Setting the PossibleValues Property of a ComboBox or ListBox Control

Rather than use the Addltem method to add items to a ListBox or ComboBox control, you can use the PossibleValues property. Note that the PossibleValues property is an undocumented property. In this example, the PossibleValues property is used to add the values Red, Green, and Blue to the ComboBoxl control when CommandButtonl is clicked.

Sub CommandButton1_Click

Set MyPage = Item.Getlnspector.ModifiedFormPages("Message") MyPage.ComboBoxlPossibleValues = "Red;Green;Blue" End Sub

Using the List Property to Populate a ListBox or ComboBox Control

The most powerful method to add values to a ListBox or ComboBox control is the List property. The List property populates a ListBox control or ComboBox control with a single statement. By assigning the control object's List property to a variant array, you can create a single-column or a multi-column list in the control. If you are populating controls with the results of an ADO recordset, the List property and the recordset GetRows method (which returns a variant array) are perfect partners. The following example from the ltem_Open event in the Control object form creates a variant array and then assigns the array to the List property of the ListBoxl and ComboBoxl controls:

Sub ltem_Open

'Instance script-level control objects

Set ListBoxl = Getlnspector.ModifiedFormPages _

("Control Click Events").ListBoxl Set ComboBoxl = Getlnspector.ModifiedFormPages_

("Control Click Events").ComboBoxl 'Create a variant array to hold values Redim varArray(2) varArray(O) = "Red" varArray(1) = "Blue" varArray(2) = "Green" 'Populate ListBoxl and ComboBoxl ListBoxl List = varArray ComboBoxl List = varArray End Sub

Hiding Columns in a ComboBox Control

You can hide a column in a ComboBox control by using the ColumnWidths property. The following example presents a typical problem for the designer of a mutli-column combo box. You want to present recognizable names, such as the full name of a state, but you only want to store the state's abbreviation. The PopulateStates procedure in the Control object form populates the cmbStates combo box with the abbreviation and full name for several vacation states. The ColumnWidths property is set so that the state abbreviation does not appear in the drop-down portion of the list. However, the TextColumn property is set so that the abbreviation appears in the ComboBox control when the user selects an item from the list. If you are binding the control to a user-defined field, you should also set the BoundColumn property so that the bound field uses the specified column as its value source. The cmbStates_Click procedure utilizes the Text, Listlndex, and Column properties of the combo box to create a message box that informs the user about their selection.

Sub PopulateStates Dim varStatesQ Set cmbStates = _

Getlnspector.ModifiedFormPagesfControl Object").cmbStates

ReDim varStates(6,1)


0,0) =



0,1) =



1,0) =



1,1) =



2,0) =



2,1) =



3,0) =



3,1) =



4,0) =



4,1) =



5,0) =



5,1) =



6,0) =



6,1) =


cmbStates.List =


cmbStates.ColumnCount = 2

cmbStates.ColumnWidths = "0

cmbStates.TextColumn = 1

cmbStates. BoundColumn = 1

End Sub

Sub cmbStates_Click

MsgBox "You selected the state of" & cmbStates.Text _ & vbCr & "Enjoy your vacation in " _

& cmbStates.Column(1, cmbStates.Listlndex), vblnformation End Sub

When you set TextColumn or BoundColumn in a ListBox or ComboBox control, remember that the Column property is zero-based so that Column 1 is referenced as Column(O).

Resizing a Control Vertically and Horizontally

Quite often, you want controls on a form to adjust horizontally and vertically when the form is resized. To do this, you must set an invisible property called LayoutFlags. This example sets the LayoutFlags property for the TextBoxl control when you click CommandButtonl.

To run the example, do the following:

1. In Design mode, add a TextBoxl control to the (P.2) page of a form.

2. Resize the control so that its bottom border is near the bottom border of the form and its right border is near the right border of the form. Don't obscure the CommandButtonl control.

3. Add the following code for the CommandButtonl control.

4. Switch to Run mode, resize the form, and notice what happens.

5. Then click CommandButtonl and resize the form. After the LayoutFlags property is set, the controls adjust to the size of the form.

Sub CommandButton1_Click

Item. Getlnspector.ModifiedFormPagesfP.2").TextBoxl LayoutFlags = 68 End Sub

After you set the LayoutFlags property, you can delete the procedure from the Script Editor. Resizing a Control Vertically

In some situations, you want controls on the form to vertically resize when the form is vertically resized. To do this, set the LayoutFlags property to 65.

To run this example, do the following:

1. Delete the TextBoxl control from the (P.2) page, and then add a new TextBoxl control.

2. Resize the control so that its bottom border is near the bottom border of the form. Don't obscure the CommandButtonl control.

3. Add the following code for the CommandButtonl control.

4. Switch to Run mode, and resize the form vertically. Then click CommandButtonl, and vertically resize the form again.

Sub CommandButton1_Click

Item.Getlnspector. ModifiedFormPages _ ("P.2").TextBoxl LayoutFlags = 65 End Sub

After you set the LayoutFlags property, you can delete the procedure from the Script Editor. Binding a Control to a Field at Run Time

You can use the hidden ItemProperty property to bind a control to a field at run time. To run this example, do the following:

1. Add a new TextBoxl control (or use the existing one if it's already on the form) to the (P.2) page of the form.

2. Add the following code for the CommandButtonl control.

3. Switch to Run mode, click the Message tab, and type Test in the Subject box.

4. Then click CommandButtonl. Notice that the value in the Subject box appears in the TextBoxl control because the control is now bound to the Subject field.

Sub CommandButton1_Click()

Item.Getlnspector. ModifiedFormPages _ ("P.2").TextBoxl ItemProperty = "Subject" End Sub

If you create a control by dragging a plain text field to a form, you cannot bind the control to a field of a different type. For example, you cannot drag a TextBox control to a form and then bind it programmatically to a field containing an E-Mail recipient type (such as the To field).

Binding a Control to a Data Source

You can bind controls on an Outlook form to a data source with ADO. However, you should be cautious when incorporating non-Exchange sources of data into your solution. If your application requires online and offline use, dynamic connections to data with ADO are not practical in the offline state. You can use ADO persisted recordsets to store data in a folder and then extract that persisted recordset to the file system when you need to access the data. This practice is limited to small sets of data that do not change frequently. Figure 11-10 illustrates a form that populates unbound controls with data from a persisted recordset stored in an Outlook folder. For additional examples of bound data controls and other methods of retrieving data in Outlook, see the Data Access Strategies For Outlook folder under "5. Advanced Topics" in the "Building Microsoft Outlook 2002 Applications" personal folders (.pst) file.

Persistent ADO Recordset - Discussion

Figure 11.10 - Bind controls to data with a persisted recordset stored in an Outlook folder Responding to the Click Event

The Click event is the only event raised by controls on Outlook forms. You must define the Click event procedure for a control in the Script Editor as follows:

Sub ControlName_Click 'Event code goes here End Sub

The Click event fires without exception for the Label, Image, CommandButton, ToggleButton, and Frame controls. Open the Control object item in the VBScript Samples folder and click the Control Click Events page for a practical demonstration of control Click events. However, there are many exceptions to the Click event:

• Right-clicking a control does not fire a Click event.

• Not every third-party or custom ActiveX control will fire a Click event in the Outlook forms container.

• The following controls do not respond to Click events in Outlook: TabStrip, SpinButton, ScrollBar, TextBox, and MultiPage.

• The following controls only respond to Click events when they are populated with items and an item in the list is selected: ComboBox and ListBox.

• The following controls respond to a Click event based on a change in their Value property: OptionButton and CheckBox.

An alternative to using a Click event is to write a CustomPropertyChange or PropertyChange event procedure for the bound field of the control. For example, the following procedure mimics the Buddy control available in Visual Basic by binding a SpinButton control to a user-defined field. When the user clicks the spin button, the value of TextBox2 changes. You can see this example in action on the page named "Using Control Values" in the Control Object item in the VBScript Samples folder.

Sub ltem_CustomPropertyChange(ByVal Name) Select Case Name Case "SpinButton2"

TextBox2. Value = SpinButton2. Value End Select End Sub

0 0

Post a comment