We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

User Control Keypress in Cell

Hi I'm using a custom control in a cell by setting the CellType to "Control" and setting the CellStyle.Control property. The custom control is derived from a standard TextBox, but when the grid goes into Edit Mode for this cell, it stops all the cursor key presses from getting to the control. Is there any way round this so my control can handle the cursor keypresses (also Tab, Delete etc)? Thanks Martin

3 Replies

AD Administrator Syncfusion Team July 22, 2003 12:31 PM UTC

You can catch the CurrentCellKeyDown event and explicitly handle the keys if your control is the current cell. The snippet below has a TextBox named box as a "Control" cell type, and these snippets moves the right and left arrow keys in the control.
private void gridControl1_CurrentCellKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
	GridCurrentCell cc = this.gridControl1.CurrentCell;
	if(cc.ColIndex == 2 && cc.RowIndex == 2)
	{
		Keys keyCode = e.KeyCode;

		if(keyCode == Keys.Tab || keyCode == Keys.Delete

			|| keyCode == Keys.Left || keyCode == Keys.Right)

		{

			string s = "";
			switch(keyCode)

			{
				case Keys.Delete:
				break;

				case Keys.Tab:
				break;
				case Keys.Left:
					if(this.box.SelectionStart > 0)
						this.box.SelectionStart--;
					break;
				case Keys.Right:
					if(this.box.SelectionStart < this.box.Text.Length)
						this.box.SelectionStart++;
					break;
			}
			e.Handled = true;
		}
	}
}


MA Martin July 23, 2003 03:38 AM UTC

But doing this would mean duplicating code that my control already should handle, as well as just the arrow keys, also escape doesn't go through (which I use as an undo edit feature) also copy & paste, selection of text by holding down shift, etc etc... Thanks.. Martin


AD Administrator Syncfusion Team July 23, 2003 04:56 AM UTC

Then in your derived control class, expose a method (make it public) that allows you to effectively call your controls' OnKeyDown (which is protected). Then in the above handler just call this newly exposed method to let your control process any given key.

Loader.
Live Chat Icon For mobile
Up arrow icon