From 81a0b09a700b8d6eece003e54c8e1dba7062ea64 Mon Sep 17 00:00:00 2001 From: Jaex Date: Sat, 24 May 2014 08:07:03 +0300 Subject: [PATCH] MyListView column sort support --- HelpersLib/HelpersLib.csproj | 2 +- HelpersLib/ListViewColumnSorter.cs | 130 ++++++++++++++++++++++++++ HelpersLib/ListViewItemComparer.cs | 54 ----------- HelpersLib/UserControls/MyListView.cs | 35 +++++++ HistoryLib/HistoryForm.Designer.cs | 2 +- HistoryLib/HistoryForm.cs | 6 -- HistoryLib/HistoryForm.resx | 123 ++++++++++++++++++++++++ HistoryLib/HistoryLib.csproj | 5 + 8 files changed, 295 insertions(+), 62 deletions(-) create mode 100644 HelpersLib/ListViewColumnSorter.cs delete mode 100644 HelpersLib/ListViewItemComparer.cs create mode 100644 HistoryLib/HistoryForm.resx diff --git a/HelpersLib/HelpersLib.csproj b/HelpersLib/HelpersLib.csproj index a8654104e..4fb35f6a0 100644 --- a/HelpersLib/HelpersLib.csproj +++ b/HelpersLib/HelpersLib.csproj @@ -101,7 +101,7 @@ QRCodeForm.cs - + Form diff --git a/HelpersLib/ListViewColumnSorter.cs b/HelpersLib/ListViewColumnSorter.cs new file mode 100644 index 000000000..85feb8fd7 --- /dev/null +++ b/HelpersLib/ListViewColumnSorter.cs @@ -0,0 +1,130 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (C) 2007-2014 ShareX Developers + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using System.Collections; +using System.Windows.Forms; + +namespace HelpersLib +{ + /// + /// This class is an implementation of the 'IComparer' interface. + /// + public class ListViewColumnSorter : IComparer + { + /// + /// Specifies the column to be sorted + /// + private int ColumnToSort; + /// + /// Specifies the order in which to sort (i.e. 'Ascending'). + /// + private SortOrder OrderOfSort; + /// + /// Case insensitive comparer object + /// + private CaseInsensitiveComparer ObjectCompare; + + /// + /// Class constructor. Initializes various elements + /// + public ListViewColumnSorter() + { + // Initialize the column to '0' + ColumnToSort = 0; + + // Initialize the sort order to 'none' + OrderOfSort = SortOrder.None; + + // Initialize the CaseInsensitiveComparer object + ObjectCompare = new CaseInsensitiveComparer(); + } + + /// + /// This method is inherited from the IComparer interface. It compares the two objects passed using a case insensitive comparison. + /// + /// First object to be compared + /// Second object to be compared + /// The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y' + public int Compare(object x, object y) + { + int compareResult; + ListViewItem listviewX, listviewY; + + // Cast the objects to be compared to ListViewItem objects + listviewX = (ListViewItem)x; + listviewY = (ListViewItem)y; + + // Compare the two items + compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text); + + // Calculate correct return value based on object comparison + if (OrderOfSort == SortOrder.Ascending) + { + // Ascending sort is selected, return normal result of compare operation + return compareResult; + } + else if (OrderOfSort == SortOrder.Descending) + { + // Descending sort is selected, return negative result of compare operation + return (-compareResult); + } + else + { + // Return '0' to indicate they are equal + return 0; + } + } + + /// + /// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0'). + /// + public int SortColumn + { + set + { + ColumnToSort = value; + } + get + { + return ColumnToSort; + } + } + + /// + /// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending'). + /// + public SortOrder Order + { + set + { + OrderOfSort = value; + } + get + { + return OrderOfSort; + } + } + } +} \ No newline at end of file diff --git a/HelpersLib/ListViewItemComparer.cs b/HelpersLib/ListViewItemComparer.cs deleted file mode 100644 index 02e0deb79..000000000 --- a/HelpersLib/ListViewItemComparer.cs +++ /dev/null @@ -1,54 +0,0 @@ -#region License Information (GPL v3) - -/* - ShareX - A program that allows you to take screenshots and share any file type - Copyright (C) 2007-2014 ShareX Developers - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - Optionally you can also view the license at . -*/ - -#endregion License Information (GPL v3) - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Windows.Forms; - -namespace HelpersLib -{ - public class ListViewItemComparer : IComparer - { - private int col; - - public ListViewItemComparer() - { - col = 0; - } - - public ListViewItemComparer(int column) - { - col = column; - } - - public int Compare(object x, object y) - { - return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text); - } - } -} \ No newline at end of file diff --git a/HelpersLib/UserControls/MyListView.cs b/HelpersLib/UserControls/MyListView.cs index 236089e6b..a44e25010 100644 --- a/HelpersLib/UserControls/MyListView.cs +++ b/HelpersLib/UserControls/MyListView.cs @@ -42,6 +42,10 @@ public class MyListView : ListView [DefaultValue(-1)] public int AutoFillColumnIndex { get; set; } + [DefaultValue(false)] + public bool AllowColumnSort { get; set; } + + private ListViewColumnSorter lvwColumnSorter; private int lineIndex = -1; private int lastLineIndex = -1; private ListViewItem dragOverItem = null; @@ -52,8 +56,12 @@ public MyListView() AutoFillColumn = false; AutoFillColumnIndex = -1; + AllowColumnSort = false; FullRowSelect = true; View = View.Details; + + lvwColumnSorter = new ListViewColumnSorter(); + ListViewItemSorter = lvwColumnSorter; } public void FillColumn(int index) @@ -182,6 +190,33 @@ protected override void OnDragLeave(EventArgs e) Invalidate(); } + protected override void OnColumnClick(ColumnClickEventArgs e) + { + base.OnColumnClick(e); + + if (AllowColumnSort) + { + if (e.Column == lvwColumnSorter.SortColumn) + { + if (lvwColumnSorter.Order == SortOrder.Ascending) + { + lvwColumnSorter.Order = SortOrder.Descending; + } + else + { + lvwColumnSorter.Order = SortOrder.Ascending; + } + } + else + { + lvwColumnSorter.SortColumn = e.Column; + lvwColumnSorter.Order = SortOrder.Ascending; + } + + Sort(); + } + } + private void DrawInsertionLine(int x1, int x2, int y) { using (Graphics g = CreateGraphics()) diff --git a/HistoryLib/HistoryForm.Designer.cs b/HistoryLib/HistoryForm.Designer.cs index 6aa954e1c..0887b6f0d 100644 --- a/HistoryLib/HistoryForm.Designer.cs +++ b/HistoryLib/HistoryForm.Designer.cs @@ -257,6 +257,7 @@ private void InitializeComponent() // // lvHistory // + this.lvHistory.AllowColumnSort = true; this.lvHistory.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); @@ -274,7 +275,6 @@ private void InitializeComponent() this.lvHistory.TabIndex = 1; this.lvHistory.UseCompatibleStateImageBehavior = false; this.lvHistory.View = System.Windows.Forms.View.Details; - this.lvHistory.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.lvHistory_ColumnClick); this.lvHistory.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.lvHistory_ItemSelectionChanged); this.lvHistory.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lvHistory_KeyDown); this.lvHistory.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.lvHistory_MouseDoubleClick); diff --git a/HistoryLib/HistoryForm.cs b/HistoryLib/HistoryForm.cs index f550c369b..747549d59 100644 --- a/HistoryLib/HistoryForm.cs +++ b/HistoryLib/HistoryForm.cs @@ -326,12 +326,6 @@ private void lvHistory_KeyDown(object sender, KeyEventArgs e) e.Handled = true; } - private void lvHistory_ColumnClick(object sender, ColumnClickEventArgs e) - { - lvHistory.ListViewItemSorter = new ListViewItemComparer(e.Column); - lvHistory.Sort(); - } - #endregion Form events } } \ No newline at end of file diff --git a/HistoryLib/HistoryForm.resx b/HistoryLib/HistoryForm.resx new file mode 100644 index 000000000..4727f6dc6 --- /dev/null +++ b/HistoryLib/HistoryForm.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/HistoryLib/HistoryLib.csproj b/HistoryLib/HistoryLib.csproj index e7ec45b92..af2c6477e 100644 --- a/HistoryLib/HistoryLib.csproj +++ b/HistoryLib/HistoryLib.csproj @@ -96,6 +96,11 @@ + + + HistoryForm.cs + +