using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace MyGridHead
{
	/// <summary>
	/// UserControl1 的摘要说明。
	/// </summary>
	/// 
	public delegate void TextCell_Click(object sender, System.EventArgs e);
	public delegate void TextCell_MouseDown ( object sender , System.Windows.Forms.MouseEventArgs e );
	public delegate void TextCell_KeyPress ( object sender , System.Windows.Forms.KeyPressEventArgs e );
	public delegate void TextCell_Enter(object sender, System.EventArgs e);
	public delegate void TextCell_Leave(object sender, System.EventArgs e);

	public delegate void TextCell_MouseEnter(object sender, System.EventArgs e);
	public delegate void TextCell_MouseLeave(object sender, System.EventArgs e);
	public delegate void TextCell_TextChanged(object sender, System.EventArgs e);
	public delegate void TextCell_DoubleClick(object sender, System.EventArgs e);
	public delegate void TextCell_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e);
	public delegate void TextCell_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e);


	//	public event AlarmEventHandler Alarm;

	public class MyGridHead : System.Windows.Forms.UserControl
	{


		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		// 定义新事件NewLineRead
		public event TextCell_Click NewCell_Click ;
		public event TextCell_MouseDown NewCell_MouseDown ;
		public event TextCell_KeyPress NewCell_KeyPress ;
		public event TextCell_Enter NewCell_Enter ;
		public event TextCell_Leave NewCell_Leave ;

		public event TextCell_MouseEnter NewCell_MouseEnter ;
		public event TextCell_MouseLeave NewCell_MouseLeave ;
		public event TextCell_TextChanged NewCell_TextChanged ;
		public event TextCell_DoubleClick NewCell_DoubleClick ;
		public event TextCell_KeyUp NewCell_KeyUp ;
		public event TextCell_KeyDown NewCell_KeyDown ;


		//			public event Cell_Click System.EventHandler ;
		////		//给产生的按钮定义一个数量计算器
		////		private int counter ;
		////		//给产生的按钮定义相对位置的纵坐标
		////		private int locY ;
		//给产生的文本框定义一个数量计算器
		//		private int counter01 ;
		//给产生的文本框定义相对位置的纵坐标
		private int locY1 ; 
		//		private int locY ; 
		//		private int locX ; 
		private int borderWidth ; 
		Label [,] ArrCellText;
		private string setcellWidth="100,100,100,100,100,100,100,100"; 
		private int row; 
		private int col;
		private int rows=2; 
		private int cols=6; 
		private int cellColLength=-1; //单元格列间距
		private int cellRowLength=-1; //单元格行间距
		private int cellWidth; 
		private int cellTop; 
		private int cellLeft; 
		private int cellHeight; 
		private Color cellBackColor=Color.White ; 
		private Color cellForeColor=Color.Black ; 
		private int gridWidth; 
		private int gridHeight; 
		private string cellTag;
		private System.Windows.Forms.Label lblBorder;
		private System.Windows.Forms.TextBox Cell;
		private bool gridStyle=true;
		private bool borderStyle=false;
		private bool enterToTab=true;

		/// <summary>
		/// 设置单元格背景色
		/// </summary>
		public Color CellBackColor
		{ 
			get {return cellBackColor;} 
				
			set { cellBackColor=value;} 
		} 	/// <summary>
		/// 设置单元格前景色
		/// </summary>
		public Color CellForeColor
		{ 
			get {return cellForeColor;} 
				
			set { cellForeColor=value;} 
		} 
		/// <summary>
		/// 在单元格中是否接收回车调用tab键
		/// </summary>
		public bool EnterToTab 
		{ 
			get {return enterToTab;} 
				
			set { enterToTab=value;} 
		} 
		/// <summary>
		/// 是否有外边框。
		/// </summary>
		public bool BorderStyle 
		{ 
   
			get {return borderStyle;} 
					

			set { borderStyle=value;} 
		   
		} 
				
		/// <summary>
		/// 单元格边框类型。
		/// </summary>
		public bool GridStyle 
		{ 
   
			get {return gridStyle;} 
					

			set { gridStyle=value;} 
		   
		} 
		/// <summary>
		/// 当前行。
		/// </summary>
		public int Row 
		{ 
   
			get {return row;} 
   
			set {row=value;} 
   
		} 
		/// <summary>
		///当前列。
		/// </summary>
		public int Col 
		{ 
   
			get {return col;} 
   
			set {col=value;} 
   
		} 
		/// <summary>
		/// 网格行数。
		/// </summary>
		public int Rows 
		{ 
   
			get {return rows;} 
   
			set {rows=value;} 
   
		} 
		/// <summary>
		/// 网格列数。
		/// </summary>
		public int Cols 
		{ 
   
			get {return cols;} 
   
			set {cols=value;} 
   
		} 
		/// <summary>
		/// 单元格列间距。
		/// </summary>
		public int CellColLength 
		{ 
			get {return cellColLength;} 
			set { cellColLength=value; 	} 
		}
		/// <summary>
		/// 单元格行间距。
		/// </summary>
		public int CellRowLength 
		{ 
			get {return cellRowLength;} 
			set	{cellRowLength=value; } 
		} 
		/// <summary>
		/// 单元格宽。
		/// </summary>
		public int CellWidth 
		{ 
			get {return cellWidth;} 
		} 
		/// <summary>
		/// 单元格高。
		/// </summary>
		public int CellHeight 
		{ 
			get {return cellHeight;} 
		} 	
		/// <summary>
		/// 单元格顶边距。
		/// </summary>
		public int CellTop 
		{ 
			get {return cellTop;} 
		} 
		/// <summary>
		/// 单元格左边距。
		/// </summary>
		public int CellLeft 
		{ 
			get {return cellLeft;} 
		} 
		/// <summary>
		/// 网格列宽值信息。
		/// </summary>
		public string SetCellWidth 
		{   
			get { return setcellWidth; } 
			set 
			{    
				if (value==null) 
					setcellWidth="100,100,100,100,100,100,100,100"; 
				else    
					setcellWidth=value; 
			} 
		} 
		/// <summary>
		/// 单元格扩展信息。
		/// </summary>
		public string CellTag 
		{   
			get { return cellTag; } 
			set 
			{    
				if (value!=null) 
					cellTag=value; 
			} 
		} 
		/// <summary>
		/// 返回指定的单元格内容。
		/// </summary>

		public string CellText(int r,int c) 
		{
		{ return ArrCellText[r,c].Text ;}

		}
		public MyGridHead()
		{
			// 该调用是 Windows.Forms 窗体设计器所必需的。
			InitializeComponent();
			ArrCellText=new Label [10,10];
			// TODO: 在 InitComponent 调用后添加任何初始化

		}
		public MyGridHead(int r,int c)
		{
			InitializeComponent();
			rows=r;
			cols=c;

		}
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			//			for (int j=0;j<rows;j++)
			//			{
			//				for (int i=0;i<cols;i++)
			//				{
			//					ArrCellText[j,i].Dispose() ;
			//				}
			//
			//			}
			base.Dispose( disposing );
		}

		#region 组件设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器 
		/// 修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.lblBorder = new System.Windows.Forms.Label();
			this.Cell = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// lblBorder
			// 
			this.lblBorder.BackColor = System.Drawing.Color.Transparent;
			this.lblBorder.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.lblBorder.Location = new System.Drawing.Point(2, 4);
			this.lblBorder.Name = "lblBorder";
			this.lblBorder.Size = new System.Drawing.Size(80, 86);
			this.lblBorder.TabIndex = 9;
			this.lblBorder.Text = "MyGrid";
			this.lblBorder.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.lblBorder.Resize += new System.EventHandler(this.lblBorder_Resize);
			// 
			// Cell
			// 
			this.Cell.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.Cell.Cursor = System.Windows.Forms.Cursors.Arrow;
			this.Cell.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.Cell.Location = new System.Drawing.Point(82, 8);
			this.Cell.Name = "Cell";
			this.Cell.Size = new System.Drawing.Size(102, 21);
			this.Cell.TabIndex = 8;
			this.Cell.Text = "我的网格";
			this.Cell.Visible = false;
			this.Cell.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Cell_KeyDown);
			this.Cell.TextChanged += new System.EventHandler(this.Cell_TextChanged);
			this.Cell.DoubleClick += new System.EventHandler(this.Cell_DoubleClick);
			this.Cell.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Cell_KeyUp);
			// 
			// MyGrid
			// 
			this.BackColor = System.Drawing.SystemColors.Control;
			this.Controls.Add(this.lblBorder);
			this.Controls.Add(this.Cell);
			this.Name = "MyGrid";
			this.Size = new System.Drawing.Size(214, 114);
			this.Resize += new System.EventHandler(this.MyGrid_Resize);
			this.Load += new System.EventHandler(this.UserControl1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void UserControl1_Load(object sender, System.EventArgs e)
		{

		}
		/// <summary>
		/// 得到可视网格的宽。
		/// </summary>	
		public int GridWidth()
		{
			if (cols<1)
			{gridWidth=50;}
			else
			{
				if (borderStyle==true)	
				{	gridWidth=4;}				
				else			
				{gridWidth=0;}
				for (int i=0;i<cols;i++)
				{
					gridWidth+=ArrCellText[0,i].Width ;
				}
				gridWidth+=(cols-1)*cellColLength;
			}
			return gridWidth;

		}
		/// <summary>
		/// 得到可视网格的高。
		/// </summary>	
		public int GridHeight()
		{
			if (rows<1)
			{
				gridHeight=50;
			}
			else
			{
				if (borderStyle==true)
				{gridHeight=4;}
				else
				{gridHeight=0;}
				for (int i=0;i<rows;i++)
				{
					gridHeight+=ArrCellText[i,0].Height  ;
				}
				gridHeight+=(rows-1)*cellRowLength;
			}
			return gridHeight;
			
		}
		/// <summary>
		/// 设定单元格的内容。
		/// </summary>	
		public void CellText(int r,int c,string t)
		{
			ArrCellText[r,c].Text =" "+t;
		}
		/// <summary>
		/// 返回单元格对象。
		/// </summary>	
		public System.Windows.Forms.Label GridCell(int r,int c)
		{
			return ArrCellText[r,c];
		}
		/// <summary>
		/// 显示网格。
		/// </summary>	
		public void ShowGrid()
		{
			string s;
			ArrCellText=new Label [rows,cols];
			string [] sArr=new string[cols] ;


			if (borderStyle==true)
			{
				Cell.Left =2;
				Cell.Top =2;
				borderWidth=2;
			}
			else
			{
				Cell.Left =0;
				Cell.Top =0;
				borderWidth=0;
			}
			s=setcellWidth;//"50,200,50,150,50,150,50,200";
			sArr=s.Split (',');
			if (gridStyle==false)
			{ 
				Cell.BorderStyle= System.Windows.Forms.BorderStyle.None;
				Cell.Height=Cell.Height+4;
			}
			else
			{
				Cell.BorderStyle= System.Windows.Forms.BorderStyle.FixedSingle ;
			}
			for (int j=0;j<rows;j++)
			{
				locY1=borderWidth;
				for (int i=0;i<cols;i++)
				{   
					InitCell(j,i,int.Parse(sArr[i]));
				}
				Cell.Top =Cell.Top+Cell.Height+cellRowLength;
			}
			//计算出总的宽度
			int w=0;
			for (int i=0;i<cols;i++)
			{
				w+=int.Parse(sArr[i]);
			}
			//计算出最后一列的宽度
			for (int r=0;r<rows;r++)
			{
				ArrCellText[r,cols-1].Width =w-ArrCellText[r,cols-1].Left ;
			}
			lblBorder.SendToBack() ;
			lblBorder.Width=GridWidth() ;
			lblBorder.Height=GridHeight()  ;


		}

		/// <summary>
		/// 设置网格大小(宽高)无滚动边框。
		/// </summary>	
		public void GridSize()
		{
			this.Width = GridWidth();
			this.Height  = GridHeight();
		}

		/// <summary>
		/// 设置(行列)单元格。
		/// </summary>	
		private void InitCell(int r,int c,int w)
		{
			//创建一个新的TextBox组件
			Label myBox = new Label( ) ;
			//设定他的名称和Text属性，以及产生的位置
			myBox.Name = "Cell_" + r.ToString() +"_"+c.ToString()  ;
			myBox.Tag =r.ToString() +c.ToString();//保存索引
//			if (c%2==0) 
//			{
//				myBox.ReadOnly =true;
//				myBox.HideSelection =true ;
//				myBox.Cursor = System.Windows.Forms.Cursors.Arrow;
//				myBox.CausesValidation=true;
//				myBox.TabStop =false;
//				myBox.BackColor =Color.White;
//			}
//			else
//			{
//				myBox.ReadOnly =false;
//				myBox.Text = "";
//			}
			myBox.BorderStyle=Cell.BorderStyle ;
			myBox.ForeColor =cellForeColor;
			myBox.BackColor =cellBackColor;
			myBox.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			myBox.Width=w ;
			myBox.Location = new Point ( locY1,Cell.Location.Y) ; 
			//			将文本空件添加到控件数组中
			ArrCellText[r,c]=myBox;

			//为产生的新的TextBox组件设定事件，本文中为产生的文本框设定了一个事件
			myBox.Click += new System.EventHandler ( this.Cell_Click ) ;
			myBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler ( this.Cell_KeyPress) ;
			myBox.MouseDown += new System.Windows.Forms.MouseEventHandler ( this.Cell_MouseDown) ;
			myBox.Enter += new System.EventHandler(this.Cell_Enter);
			myBox.Leave += new System.EventHandler(this.Cell_Leave);
			myBox.MouseEnter += new System.EventHandler(this.Cell_MouseEnter);
			myBox.MouseLeave += new System.EventHandler(this.Cell_MouseLeave);
			myBox.TextChanged += new System.EventHandler(this.Cell_TextChanged);
			myBox.DoubleClick += new System.EventHandler(this.Cell_DoubleClick);
			myBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Cell_KeyUp);
			myBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Cell_KeyDown);
			//在窗体中显示此文本框
			this.Controls.Add ( myBox ) ;
			//对要产生的文本框的纵坐标的相对位置是前一个产生按钮的相对位置的纵坐标加"3
			if (GridStyle==true) {locY1 +=w+cellColLength;} 			
			else {locY1 +=w;}
			myBox.BringToFront();    //置前

		}

		/// <summary>
		/// 得到网格基本属性值。
		/// </summary>	
		private void GetCellAttribute( object sender)
		{
			TextBox control = ( TextBox ) sender ; 
			string S;
			S=control.Name;

			string[] sArray=S.Split('_');
			row=int.Parse(sArray[1]);
			col=int.Parse(sArray[2]);

			cellTag=control.Tag.ToString()  ;

			cellWidth=control.Width ; 
			cellTop=control.Location.Y;//Top ; 
			cellLeft=control.Location.X ;//Left ; 
			cellHeight=control.Height ; 

		}


		/// <summary>
		/// 定义事件过程。
		/// </summary>	
		private void Cell_MouseEnter ( object sender , System.EventArgs e )
		{
			//			//出箱
			//			TextBox control = ( TextBox ) sender ; 
			//			//设定按钮的背景色
			//			control.BackColor = Color.SkyBlue ;
			if (NewCell_MouseEnter!=null)	
			{
				NewCell_MouseEnter(sender, e );
			}
		}
		//
		//
		//
		private void Cell_MouseLeave ( object sender , System.EventArgs e )
		{
			//			//出箱
			//					TextBox control = ( TextBox ) sender ; 
			//					control.BackColor = Control.DefaultBackColor ;
			if (NewCell_MouseLeave!=null)	
			{
				NewCell_MouseLeave(sender, e );
			}
		}

		/// <summary>
		/// 单元格鼠标按下事件过程。
		/// </summary>	
		public void Cell_MouseDown ( object sender , System.Windows.Forms.MouseEventArgs e )
		{
			TextBox control = ( TextBox ) sender ; 
			GetCellAttribute(control);
			if (NewCell_MouseDown!=null)	
			{
				NewCell_MouseDown(sender, e );
			}
		}
		/// <summary>
		/// 单元格按键事件过程。
		/// </summary>	
		private void Cell_KeyPress ( object sender , System.Windows.Forms.KeyPressEventArgs e )
		{
			TextBox control = ( TextBox ) sender ; 
			GetCellAttribute(sender);
			if (NewCell_KeyPress!=null)	
			{
				NewCell_KeyPress(sender, e );
			}
			if (e.KeyChar == (char)13 && EnterToTab==true) 
			{			SendKeys.Send("{TAB}") ;}

			//				MessageBox.Show ( control.Text + "被按动了！");
		}
		/// <summary>
		/// 单元格单击事件过程。
		/// </summary>	
		private void Cell_Click ( object sender , System.EventArgs e )
		{

			//			if ( sender.GetType ( ) == typeof ( Button ) ) 
			//			{
			//				Button control = ( Button ) sender ;
			//			}
			//			else 
			//			{
			TextBox control = ( TextBox ) sender ; 

			//			}
			GetCellAttribute(sender);
			if (NewCell_Click!=null)	
			{
				NewCell_Click(sender, e );
			}
		}
		/// <summary>
		/// 单元格得到焦点事件过程。
		/// </summary>	
		private void Cell_Enter(object sender, System.EventArgs e)
		{
			GetCellAttribute(sender);
			if (NewCell_Enter!=null)	
			{
				NewCell_Enter(sender, e );
			}
		}
		/// <summary>
		///单元格焦点离开事件过程。
		/// </summary>	
		private void Cell_Leave(object sender, System.EventArgs e)
		{
			GetCellAttribute(sender);
			if (NewCell_Leave!=null)	
			{
				NewCell_Leave(sender, e );
			}
		}

	

		private void MyGrid_SizeChanged(object sender, System.EventArgs e)
		{

			//			if (this.Width > gridWidth)
			//			{
			//			this.Width = gridWidth;
			//			}
			//			if (this.Height > gridHeight)
			//			{
			//			this.Height  = gridHeight;
			//			}

		}

		private void lblBorder_Resize(object sender, System.EventArgs e)
		{
				
		}

		private void MyGrid_Resize(object sender, System.EventArgs e)
		{
			lblBorder.Top=0;
			lblBorder.Left=0;
		}

		private void Cell_TextChanged(object sender, System.EventArgs e)
		{
			if (NewCell_TextChanged!=null)	
			{
				NewCell_TextChanged(sender, e );
			}
		}

		private void Cell_DoubleClick(object sender, System.EventArgs e)
		{
			if (NewCell_DoubleClick!=null)	
			{
				NewCell_DoubleClick(sender, e );
			}
		}

		private void Cell_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (NewCell_KeyUp!=null)	
			{
				NewCell_KeyUp(sender, e );
			}
		}

		private void Cell_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (NewCell_KeyDown!=null)	
			{
				NewCell_KeyDown(sender, e );
			}
		}




	



		
	}
}
