Share via


Selecting Contacts from the List

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.

The SelectButton control on the contact list form processes all of the contacts selected by the user. A collection of ListViewItem objects is obtained from the SelectedItems property of the ListView object. The collection is iterated over and a string is appended with the SigninName of the selected contact and delimited with a comma. The genericdictionary<int, string> that was created and filled when the contact list was loaded is used here to obtain the SigninNames of the selected contacts.

        private void SelectButton_Click(object sender, EventArgs e)
        {
            /*
             * When this form is opened and the SelectMode property is
             * set to true, this selectButton is displayed so that the user
             * can select multiple contacts. The selected contacts are returned
             * with the SignInName property which will contain a comma-separated list
             * of contact sign-in names.
             * This code iterates on the collection of selected listview items and looks
             * up the corresponding signin name from the dictionary<int,string> object listDict
             * Each signin name is appended to a string. Until the count of 
             * selected contacts processed by the foreach loop reaches the total number of contacts
             * processed, a comma is appended to the string.
             */
            StringBuilder sb = new StringBuilder();
            int currentSelectedContact = 1;
            foreach (ListViewItem selectedContactItem in contactListView.SelectedItems)
            {
                if (contactListView.SelectedItems.Count == 1)
                {
                    //append signin name of contact
                    sb.Append(listDict[selectedContactItem.Index+1]);
                }
                else
                {
                    //append signin name of contact
                    sb.Append(listDict[selectedContactItem.Index+1]);

                    //append a comma if this contact is not the last contact selected
                    if (currentSelectedContact < contactListView.SelectedItems.Count)
                        sb.Append(", ");
                    currentSelectedContact++;
                }
            }
            // fill property with string built by foreach loop
            signInName = sb.ToString();
            this.Close();
        }

See Also

Concepts

Building a Custom Contact List