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

Text editing like a regular edit control

Hello, I have one editable cell in my grid derived from GridTextBoxCellModel. I would like the editing behavior to be exactly like that found in Windows Explorer when you rename a file or directory. I overrode OnKeyDown to handle the arrow keys, but just preventing focus from leaving the cell on arrow keys is insufficient. There are so many special cases that I found myself basically implementing the cursor behavior of an edit control. I'm wondering if there is an easier way (some property I can set?)to get that functionality. Thanks! Laurie

4 Replies

AD Administrator Syncfusion Team October 14, 2003 04:31 PM UTC

I am not sure what you what to do. If I right-click a filename in Windows Explorer, I get the name highlighted with a cursor on the right-end of the name. Is this what you want - the text to be highlighted when the cell goes into editmode? If so, you can use the grid.ActivateCurrentCellBehavior property to handle this, setting it to SelectAll. (The cursor is at the left side, but that can be moved in CurrentCellControlGotFocus). But I suspect I am missing the point of what you need...

LS Laurie Spencer October 14, 2003 04:52 PM UTC

Thanks for the reply! Yes I got the select all part. Here's where I am with this (from OnKeyDown): if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Down )) { e.Handled = true; } else if (e.KeyCode == Keys.Right) { if (TextBox.SelectionLength == TextBox.TextLength) { TextBox.SelectionStart = TextBox.TextLength; TextBox.SelectionLength = 0; e.Handled = true; } else if (TextBox.SelectionStart + TextBox.SelectionLength == TextBox.TextLength) { e.Handled = true; } } else if (e.KeyCode == Keys.Left) { if (TextBox.SelectionLength == TextBox.TextLength) { TextBox.SelectionStart = 0; TextBox.SelectionLength = 0; e.Handled = true; } else if (TextBox.SelectionStart == 0) { e.Handled = true; } } } else { base.OnKeyDown(e); } This is incomplete: the control key still causes the edit control to lose focus. If the first n characters are selected then the left arrow key doesn't work. Plus more little special cases like that. I was hoping that there is an easier way to get the ctrl key, shift key, mouse selection, arrow key functionality without recreating an edit control. Thanks! Laurie

AD Administrator Syncfusion Team October 14, 2003 09:28 PM UTC

If your main concern is not leaving the cell when your user hits any of the arrow keys, you might be able to do it a little simpler using the CurrentCellMoving and just cancel the move if an arrow key was hit. (You could also use your selection code at this point to move the edit control cursor if you need to.)
Keys lastKeyCode = 0;
private void gridControl1_CurrentCellKeyDown(object sender, KeyEventArgs e)
	lastKeyCode = e.KeyCode;

private void gridControl1_CurrentCellMoving(object sender, GridCurrentCellMovingEventArgs e)
	if(lastKeyCode == Keys.Left || lastKeyCode == Keys.Right || lastKeyCode == Keys.Up || lastKeyCode == Keys.Down)
		e.Cancel = true;

LS Laurie Spencer October 15, 2003 03:42 PM UTC

Thanks - much simpler! Laurie

Live Chat Icon For mobile
Up arrow icon