Share via


Removing a Contact from a Group

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

A contact can also be moved to the "Other Contacts" group from a user-defined group. Moving a contact to the "Other Contacts" group involves removing the contact from the user-defined group with the IMessengerGroup::RemoveContact method.

The contact list is rebuilt by the event handler registered for the DMessengerEvents::OnContactRemovedFromGroup event.

private void removeFromGroupItem_Click(object sender, EventArgs e)
{
  try
  {
    ListViewItem selectedContactItem = contactListView.FocusedItem;
    if (selectedContactItem.Text.Length > 0)
    {
      object contactToRemove = null;
      contactToRemove = communicator.GetContact(listDict[selectedContactItem.Index + 1],
                       communicator.MyServiceId);
      if (contactToRemove != null)
      {
        IMessengerGroups theseGroups = (IMessengerGroups)communicator.MyGroups;
        foreach (IMessengerGroup thisGroup in theseGroups)
        {
          if (thisGroup.Name == selectedContactItem.Group.Header)
          {
            thisGroup.RemoveContact(contactToRemove);
            break;
          }
        }
      }
      selectedContactItem.BackColor = Color.Gray;
    }
  }
  catch (ArgumentException AE)
  {
    MessageBox.Show("Contact List Argument Exception: " + 
                    AE.Message.ToString());
  }
  catch (COMException CE)
  {
    MessageBox.Show("Contact List COM Exception: " + 
                    formReturnErrors.returnComError(CE.ErrorCode));
  }
  catch (Exception Ex)
  {
    MessageBox.Show("Contact List Exception: " + Ex.Message);
  }
}

See Also

Concepts

Building a Custom Contact List