Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
375
UltraGrid Keyboard navigation with left and right arrow keys.
posted

I'm expecting that I'll need to custom code for this, but I want to ask before I do.

If I am in edit mode of a cell, before the first character position, and I hit left-arrow, I want to move focus to the previous cell of the row, or last cell of the previous row if I am in the first column.

If I am in edit mode of a cell, after the last character position, and I hit right-arrow, I want to move focus to the next cell of the row, or first cell of the next row if I am in the last column.

Parents
No Data
Reply
  • 469350
    Verified Answer
    Offline posted

    Hi Kipp, 

    There's no really simple way to do this. Typically, the arrows keys move within the cell and the Tab (or shift+tab) key moves between cells. 

    It is, of course, easy enough to trap KeyDown (or KeyUp) and watch for the left and right arrow keys. But then you would have to detect if the caret is in the first or last position. This is a little trickier. I guess if you grid is very simple and every cell is just showing plain text, it would be pretty straightforward. But if you are using other editors like dropdown lists, masked cells, or FormattedText, you would have to handle each case separately, since each editor does something slightly different. You might be able to get it to work in some generic way by accessing the properties of the editor, but the behavior would still be a little weird because once you arrowed into a cell that doesn't enter edit mode, like for example a Checkbox cell, you would be in selection mode instead of edit mode. 

    Anyway... if you want to try to go down that path (or if your grid is, in fact, simple and just has text cells), then here's some code I threw together that seems to work in a very basic test. 


            private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
            {
                var grid = (UltraGrid)sender;
                var activeCell = grid.ActiveCell;
                if (null == activeCell ||
                    false == activeCell.IsInEditMode)
                {
                    return;
                }
                var editor = activeCell.EditorResolved;
                if (false == editor.SupportsSelectableText)
                    return;
                switch (e.KeyCode)
                {
                    case Keys.Left:
                        if (editor.SelectionStart == 0)
                        {
                            e.Handled = true;
                            e.SuppressKeyPress = true;
                            grid.PerformAction(UltraGridAction.PrevCellByTab);
                            grid.PerformAction(UltraGridAction.EnterEditMode);
                        }
                        break;
                    case Keys.Right:
                        if (editor.SelectionStart == editor.TextLength)
                        {
                            e.Handled = true;
                            e.SuppressKeyPress = true;
                            grid.PerformAction(UltraGridAction.NextCell);
                            grid.PerformAction(UltraGridAction.EnterEditMode);
                        }
                        break;
                }
            }

Children