Step-by-Step Tutorial: Working with Contacts
In this step-by-step exercise, you'll create an application that demonstrates working with contacts within Pocket Outlook. As part of this exercise, you'll
This application provides all of the fundamentals of working with contacts within Pocket Outlook.
NOTE To complete this tutorial you'll need the PocketOutlookcomponent from InTheHand at http://www.inthehand.com.
NOTE I provide a completed version of this application, titled Contacts - Complete, under the Chapter 18 folder of the Samples folder for this book. See Appendix D for more information on accessing and loading the sample applications.
Step 1: Opening the Project
To simplify this tutorial, I've already created the project and the user interface for the Contacts Tutorial application. This template project is included under the Chapter 18 folder in the Samples folder. To load this project, follow these steps:
Step 2: Examining the User InterfaceThe user interface for the Contacts Tutorial application is comprised of several controls: a ListBox, four Buttons, and a ComboBox. Figure 18-3 shows the application's interface. Figure 18 - 3. The contacts application interfaceThe ListBox will display available contacts.
The four buttons for the Contacts Tutorial application function as follows:
The ComboBox provides a list from which the user may select an alphabetical subset of the contacts. The ComboBox has been preloaded with all of the letters of the alphabet.
Step 3: Adding a Reference to PocketOutlook
You need to add a single reference to your application to enable you to work with the Pocket Outlook Object Model from a .NET Compact Framework application.
Step 4: Adding the Imports Statement
The first line of code you'll be adding imports the PocketOutlook namespace.This allows you to reference and work with the PocketOutlook elements within your code without having to fully qualify each element.To import the PocketOutlook namespace, perform the following steps:
Imports InTheHand.PocketOutlook
Step 5: Declaring Module-Level Objects
This tutorial uses two module-level object variables. These variables hold instances of the PocketOutlook OutlookApplication and OutlookItemCollection objects.To define these variables, add the following code to the module level of the form:
Dim poApplication As New OutlookApplicationDim myContacts As OutlookItemCollection
Step 6: Loading a List of All Contacts
The first functionality that you'll add to the application is to display a list of all contacts. Obtaining this list is simple. Loading the list into your ListBox requires nothing more than a For loop for running through the collection.
The first part of this step obtains a list of contacts. While you could simply reference the Contacts collection of the OutlookApplication object, you are instead going to retrieve a copy of the Contacts collection into a collection of its own. Having a collection of contacts that matches those contacts displayed in your ListBox makes it easier to display the details of an individual contact. You'll see more on this later in the tutorial.
The code required to retrieve the Contacts collection is shown in Listing 18-16. Add this code to the Click event procedure of the Load button. In this procedure, you create a copy of the Contacts collection through the OutlookApplication.
Contacts.Items collection.
Listing 18-16. Retrieving a List of All Contacts
Private Sub btnLoad_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnLoad.Click
' Store the collection of contacts for future use. myContacts = poApplication.Contacts.Items
' Load the list of contacts. LoadContacts()
End Sub
Displaying the list of contacts is triggered at the bottom of the procedureshown in Listing 18-16. The LoadContacts procedure is a general-purpose procedure that displays the contents of the myContacts collection in a ListBox.
To define the LoadContacts procedure, add the code shown in Listing 18-17 to your form module. The heart of this procedure is the For loop located near its bottom.This loop iterates through all of the contacts stored within the myContacts collection, adding the FileAs property of each contact to the ListBox. Remember,myContacts is a collection of contacts. Each item in this collection is a Contact object, with all of its properties and methods.
Listing 18-17. The LoadContacts Procedure
Sub LoadContacts() Dim intCount As Integer Dim myContact As Contact
' First, make sure that the list box is empty. lstContacts.Items.Clear()
' Next, load the contacts into the list box. For intCount = 0 To myContacts.Count - 1
myContact = myContacts.Item(intCount) lstContacts.Items.Add(myContact.FileAs) Next
Step 7: Displaying a Contact
The process of displaying a contact is easy because of the functionality provided through the Pocket Outlook Object Model. Calling the Display method of the Contact object results in the contact being displayed using the default contact interface.
To display a contact, add the code shown in Listing 18-18 to the Click event procedure of the Display button.
Listing 18-18. Displaying a Contact
Private Sub btnDisplay_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim myContact As Contact
' Display the selected contact.
If (lstContacts.SelectedIndex <> -1) Then myContact = myContacts.Item(lstContacts.SelectedIndex myContact.Display()End If
Earlier in this tutorial, I mentioned using a collection to hold a list of Contact objects. This is where that approach pays off. Since your collection myContacts matches the contacts displayed in the ListBox in a one-to-one relationship, it's easy to display a single contact. All that you need to do is to create an instance of the contact and then call the Display method of that contact.
Step 8: Adding a Contact
Adding a contact is a three-step process. First, you need to create the contact. Second, you configure the properties of the contact. Third, you save the contact.In this tutorial, the contact that is added is predefined, which is to say that the user has no input in the matter. Insert the code shown in Listing 18-19 into the Click event of the Add button.
Listing 18-19. Adding a Contact
Private Sub btnAdd_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnAdd.Click
' Create a new contact. myContact = poApplication.CreateContact
' Configure the contact. With myContact .Birthday = CDate("01/01/1960") .Body = "This is a sample contact." .BusinessTelephoneNumber = "888-555-0001" .Categories = "demo" .CompanyName = "Acme" .Email1Address = "[email protected]" .FileAs = "Acme, Joe" .FirstName = "Joe" .LastName = "Acme" .Title = "President"
' Finally, save the contact. .Save() End With
' Let the user know that the contact was added. MessageBox.Show("contact added...")
Step 9: Loading a List of Select Contacts
The last feature that you're going to add to this application is the ability to select a subset of the contacts list. In this case, you're going to select all of the contacts that begin with a specific letter-for example, only those contacts that begin with the letter A.Add the code shown in Listing 18-20 to the Click event of the Select button. At the heart of this procedure are two steps-the building of the selection string and then the use of this string with the Restrict method. The result is the creation of a collection of contacts that match the desired criteria.
Listing 18-20. Selecting a Subset of the Contacts
Private Sub btnSelect_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSelect.Click
Dim strQuery As String
' Retrieve the selected contacts. strQuery = "[FileAs] >= " & ControlChars.Quote & cmbPrefix.Text & _ ControlChars.Quote
' Use a range for anything other than contacts beginning with the letter "Z". If (cmbPrefix.Text < "Z") Then strQuery = strQuery & " AND [FileAs] < " & ControlChars.Quote & _ cmbPrefix.Items(cmbPrefix.SelectedIndex + 1) & ControlChars.Quote End If
myContacts = poApplication.Contacts.Items.Restrict(strQuery)
Step 10: Testing the Application
Finally, you're ready to test your application. To begin, you need to copy the application to your target device by performing the following steps
Your application copies to the target device along with the PocketOutlook component. Upon completion the application starts.
To verify the functionality of your application, perform the following steps:
NOTE The availability of contacts that begin with a specific letter is dependent upon the contacts resident on your test device.
HOMEWORK Add a button to this application that will modify an existing contact. The contact to modify is the one selected within the ListBox. Next, we'll turn our attention to accessing, creating, and modifying appointments through the Pocket Outlook Object Model.