private int lastSortColumn = -1; // Track the last clicked column /// <summary> /// Sorts the ListView by the clicked column, automatically /// reversing the sort order on subsequent clicks of the /// same column. /// </summary> /// <param name="sender"></param> /// <param name="e">Provides the index of the clicked column.</param> private void lvFileList_ColumnClick(object sender, ColumnClickEventArgs e) { // Define a variable of the abstract (generic) comparer ListViewItemComparer comparer = null; // Create an instance of the specific comparer in the 'comparer' // variable. Since each of the explicit comparer classes is // derived from the abstract case class, polymorphism applies. switch (e.Column) { // Line count columns case 1: case 2: case 3: comparer = new FileLinesComparer(); break; // The file extension column case 4: comparer = new FileExtensionComparer(); break; // All other columns sort by file name default: comparer = new FileNameComparer(); break; } // Set the sorting order if (lastSortColumn == e.Column) { if (lvFileList.Sorting == SortOrder.Ascending) { lvFileList.Sorting = SortOrder.Descending; } else { lvFileList.Sorting = SortOrder.Ascending; } } else { lvFileList.Sorting = SortOrder.Ascending; } lastSortColumn = e.Column; // Send the comparer the list view and column being sorted comparer.SortingList = lvFileList; comparer.Column = e.Column; // Attach the comparer to the list view and sort lvFileList.ListViewItemSorter = comparer; lvFileList.Sort(); }
private int lastSortColumn = -1; // Track the last clicked column /// <summary> /// Sorts the Vista_Api.ListView by the clicked column, automatically /// reversing the sort order on subsequent clicks of the /// same column. /// </summary> /// <param name="sender"></param> /// <param name="e">Provides the index of the clicked column.</param> private void lvFileList_ColumnClick(object sender, ColumnClickEventArgs e) { // Define a variable of the abstract (generic) comparer ListViewItemComparer comparer = null; // Create an instance of the specific comparer in the 'comparer' // variable. Since each of the explicit comparer classes is // derived from the abstract case class, polymorphism applies. switch (e.Column) { // Line count columns case 1: case 2: case 3: comparer = new FileLinesComparer(); break; // The file extension column case 4: comparer = new FileExtensionComparer(); break; // All other columns sort by file name default: comparer = new FileNameComparer(); break; } // Set the sorting order if (lastSortColumn == e.Column) { if (lvFileList.Sorting == SortOrder.Ascending) { lvFileList.Sorting = SortOrder.Descending; } else { lvFileList.Sorting = SortOrder.Ascending; } } else { lvFileList.Sorting = SortOrder.Ascending; } lastSortColumn = e.Column; // Send the comparer the list view and column being sorted comparer.SortingList = lvFileList; comparer.Column = e.Column; // Attach the comparer to the list view and sort lvFileList.ListViewItemSorter = comparer; lvFileList.Sort(); }