⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reportitemcollection.cs

📁 c#源代码
💻 CS
字号:
/*
 * Created by SharpDevelop.
 * User: Forstmeier Helmut
 * Date: 01.03.2005
 * Time: 09:19
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;

namespace SharpReportCore {

	/// <summary>
	///   A collection that stores <see cref='IItemRenderer'/> objects.
	/// </summary>
	[Serializable()]
	public class ReportItemCollection : CollectionBase {
		
		public event ItemCollectionEventHandler ItemAdded;
		public event ItemCollectionEventHandler ItemRemoved;
		
		/// <summary>
		///   Initializes a new instance of <see cref='IItemRendererCollection'/>.
		/// </summary>
		public ReportItemCollection()
		{
		}
		
		/// <summary>
		///   Initializes a new instance of <see cref='IItemRendererCollection'/> based on another <see cref='IItemRendererCollection'/>.
		/// </summary>
		/// <param name='val'>
		///   A <see cref='IItemRendererCollection'/> from which the contents are copied
		/// </param>
		public ReportItemCollection(ReportItemCollection val)
		{
			this.AddRange(val);
		}
		
		/// <summary>
		///   Initializes a new instance of <see cref='IItemRendererCollection'/> containing any array of <see cref='IItemRenderer'/> objects.
		/// </summary>
		/// <param name='val'>
		///       A array of <see cref='IItemRenderer'/> objects with which to intialize the collection
		/// </param>
		public ReportItemCollection(IItemRenderer[] val)
		{
			this.AddRange(val);
		}
		
		/// <summary>
		///   Represents the entry at the specified index of the <see cref='IItemRenderer'/>.
		/// </summary>
		/// <param name='index'>The zero-based index of the entry to locate in the collection.</param>
		/// <value>The entry at the specified index of the collection.</value>
		/// <exception cref='ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public IItemRenderer this[int index] {
			get {
				return ((IItemRenderer)(List[index]));
			}
			set {
				List[index] = value;
			}
		}
		
		public IItemRenderer this[string itemName] {
			get {
				int i = FindByName (itemName);
				if ( i > -1 ){
					return(IItemRenderer)List[i];
				}
				return null;
			}
		}
		
		private int FindByName (string name) {
			for (int i = 0;i < List.Count ;i++ ) {
				if (((IItemRenderer)List[i]).Name== name) {
					return i;
				}
			}
			return -1;
		}
		/// <summary>
		///   Adds a <see cref='IItemRenderer'/> with the specified value to the 
		///   <see cref='IItemRendererCollection'/>.
		/// </summary>
		/// <param name='val'>The <see cref='IItemRenderer'/> to add.</param>
		/// <returns>The index at which the new element was inserted.</returns>
		/// <seealso cref='IItemRendererCollection.AddRange'/>
		public int Add(IItemRenderer val){
			ReportItemCollectionEventArgs e = new ReportItemCollectionEventArgs();
			e.Item = val;
			if (ItemAdded != null){
				ItemAdded(this, e);
			}
			this.SortByLocation();
			return List.Add(val);
		}
		
		/// <summary>
		///   Copies the elements of an array to the end of the <see cref='IItemRendererCollection'/>.
		/// </summary>
		/// <param name='val'>
		///    An array of type <see cref='IItemRenderer'/> containing the objects to add to the collection.
		/// </param>
		/// <seealso cref='IItemRendererCollection.Add'/>
		public void AddRange(IItemRenderer[] val)
		{
			
			for (int i = 0; i < val.Length; i++) {
				
				if (ItemAdded != null){
					ReportItemCollectionEventArgs e = new ReportItemCollectionEventArgs();
					e.Item = (IItemRenderer)val[i];
					ItemAdded(this, e);
				}
				this.Add(val[i]);
			}
		}
		
		/// <summary>
		///   Adds the contents of another <see cref='IItemRendererCollection'/> to the end of the collection.
		/// </summary>
		/// <param name='val'>
		///    A <see cref='IItemRendererCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <seealso cref='IItemRendererCollection.Add'/>
		public void AddRange(ReportItemCollection val)
		{
			for (int i = 0; i < val.Count; i++)
			{
				if (ItemAdded != null){
					ReportItemCollectionEventArgs e = new ReportItemCollectionEventArgs();
					e.Item = (IItemRenderer)val[i];
					ItemAdded(this, e);
				}
				this.Add(val[i]);
			}
		}
		
		/// <summary>
		///   Gets a value indicating whether the 
		///    <see cref='IItemRendererCollection'/> contains the specified <see cref='IItemRenderer'/>.
		/// </summary>
		/// <param name='val'>The <see cref='IItemRenderer'/> to locate.</param>
		/// <returns>
		/// <see langword='true'/> if the <see cref='IItemRenderer'/> is contained in the collection; 
		///   otherwise, <see langword='false'/>.
		/// </returns>
		/// <seealso cref='IItemRendererCollection.IndexOf'/>
		public bool Contains(IItemRenderer val)
		{
			return List.Contains(val);
		}
		
		/// <summary>
		///   Copies the <see cref='IItemRendererCollection'/> values to a one-dimensional <see cref='Array'/> instance at the 
		///    specified index.
		/// </summary>
		/// <param name='array'>The one-dimensional <see cref='Array'/> that is the destination of the values copied from <see cref='IItemRendererCollection'/>.</param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <exception cref='ArgumentException'>
		///   <para><paramref name='array'/> is multidimensional.</para>
		///   <para>-or-</para>
		///   <para>The number of elements in the <see cref='IItemRendererCollection'/> is greater than
		///         the available space between <paramref name='arrayIndex'/> and the end of
		///         <paramref name='array'/>.</para>
		/// </exception>
		/// <exception cref='ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='ArgumentOutOfRangeException'><paramref name='arrayIndex'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='Array'/>
		public void CopyTo(IItemRenderer[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		///    Returns the index of a <see cref='IItemRenderer'/> in 
		///       the <see cref='IItemRendererCollection'/>.
		/// </summary>
		/// <param name='val'>The <see cref='IItemRenderer'/> to locate.</param>
		/// <returns>
		///   The index of the <see cref='IItemRenderer'/> of <paramref name='val'/> in the 
		///   <see cref='IItemRendererCollection'/>, if found; otherwise, -1.
		/// </returns>
		/// <seealso cref='IItemRendererCollection.Contains'/>
		public int IndexOf(IItemRenderer val)
		{
			return List.IndexOf(val);
		}
		
		/// <summary>
		///   Inserts a <see cref='IItemRenderer'/> into the <see cref='IItemRendererCollection'/> at the specified index.
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='IItemRenderer'/> to insert.</param>
		/// <seealso cref='IItemRendererCollection.Add'/>
		public void Insert(int index, IItemRenderer val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		///  Returns an enumerator that can iterate through the <see cref='IItemRendererCollection'/>.
		/// </summary>
		/// <seealso cref='IEnumerator'/>
		public new IItemRendererEnumerator GetEnumerator()
		{
			return new IItemRendererEnumerator(this);
		}
		
		/// <summary>
		///   Removes a specific <see cref='IItemRenderer'/> from the <see cref='IItemRendererCollection'/>.
		/// </summary>
		/// <param name='val'>The <see cref='IItemRenderer'/> to remove from the <see cref='IItemRendererCollection'/>.</param>
		/// <exception cref='ArgumentException'><paramref name='val'/> is not found in the Collection.</exception>
		public void Remove(IItemRenderer val)
		{
			if (ItemRemoved != null){
				ReportItemCollectionEventArgs e = new ReportItemCollectionEventArgs(val);
				ItemRemoved(this, e);
			}
			BaseReportItem i = val as BaseReportItem;
			if (i != null) {
				i.Dispose();
			}
			List.Remove(val);
			this.SortByLocation();
		}
	
		
		#region Sort the List
		///<summary>
		/// Sort the InnerList, so we have the varios Reportitems allway's in the
		/// order we print them.
		/// </summary>
		
		public void SortByLocation() {
			IComparer sorter = new LocationSorter();
			if (this.InnerList.Count > 1) {
				this.InnerList.Sort(sorter);
			}
			
		}
		///<summary>
		/// Comparer to Sort the <see cref="IItemRendererCollection"></see>
		/// by System.Drawing.Location.Y, so we have the IItemRenderers in the Order we use them
		/// (Line by Line)
		/// </summary>
		private class LocationSorter : IComparer  {

			public int Compare(object leftValue, object rightValue){
				// Windows.Forms.Control angenommen...
				BaseReportItem left = leftValue as BaseReportItem;
				BaseReportItem  right = rightValue as BaseReportItem;
				if (left == null){
					if (right == null){
						return 0;
					}
					
					return -1;
				}
				if (right == null){
					return 1;
				}
				
				if (left.Location.Y == right.Location.Y){
					return left.Location.X - right.Location.X;
				}
				
				return left.Location.Y - right.Location.Y;
			}
		}
		#endregion
		
		/// <summary>
		///   Enumerator that can iterate through a IItemRendererCollection.
		/// </summary>
		/// <seealso cref='IEnumerator'/>
		/// <seealso cref='IItemRendererCollection'/>
		/// <seealso cref='IItemRenderer'/>
		public class IItemRendererEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			/// <summary>
			///   Initializes a new instance of <see cref='IItemRendererEnumerator'/>.
			/// </summary>
			public IItemRendererEnumerator(ReportItemCollection mappings)
			{
				this.temp = ((IEnumerable)(mappings));
				this.baseEnumerator = temp.GetEnumerator();
			}
			
			/// <summary>
			///   Gets the current <see cref='IItemRenderer'/> in the <seealso cref='IItemRendererCollection'/>.
			/// </summary>
			public IItemRenderer Current {
				get {
					return ((IItemRenderer)(baseEnumerator.Current));
				}
			}
			
			object IEnumerator.Current {
				get {
					return baseEnumerator.Current;
				}
			}
			
			/// <summary>
			///   Advances the enumerator to the next <see cref='IItemRenderer'/> of the <see cref='IItemRendererCollection'/>.
			/// </summary>
			public bool MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			/// <summary>
			///   Sets the enumerator to its initial position, which is before the first element in the <see cref='IItemRendererCollection'/>.
			/// </summary>
			public void Reset()
			{
				baseEnumerator.Reset();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -