Search code examples
c#.netdevexpressxtragridtextedit

How to prevent XtraGrid TextEdit from losing focus when moving cursor beyond contents


I am using an XtraGrid from DeveloperExpress with a column holding strings, edited via a TextEdit control.

When a user enters edit mode (the editor is shown), the text is automatically selected, just as it is in for example Windows Explorer when you hit F2 having something selected.

Now, if the user presses left/right, the focus moves away from the current cell into the cell immediately to the left/right (may also be the cell above/below depending on the column layout).

The same thing happens in the user deselects the contents of the editor by for example press home/end and then moves the caret all the way to the left/right in the cell (to the first/last char) and then moves it another step, "beyond" the contents.

In Windows Explorer, doing the same thing just keeps the caret at the left/rightmost position, it never leaves edit mode unless terminated via for example hitting Escape. Even in Excel, edit mode is not terminated when hitting the left/right cell boundary whilst editing the contents of a cell.

I would like the same behavior in my XtraGrid when in edit mode, but I cannot find any setting or way to do it.

Has anyone run into this problem and found a way to overcome it? Am I perhaps thinking backwards here? I really want to have my application to behave in a consistent and "standard Windows" way as much as possible, which I do not feel is the case as it is.


Solution

  • I think this is what you want...

        private void gridControl1_EditorKeyDown(object sender, KeyEventArgs e)
        {
            GridView view = (sender as GridControl).FocusedView as GridView;
            VisualStyleElement.TextBox.TextEdit edit = view.ActiveEditor as VisualStyleElement.TextBox.TextEdit;
            if (edit == null) return;
            if (view.FocusedColumn.FieldName == "FirstName" && view.FocusedRowHandle % 2 == 0)
            {
                e.Handled = (e.KeyData == Keys.Right && edit.SelectionStart == edit.Text.Length) ||
                    (e.KeyData == Keys.Left && edit.SelectionStart == 0);
            }
        }