RowHeaderCollection类:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing;
namespace UserControls
{
/// <summary>
/// Summary description for RowHeaderFieldCollection.
/// </summary>
public class RowHeaderFieldCollection : IEnumerable, ICollection
{
/// <summary>用于存取<see cref="RowHeaderField"/>元素</summary>
private ArrayList list;
/// <summary>
/// 在此<see cref="RowHeaderField.Index"/>之前,
/// 所有<see cref="RowHeaderField"/>对象总的高度。
/// </summary>
private int _nBeforeIndexTotalHeight = 0;
/// <summary> 选中的<see cref="RowHeaderField"/></summary>
private int indexInDraging = -1;
/// <summary> 获取选中的<see cref="RowHeaderField"/></summary>
public int IndexInDraging
{
get
{
indexInDraging = -1;
for( int i = 0; i < list.Count; i++ )
{
if ( this[i].InDraging == true )
{
indexInDraging = i;
}
}
return indexInDraging;
}
}
/// <summary> </summary>
private RowHeaderFieldCollection()
{
list = new ArrayList();
}
/// <summary> </summary>
/// <returns></returns>
public static RowHeaderFieldCollection CreateInstance()
{
return new RowHeaderFieldCollection();
}
/// <summary> </summary>
/// <param name="id"></param>
/// <returns></returns>
public RowHeaderField FindByID( int id )
{
for ( int i = 0; i < list.Count; i++ )
{
if ( this[i].ID == id )
{
return list[i] as RowHeaderField;
}
}
return null;
}
/// <summary> </summary>
/// <returns></returns>
public bool IsReadOnly()
{
return list.IsReadOnly;
}
/// <summary> </summary>
/// <param name="index"></param>
/// <returns></returns>
public int GetTotalHaightBeforeIndex( int index )
{
_nBeforeIndexTotalHeight = 0;
if ( index == 0 )
_nBeforeIndexTotalHeight = 0;
else
{
for ( int i = 0; i <= index - 1; i++ )
{
_nBeforeIndexTotalHeight += this[i].Height;
}
}
return _nBeforeIndexTotalHeight;
}
/// <summary>
/// 获取鼠标在<see cref="RowHeaderFieldCollection"/>
/// 内的<see cref="RowHeaderField.Index"/>
/// </summary>
/// <remarks>
/// 若Index小于零,则表示此坐标不在某个<see cref="RowHeaderField"/>
/// 对象内,默认的为 -1
/// </remarks>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int GetIndexRegionByPostion ( float x, float y)
{
RectangleF recF = new RectangleF();
for ( int i = 0; i < list.Count; i++ )
{
recF.X = this[i].X;
recF.Y = this[i].Y + 1;
recF.Width = this[i].Width;
recF.Height = this[i].Height - 1;
if ( x >= recF.Left && x <= recF.Right && y >= recF.Top && y <= recF.Bottom )
return i;
}
return -1;
}
/// <summary>
/// 获取即将拖拽或者正被拖拽的<see cref="RowHeaderField.Index"/>
/// </summary>
/// <remarks>
/// 若Index小于零,则表示没有拖拽的某个<see cref="RowHeaderField"/>
/// 对象,默认的为 -1
/// </remarks>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int GetIndexDraggedRegionByPostion( float x,float y )
{
RectangleF recF = new RectangleF();
for ( int i = 0; i < list.Count; i++ )
{
recF.X = this[i].X;
recF.Y = this[i].Y + 1;
recF.Width = this[i].Width;
recF.Height = this[i].Height - 1;
if ( x >= recF.Left && x <= recF.Right && y <= recF.Bottom + 2 && y >= recF.Bottom + 1 )
return i;
}
return -1;
}
/// <summary>
/// 设置值为<see cref="RowHeaderField.Index"/>
/// 的对象为拖拽对象
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public int SetDraggedField( int index )
{
int indexBef = -1;
int indexCurr = -1;
indexBef = this.IndexInDraging;
indexCurr = index;
if ( indexCurr >= 0 )
{
if ( indexCurr == indexBef && this[indexBef].InDraging == true )
return indexBef;
if ( indexBef >= 0 )
{
this[indexBef].InDraging = false;
}
this[indexCurr].InDraging = true;
this.indexInDraging = indexCurr;
return indexCurr;
}
return indexBef;
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
public int Add( RowHeaderField value )
{
return list.Add(value);
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
public void Remove( RowHeaderField value )
{
list.Remove( value );
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
public void RemoveAt( int index )
{
if ( index >= 0 && index < list.Count )
list.RemoveAt(index);
}
/// <summary>
///
/// </summary>
public RowHeaderField this[int index]
{
get
{
if ( index >= 0 && index < list.Count )
return list[index] as RowHeaderField;
else
return null;
}
set
{
if ( index >= 0 && index < list.Count )
list[index] = value;
}
}
/// <summary>
///
/// </summary>
/// <param name="rhf"></param>
/// <returns></returns>
public bool Contains( RowHeaderField rhf )
{
bool find = false;
for ( int i = 0; i < list.Count; i++ )
{
if ( this[i].ID == rhf.ID )
{
find = true;
}
}
return find;
}
#region IEnumerable 成员
/// <summary>
///
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator()
{
return list.GetEnumerator();
}
#endregion
#region ICollection 成员
/// <summary>
///
/// </summary>
public bool IsSynchronized
{
get
{
return list.IsSynchronized;
}
}
/// <summary>
///
/// </summary>
public int Count
{
get
{
return list.Count;
}
}
/// <summary>
///
/// </summary>
/// <param name="array"></param>
/// <param name="index"></param>
public void CopyTo(Array array, int index)
{
list.CopyTo(array, index);
}
/// <summary>
///
/// </summary>
public object SyncRoot
{
get
{
return list.SyncRoot;
}
}
#endregion
}
}