/// <summary> /// Fills or refreshes the supplied listview with the current cards and based on the supplied filter. /// </summary> /// <param name="listView">The list view.</param> /// <param name="filter">The card filter.</param> /// <param name="preserveSelection">if set to <c>true</c> [preserve selection].</param> /// <remarks>Documented by Dev02, 2007-12-17</remarks> public void RefreshListView(ListView listView, MaintainCardFilter filter, bool preserveSelection) { List<int> selectedIDs = null; int topIndex = -1; listView.BeginUpdate(); //save selection if (preserveSelection) { selectedIDs = new List<int>(); foreach (ListViewItem lvi in listView.SelectedItems) selectedIDs.Add((lvi.Tag as ICard).Id); //preserve scroll position if (listView.TopItem != null) topIndex = listView.TopItem.Index; } else { //[ML-770] Clear selection when it should not be preserved listView.SelectedIndices.Clear(); } //add the items listView.Items.Clear(); listView.Items.AddRange(GetCards(filter).ToArray()); //restore selection if (selectedIDs != null && selectedIDs.Count > 0) { if (selectedIDs.Count < indexIDs.Count / 4) { //faster for a low amount of selected items foreach (int ID in selectedIDs) { ListViewItem lvi = cards[ID]; if (listView.Items.Contains(lvi)) { listView.Items[listView.Items.IndexOf(lvi)].Selected = true; } } } else { //faster for a high amount of selected items foreach (ListViewItem lvi in listView.Items) { if (selectedIDs.Contains((lvi.Tag as ICard).Id)) lvi.Selected = true; } } } //ensure the first selected item to be visible if (preserveSelection) { if (topIndex > 0 && topIndex < listView.Items.Count) listView.TopItem = listView.Items[topIndex]; else if (listView.SelectedItems.Count > 0) listView.SelectedItems[0].EnsureVisible(); } listView.EndUpdate(); }
/// <summary> /// Redraws the listView with the current values. /// </summary> /// <param name="preserveSelection">if set to <c>true</c> [preserve selection].</param> /// <remarks>Documented by Dev00, 2007-07-19</remarks> private void RefreshListView(bool preserveSelection) { Cursor.Current = Cursors.WaitCursor; MaintainCardFilter filter = new MaintainCardFilter(); filter.Chapter = (comboBoxShowChapter.SelectedItem is IChapter) ? ((IChapter)comboBoxShowChapter.SelectedItem).Id : -1; filter.CardActive = (comboBoxShowChapter.SelectedItem is string) ? comboBoxShowChapter.SelectedItem as string == Resources.ACTIVE_CARDS ? true : comboBoxShowChapter.SelectedItem as string == Resources.INACTIVE_CARDS ? false : (bool?)null : (bool?)null; filter.SearchString = SearchBox.Text; CardDB.RefreshListView(listViewCards, filter, preserveSelection); labelSelectedCardsCount.Text = CardDB.TotalCardCount.ToString(); listViewCards.Sort(); Cursor.Current = Cursors.Default; }
/// <summary> /// Gets the cards matching the filter. /// </summary> /// <param name="filter">The filter.</param> /// <returns>List of matching cards.</returns> /// <remarks>Documented by Dev02, 2007-12-17</remarks> public List<ListViewItem> GetCards(MaintainCardFilter filter) { List<ListViewItem> getCards = new List<ListViewItem>(); List<int> foundIndezes = new List<int>(); if (filter.SearchString.Length > 0) { if (filter.CaseSensitive) { for (int i = 0; i < indexStrings.Count; i++) { if (indexStrings[i].Contains(filter.SearchString)) foundIndezes.Add(i); } } else { for (int i = 0; i < indexStrings.Count; i++) { if (indexStrings[i].ToLower().Contains(filter.SearchString.ToLower())) foundIndezes.Add(i); } } foreach (int index in foundIndezes) { if ((filter.Chapter == -1 || filter.Chapter == indexChapters[index]) && (!filter.CardActive.HasValue || (cards[indexIDs[index]].Tag as ICard).Active == filter.CardActive.Value)) getCards.Add(cards[indexIDs[index]]); } } else { for (int index = 0; index < indexStrings.Count; index++) { if ((filter.Chapter == -1 || filter.Chapter == indexChapters[index]) && (!filter.CardActive.HasValue || (cards[indexIDs[index]].Tag as ICard).Active == filter.CardActive.Value)) getCards.Add(cards[indexIDs[index]]); } } return getCards; }