类似Excel行头标题(3)

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
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值