Programming Microsoft Outlook and Microsoft Exchange, Second Edition (DV-MPS Programming)

[Previous] [Next]

Under the Pages collection, which you just learned about, is the Controls collection, which contains all the controls on an Outlook form. You can use this collection to dynamically add, remove, or change the properties for individual controls in your Outlook application. The following sections looks at the Controls collection and the individual Control objects. Note that because the Controls collection and objects are from the Microsoft Forms 2.0 library, to learn about methods and properties that are not described in this supplement, you will need to use an Object Browser or view the Fm20.hlp file on the companion CD.

Controls Collection Properties

The Controls collection implements only one property, Count. This property allows you to create a For or While loop that implements changes on or displays properties for all the controls in your form. One issue to note is that when looping through the controls in the Controls collection, the index of the first control is 0, the second is 1, and so on. The following code sample displays the name of each control on your form by using the Count property to loop through the controls:

Sub CommandButton1_Click set oPages = Item.GetInspector.ModifiedFormPages set oControls = oPages("P.2").Controls for i = 0 to oControls.Count - 1 msgbox "Control #" & i +1 & " is named " & _ oControls.Item(i).Name next End Sub

Controls Collection Methods

The Controls collection implements methods that affect the controls on your form. For this reason, you have to be careful when adding code that uses these methods since you might accidentally delete items from the form that other code in your application is expecting will be on the form. The following section describes some of the methods for the Controls collection.

Add Method

The Add method allows you to programmatically add a control to your form. The required parameter for this method is the ProgID of the control that you want to create. Optionally, you can specify the name and whether the newly created control is visible. The following example shows you how to add a control from the Microsoft Forms 2.0 library as well as create an ActiveX control on your form:

Sub CommandButton1_Click set oPages = Item.GetInspector.ModifiedFormPages set oControls = oPages("P.2").Controls set oNewControl = oControls.Add("Forms.TextBox.1") oNewControl.Text = "My New TextBox Control" oNewControl.AutoSize = True 'This inserts in the Access Calendar Control set oNewActiveX = oControls.Add("MSCal.Calendar") oNewActiveX.Top = 100 oNewActiveX.Left = 10 End Sub

Clear Method

This method clears all the controls off the selected form. You can use this method to programmatically change the interface of your application for your users. The following code shows you how to use the Clear method:

Sub CommandButton1_Click set oPages = Item.GetInspector.ModifiedFormPages set oControls = oPages("P.2").Controls oControls.Clear End Sub

Move Method

The Move method moves all the controls on a form a certain distance from their current horizontal and vertical coordinates. This method takes parameters X and Y, which correspond to a distance in points from the control's current position.

Sub CommandButton1_Click set oPages = Item.GetInspector.ModifiedFormPages set oControls = oPages("P.2").Controls oControls.Move 20,20 End Sub

Working with Individual Controls: The Control Object

The children objects of the Controls collection are Control objects. The individual Control objects correspond to the type of control. So when developing your application, you have to be aware of what type of Control object you are dealing with before you call methods or set properties on it—some of these control objects might not support properties and methods that other objects do. When calling individual Control objects from the Controls collection, you can use either the name of the control or the index number of the control. Most developers find it easier to call their Control objects by using the name. The following section describes some of the common properties and methods for most Control objects.

Control Object Properties and Methods

The most common properties that most Control objects implement are Left, Top, Visible, Enabled, and Locked. By using these properties, you can change the appearance, location, and accessibility of controls on your forms. You can also use them to change the available choices in a list box programmatically. Of course, you can use the advanced properties of any of these controls. The first code sample shows using some common and advanced properties of the built-in Label control as well as the custom properties of an ActiveX control. The second code sample is taken from the Account Tracking application and shows you how to dynamically change the values in a list box according to the current value in another list box. In this example, when the user selects a region of the world, the control automatically populates with the district offices that are available in that region:

Sub CommandButton1_Click set oPages = Item.GetInspector.ModifiedFormPages set oControls = oPages("P.2").Controls set oNewControl = oControls.Add("Forms.Label.1") oNewControl.Caption = "This is a new Label" oNewControl.Left = 10 oNewControl.Top = 10 oNewControl.AutoSize = True 'Use the MousePointer property oNewControl.MousePointer = 14 'Arrow with Question Mark oNewControl.ControlTipText = "This label should show a question mark" set oNewActiveX = oControls.Add("MSCal.Calendar") oNewActiveX.Top = 100 oNewActiveX.Left = 10 oNewActiveX.GridFontColor = RGB(255,0,0) 'Red End Sub 'Taken from the Account Tracking application set oDistrict = oDefaultPage.Controls("lstDistrict") oDistrict.visible = True oDistrict.clear select case item.userproperties.find("txtAccountRegion").value case "East" oDistrict.additem "Florida" oDistrict.additem "New Jersey" oDistrict.additem "New York" oDistrict.additem "Washington DC" case "Central" oDistrict.additem "Chicago" oDistrict.additem "Detriot" oDistrict.additem "Minneapolis" case "West" oDistrict.additem "California" oDistrict.additem "Colorado" oDistrict.additem "Washington" case "International" oDistrict.additem "England" oDistrict.additem "France" oDistrict.additem "Italy" oDistrict.additem "Japan" oDistrict.additem "Other" end select

Категории