Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#

SourceGrid - Open Source C# Grid Control

Rate me:
Please Sign up or sign in to vote.
4.94/5 (429 votes)
4 Aug 2013MIT24 min read 4.7M   23.7K   1K  
SourceGrid is a free open source grid control. Supports virtual grid, custom cells and editors, advanced formatting options and many others features
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace SourceGrid.Controls
{
	/// <summary>
	/// Summary description for ComboBoxEx.
	/// </summary>
	public class TextBoxButton : System.Windows.Forms.UserControl
	{
		private TextBox txtBox;
		private System.Windows.Forms.Button btDown;
			
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public TextBoxButton()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			btDown.BackColor = Color.FromKnownColor(KnownColor.Control);
			txtBox.AutoSize = false;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.txtBox = new TextBox();
			this.btDown = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// txtBox
			// 
			this.txtBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtBox.Name = "txtBox";
			this.txtBox.Size = new System.Drawing.Size(264, 20);
			this.txtBox.TabIndex = 0;
			this.txtBox.Text = "";
			// 
			// btDown
			// 
			this.btDown.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.btDown.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btDown.Location = new System.Drawing.Point(260, 0);
			this.btDown.Name = "btDown";
			this.btDown.Size = new System.Drawing.Size(22, 64);
			this.btDown.TabIndex = 1;
			this.btDown.Text = "...";
			this.btDown.Click += new System.EventHandler(this.btDown_Click);
			// 
			// TextBoxButton
			// 
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.txtBox,
																		  this.btDown});
			this.Name = "TextBoxButton";
			this.Size = new System.Drawing.Size(282, 64);
			this.ResumeLayout(false);

		}
		#endregion

		public void FillControl()
		{
			btDown.Size = new Size(22,Height);
			btDown.Location = new Point(Width-btDown.Width,0);
			txtBox.Location = new Point(0,0);
			txtBox.Size = new Size(Width-btDown.Width,Height);
		}

		private void btDown_Click(object sender, System.EventArgs e)
		{
			OnButtonClick(EventArgs.Empty);
		}

		protected override void OnSizeChanged(EventArgs e)
		{
			base.OnSizeChanged(e);
			FillControl();
		}

//		private void txtBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
//		{
//			base.OnKeyDown(e);
//		}

		protected virtual void OnButtonClick(EventArgs e)
		{
			if (m_Cell==null)
				throw new ApplicationException("LoadCell not called");

			if (ButtonClick != null)
				ButtonClick(this,e);
		}

		public event EventHandler ButtonClick;

		private object m_EditObject = null;
		public object EditObject
		{
			get
			{
				if (m_Cell!=null && m_Cell.Model != null)
				{
					if (m_Cell.Model.SupportStringConversion)
						return m_Cell.Model.StringToObject(txtBox.Text);
					else
						return m_EditObject;
				}
				else
					return m_EditObject;
			}
			set
			{
				if (m_Cell!=null && m_Cell.Model!=null)
				{
					if (m_Cell.Model.SupportStringConversion)
						txtBox.Text = m_Cell.Model.ObjectToString(value);
					else
						txtBox.Text = m_Cell.Model.GetStringRappresentation(value);
				}

				m_EditObject = value;
			}
		}

		private bool ReadOnlyTextBox
		{
			get{return txtBox.ReadOnly;}
			set{txtBox.ReadOnly = value;}
		}

		private Cell m_Cell = null;
		public Cell Cell
		{
			get{return m_Cell;}
		}

		public virtual void LoadCell(Cell p_Cell, object p_StartEditValue)
		{
			m_Cell = p_Cell;

			if (p_StartEditValue!=null)
				EditObject = p_StartEditValue;
			else
				EditObject = m_Cell.Value;;

			if (m_Cell.Model.SupportStringConversion)
				ReadOnlyTextBox = false;
			else
				ReadOnlyTextBox = true;

			SelectAllDisplayText();
		}

		public void SelectAllDisplayText()
		{
			txtBox.SelectAll();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions