From d037b7f94807d112987d8386605923588f28b783 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 16 Nov 2019 02:01:31 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20ListViewExtensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MoveToTop() moves the selected list item to the top of the list view by removing the item, and inserting it at index 0. --- X10D.WinForms/ListViewExtensions.cs | 35 +++++++++++++++++++++++++++++ X10D.WinForms/X10D.WinForms.csproj | 1 + 2 files changed, 36 insertions(+) create mode 100644 X10D.WinForms/ListViewExtensions.cs diff --git a/X10D.WinForms/ListViewExtensions.cs b/X10D.WinForms/ListViewExtensions.cs new file mode 100644 index 0000000..e0dcd36 --- /dev/null +++ b/X10D.WinForms/ListViewExtensions.cs @@ -0,0 +1,35 @@ +namespace X10D.WinForms +{ + #region Using Directives + + using System.Windows.Forms; + + #endregion + + /// + /// A set of extension methods for and . + /// + public static class ListViewExtensions + { + /// + /// Moves the to the top of the specified . + /// + /// The to move. + /// Optional. The parent . Defaults to the current parent. + public static void MoveToTop(this ListViewItem item, ListView listView = null) + { + if (listView == null) + { + listView = item.ListView; + } + + if (listView.Items.Contains(item)) + { + int i = listView.Items.IndexOf(item); + listView.Items.RemoveAt(i); + } + + listView.Items.Insert(0, item); + } + } +} diff --git a/X10D.WinForms/X10D.WinForms.csproj b/X10D.WinForms/X10D.WinForms.csproj index 9fe7bc2..93359cd 100644 --- a/X10D.WinForms/X10D.WinForms.csproj +++ b/X10D.WinForms/X10D.WinForms.csproj @@ -43,6 +43,7 @@ +