private void tagsListView_MouseClick(object sender, MouseEventArgs e)
        {
            TagsListViewItem item = (TagsListViewItem)tagsListView.GetItemAt(e.X, e.Y);

            if (item != null)
            {
                Rectangle iconRect  = tagsListView.GetItemRect(item.Index, ItemBoundsPortion.Icon);
                Rectangle checkRect = new Rectangle(0, iconRect.Top, iconRect.Left, iconRect.Height);

                // if there's a selection and the clicked item is in the selection, then toggle the
                // entire selection

                if (checkRect.Contains(e.Location))
                {
                    if (tagsListView.SelectedItems.Count > 0 && tagsListView.SelectedItems.Contains(item))
                    {
                        ToggleItems(tagsListView.SelectedItems);
                    }
                    else
                    {
                        item.Toggle();
                    }
                }
            }
        }
        private void AddTag()
        {
            string text = this.textBox1.Text.Trim();

            if (!TagPresent(text))
            {
                TagsListViewItem item = (TagsListViewItem)tagsListView.Items.Add(new TagsListViewItem(text));
                item.Checked = CheckState.Checked;
                item.Text    = text;
            }
            else
            {
                foreach (TagsListViewItem item in tagsListView.Items)
                {
                    if (item.Text == text)
                    {
                        item.Checked = CheckState.Checked;
                        break;
                    }
                }
            }
            this.textBox1.Text = "";
            addButton.Enabled  = false;
            SortList();
        }
        private void LoadTags(List <string> tags, List <string> indeterminateTags)
        {
            Program.AssertOnEventThread();

            tagsListView.Items.Clear();

            foreach (string tag in Tags.GetAllTags())
            {
                TagsListViewItem item = (TagsListViewItem)tagsListView.Items.Add(new TagsListViewItem(tag));

                if (tags.Contains(tag))
                {
                    item.Checked = CheckState.Checked;
                }
                else if (indeterminateTags.Contains(tag))
                {
                    item.Checked = CheckState.Indeterminate;
                }

                item.Text = tag;
            }
            foreach (string tag in tags)   // We need to include these too, because they may have been recently added and not yet got into GetAllTags()
            {
                if (!TagPresent(tag))
                {
                    TagsListViewItem item = (TagsListViewItem)tagsListView.Items.Add(new TagsListViewItem(tag));
                    item.Checked = CheckState.Checked;
                    item.Text    = tag;
                }
            }
            SortList();
        }
        private void tagsListView_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            okButton.Enabled = true;

            TagsListViewItem itemRenaming = (TagsListViewItem)tagsListView.Items[e.Item];

            if (itemRenaming != null)
            {
                string oldName = itemRenaming.Text;
                string newName = (e.Label == null ? oldName : e.Label.Trim());   // null indicates no change

                if (newName == "")
                {
                    e.CancelEdit = true;
                    return;
                }

                foreach (TagsListViewItem currentItem in tagsListView.Items)
                {
                    if (currentItem != itemRenaming && currentItem.Text == newName)
                    {
                        e.CancelEdit = true;
                        return;
                    }
                }

                // Rename the tag in the list view ourselves (necessary if we've trimmed whitespace from the ends of the name)
                itemRenaming.Text = newName;
                e.CancelEdit      = true;

                // Rename the tag on all the servers
                DelegatedAsyncAction action = new DelegatedAsyncAction(null,
                                                                       String.Format(Messages.RENAME_TAG, oldName),
                                                                       String.Format(Messages.RENAMING_TAG, oldName),
                                                                       String.Format(Messages.RENAMED_TAG, oldName),
                                                                       delegate(Session session)
                {
                    Tags.RenameTagGlobally(oldName, newName);
                });
                action.RunAsync();
            }
        }