1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 16:15:41 +00:00

Add ListViewExtensions

MoveToTop() moves the selected list item to the top of the list view by
removing the item, and inserting it at index 0.
This commit is contained in:
Oliver Booth 2019-11-16 02:01:31 +00:00
parent ac10a5912e
commit d037b7f948
No known key found for this signature in database
GPG Key ID: 4B0992B2602C3778
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,35 @@
namespace X10D.WinForms
{
#region Using Directives
using System.Windows.Forms;
#endregion
/// <summary>
/// A set of extension methods for <see cref="ListView"/> and <see cref="ListViewItem"/>.
/// </summary>
public static class ListViewExtensions
{
/// <summary>
/// Moves the <see cref="ListViewItem"/> to the top of the specified <see cref="ListView"/>.
/// </summary>
/// <param name="item">The <see cref="ListViewItem"/> to move.</param>
/// <param name="listView">Optional. The parent <see cref="ListView"/>. Defaults to the current parent.</param>
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);
}
}
}

View File

@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ControlExtensions.cs" />
<Compile Include="ListViewExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />