📄 treelistviewitemcollection.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
namespace System.Windows.Forms
{
/// <summary>
/// Represents the collection of items in a TreeListView control or in a TreeListViewItem
/// </summary>
public class TreeListViewItemCollection : CollectionBase
{
/// <summary>
/// Comparer for TreeListViewItem
/// </summary>
public class TreeListViewItemCollectionComparer : ITreeListViewItemComparer
{
#region Order Property
private SortOrder _sortorder = SortOrder.Ascending;
/// <summary>
/// Sort order
/// </summary>
public SortOrder SortOrder
{
get{return(_sortorder);}
set{_sortorder = value;}
}
private int _column;
/// <summary>
/// Column for the comparison
/// </summary>
public int Column
{
get{return(_column);}
set{_column = value;}
}
#endregion
#region Constructor
/// <summary>
/// Create a new instance of Comparer
/// </summary>
public TreeListViewItemCollectionComparer() : this(SortOrder.Ascending, 0)
{}
/// <summary>
/// Create a new instance of Comparer
/// </summary>
/// <param name="order"></param>
public TreeListViewItemCollectionComparer(SortOrder order) : this(order, 0)
{
SortOrder = order;
}
/// <summary>
/// Create a new instance of Comparer
/// </summary>
/// <param name="order"></param>
/// <param name="column"></param>
public TreeListViewItemCollectionComparer(SortOrder order, int column)
{
SortOrder = order;
_column = column;
}
#endregion
#region Compare
/// <summary>
/// Compare two TreeListViewItems
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int Compare(object x, object y)
{
TreeListViewItem a = (TreeListViewItem) x;
TreeListViewItem b = (TreeListViewItem) y;
int res = 1;
if(Column < a.SubItems.Count && Column < b.SubItems.Count)
res = string.CompareOrdinal(a.SubItems[Column].Text.ToUpper(), b.SubItems[Column].Text.ToUpper());
switch(SortOrder)
{
case SortOrder.Ascending:
return(res);
case SortOrder.Descending:
return(-res);
default:
return(1);
}
}
#endregion
}
#region Private delegates
private delegate void VoidHandlerSortOrder(SortOrder value);
private delegate void VoidHandler();
private delegate void ChangeChildrenCheckStateRecursivelyHandler(CheckState state);
private delegate TreeListViewItemCollection GetCollectionHandler();
private delegate string GetStringHandler();
private delegate int GetIntHandler();
#endregion
#region Properties
#region Sortable
private bool _sortable = true;
/// <summary>
/// Gets or sets a value indicating whether the items of the collection can be reorder while the sort function is being called
/// </summary>
public bool Sortable
{
get
{
return _sortable;
}
set
{
_sortable = value;
}
}
#endregion
#region SortOrder
/// <summary>
/// Get or set the new sortorder (apply automatically the sort function
/// if the sortorder value is changed)
/// </summary>
public SortOrder SortOrder
{
get{return(Comparer.SortOrder);}
set{Comparer.SortOrder = value;
Sort(false);}
}
#endregion
#region Comparer
private ITreeListViewItemComparer _comparer = new TreeListViewItemCollectionComparer(SortOrder.Ascending);
/// <summary>
/// Gets the comparer used in the Sort and Add functions
/// </summary>
public ITreeListViewItemComparer Comparer
{
get{return(_comparer);}
set{_comparer = value;}
}
#endregion
#region SortOrderRecursively
/// <summary>
/// Set the new sortorder (apply automatically the sort function
/// if the sortorder value is changed) for each collection recursively
/// </summary>
public SortOrder SortOrderRecursively
{
set
{
if(TreeListView != null)
if(TreeListView.InvokeRequired)
{
TreeListView.Invoke(new VoidHandlerSortOrder(SetSortOrderRecursively), new object[] {value});
return;
}
SetSortOrderRecursively(value);
}
}
private void SetSortOrderRecursively(SortOrder value)
{
SortOrder = value;
foreach(TreeListViewItem item in this)
item.Items.SortOrderRecursively = value;
}
internal SortOrder SortOrderRecursivelyWithoutSort
{
set{if(TreeListView != null)
if(TreeListView.InvokeRequired)
throw(new Exception("Invoke Required"));
Comparer.SortOrder = value;
foreach(TreeListViewItem item in this)
item.Items.SortOrderRecursivelyWithoutSort = value;}
}
#endregion
#region Owner
private TreeListView _owner;
/// <summary>
/// TreeListView control that directly contains this collection
/// </summary>
public TreeListView Owner{get{return(_owner);}}
#endregion
#region Parent
private TreeListViewItem _parent;
/// <summary>
/// TreeListViewItem that contains this collection
/// </summary>
public TreeListViewItem Parent{get{return(_parent);}}
#endregion
#region TreeListView
/// <summary>
/// Returns the TreeListView set in Owner or in Parent
/// </summary>
private TreeListView TreeListView
{
get
{
if(Owner != null) return(Owner);
if(Parent != null) return(Parent.ListView);
return(null);
}
}
#endregion
#region this[]
/// <summary>
/// Get an item in the collection
/// </summary>
public virtual TreeListViewItem this[int index]
{
get
{
return((TreeListViewItem) this.List[index]);}
}
#endregion
#endregion
#region Events, Delegates, and internal calls
#region Events
/// <summary>
/// Occurs when an item is added to the collection
/// </summary>
[Description("Occurs when an item is added to the collection.")]
public TreeListViewEventHandler ItemAdded;
/// <summary>
/// Occurs when an item is removed from the collection
/// </summary>
[Description("Occurs when an item is removed from the collection.")]
public TreeListViewEventHandler ItemRemoved;
#endregion
#region On???
/// <summary>
/// Raises the ItemAdded event
/// </summary>
/// <param name="e"></param>
protected virtual void OnItemAdded(TreeListViewEventArgs e)
{
if(ItemAdded != null) ItemAdded(this, e);
}
/// <summary>
/// Raises the ItemRemoved event
/// </summary>
/// <param name="e"></param>
protected virtual void OnItemRemoved(TreeListViewEventArgs e)
{
if(ItemRemoved != null) ItemRemoved(this, e);
}
#endregion
#endregion
#region Constructors
/// <summary>
/// Create a collection in the root of a TreeListView (first level items)
/// </summary>
/// <param name="owner"></param>
public TreeListViewItemCollection(TreeListView owner)
{
_owner = owner;
}
/// <summary>
/// Create a collection within a TreeListViewItem
/// </summary>
/// <param name="parent"></param>
public TreeListViewItemCollection(TreeListViewItem parent)
{
_parent = parent;
}
/// <summary>
/// Create a free TreeListViewItemCollection (items will not be
/// displayed in a TreeListView
/// </summary>
public TreeListViewItemCollection()
{
}
#endregion
#region Sort Functions
/// <summary>
/// Transforms the collection to an array
/// </summary>
public TreeListViewItem[] ToArray()
{
if(TreeListView != null)
if(TreeListView.InvokeRequired)
throw(new Exception("Invoke required"));
int size = this.Count;
TreeListViewItem[] eltsArray = new TreeListViewItem[size];
for(int i = 0 ; i < size ; i++)
eltsArray[i] = this[i];
return(eltsArray);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -