Skip to content

Commit

Permalink
Added IComparer override to custom columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Nov 8, 2018
1 parent eea9ab8 commit f0d13c6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
7 changes: 7 additions & 0 deletions ImageListView/ImageListViewColumnHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// Ozgur Ozcitak ([email protected])

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Resources;
using System.Windows.Forms;
Expand Down Expand Up @@ -177,6 +178,11 @@ public int Width
mImageListView.Refresh();
}
}
/// <summary>
/// Gets or sets the comparer used while sorting items with this custom column.
/// </summary>
[Browsable(false)]
public IComparer<ImageListViewItem> Comparer { get; set; }
#endregion

#region Custom Property Serializers
Expand Down Expand Up @@ -213,6 +219,7 @@ public ImageListViewColumnHeader(ColumnType type, string key, string text, int w
mImageListView = null;
owner = null;
mGuid = Guid.NewGuid();
Comparer = null;

mType = type;
mKey = key;
Expand Down
23 changes: 11 additions & 12 deletions ImageListView/ImageListViewItemCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
// Ozgur Ozcitak ([email protected])

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Manina.Windows.Forms
{
Expand Down Expand Up @@ -641,8 +641,7 @@ internal int IndexOf(ImageListViewItem item)
/// </summary>
internal int IndexOf(Guid guid)
{
ImageListViewItem item = null;
if (lookUp.TryGetValue(guid, out item))
if (lookUp.TryGetValue(guid, out ImageListViewItem item))
return item.Index;
return -1;
}
Expand Down Expand Up @@ -772,10 +771,10 @@ private void AddRemoveGroupItem(int index, bool add)
/// </summary>
private class ImageListViewItemComparer : IComparer<ImageListViewItem>
{
private ImageListViewColumnHeader mGroupColumn;
private readonly ImageListViewColumnHeader mGroupColumn;
private ImageListViewColumnHeader mSortColumn;
private SortOrder mGroupOrder;
private SortOrder mSortOrder;
private readonly SortOrder mGroupOrder;
private readonly SortOrder mSortOrder;

public ImageListViewItemComparer(ImageListViewColumnHeader groupColumn, SortOrder groupOrder, ImageListViewColumnHeader sortColumn, SortOrder sortOrder)
{
Expand Down Expand Up @@ -809,12 +808,9 @@ private int CompareStrings(string x, string y, bool natural)

string xpart = xparts[i];
string ypart = yparts[i];

int xi = 0;
int yi = 0;
int res = 0;

if (int.TryParse(xpart, out xi) && int.TryParse(ypart, out yi))
if (int.TryParse(xpart, out int xi) && int.TryParse(ypart, out int yi))
res = (xi < yi ? -1 : (xi > yi ? 1 : 0));
else
res = string.Compare(xpart, ypart, StringComparison.InvariantCultureIgnoreCase);
Expand Down Expand Up @@ -925,7 +921,10 @@ public int Compare(ImageListViewItem x, ImageListViewItem y)
result = (x.FocalLength < y.FocalLength ? -1 : (x.FocalLength > y.FocalLength ? 1 : 0));
break;
case ColumnType.Custom:
result = CompareStrings(x.SubItems[mSortColumn].Text, y.SubItems[mSortColumn].Text, natural);
if (mSortColumn.Comparer != null)
result = mSortColumn.Comparer.Compare(x, y);
else
result = CompareStrings(x.SubItems[mSortColumn].Text, y.SubItems[mSortColumn].Text, natural);
break;
default:
result = 0;
Expand Down
4 changes: 2 additions & 2 deletions ImageListView/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("13.0.0.0")]
[assembly: AssemblyFileVersion("13.0.0.0")]
[assembly: AssemblyVersion("13.1.0.0")]
[assembly: AssemblyFileVersion("13.1.0.0")]
11 changes: 10 additions & 1 deletion ImageListViewDemo/DemoForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ public DemoForm()
imageListView1.Columns.Add(ColumnType.Dimensions);
imageListView1.Columns.Add(ColumnType.FileSize);
imageListView1.Columns.Add(ColumnType.FolderName);
imageListView1.Columns.Add("random", "Random");
var col = new ImageListView.ImageListViewColumnHeader(ColumnType.Custom, "random", "Random");
col.Comparer = new RandomColumnComparer();
imageListView1.Columns.Add(col);

TreeNode node = new TreeNode("Loading...", 3, 3);
node.Tag = null;
Expand All @@ -128,6 +130,13 @@ public DemoForm()
bw.RunWorkerAsync(node);
}

public class RandomColumnComparer : IComparer<ImageListViewItem>
{
public int Compare(ImageListViewItem x, ImageListViewItem y)
{
return int.Parse(x.SubItems["random"].Text.Substring(1, 1)).CompareTo(int.Parse(y.SubItems["random"].Text.Substring(1, 1)));
}
}
#endregion

#region Update UI while idle
Expand Down

0 comments on commit f0d13c6

Please sign in to comment.