When the contact list is loaded with your contacts, the contact status and availability must be kept current. For this to happen, your application must register an event handler for the DMessengerEvents::OnContactStatusChange event.

Registering the Callback Function

The following syntax is completed by the Intellisense feature of the .NET environment as long as you have added CommunicatorAPI to the references for your project.

Copy Code
 [C#]
communicator.OnContactStatusChange += new DMessengerEvents_OnContactStatusChangeEventHandler(communicator_OnContactStatusChange);

Handling the Event

The following example illustrates the handling of the contact status changed event.

The event handler receives two arguments. The first is an object which represents the contact whose status has changed and the second argument is the enum, MISTATUS. This enum provides the current status of the contact.

The following code snippet is from OC_API_Tester.cs. The snippet provides an example of a good way to handle the contact status changed event.

The event handler performs two tasks. First, details of the event are written to a status box on the main application form. Second, the handler calls updateContactStatus(object,MISTATUS), a method on the contactList class. This method accepts the contact object and status as arguments.

Copy Code
		void communicator_OnContactStatusChange(object pMContact, MISTATUS mStatus)
		{
			IMessengerContactAdvanced imContact = (IMessengerContactAdvanced)pMContact;
			StringBuilder sb = new StringBuilder();
			sb.Append("CONTACT STATUS CHANGED: Current STATUS-" + mStatus.ToString());
			sb.Append("Contact:" + imContact.FriendlyName);
			object[] contactPresenceProperties = (object[])imContact.PresenceProperties;
			sb.Append("Availability: " + contactPresenceProperties[(int)PRESENCE_PROPERTY.PRESENCE_PROP_AVAILABILITY].ToString());
			writeToTextBox("EVENT: " + sb.ToString());
			try
			{
				myContactList.updateContactStatus(imContact, mStatus);
		}
			catch (Exception e)
			{
				writeToTextBox("Error updating contact status in contact list:" + e.Message.ToString());
		}
	}

The updateContactStatus method locates the contact in the list by the IMessengerContact::FriendlyName. When the contact is located in the list, the status image is updated along with the availability text.

See Also