Search code examples
c#.netdevexpress

DevExpress select all when clicked on GridView cell (TextEdit)


I need to make it so when the user clicks on a cell with TextEdit in a grid view, it will select all in the textedit. I tried many many ways i could find in the internet, but none of them work well.

"EditorShowMode = MouseUp" way breaks everything, for example when you click on a cell that has checkedit; it selects the cell, then you need o click again to actually click on the CheckEdit.

"Use EditorShowMode = MouseUp and manually handle other things on MouseDown" is just ew. Won't work fine for all types of controls.

"Change selection length etc. on ShownEditor event" way doesn't work too, actually it selects the text when clicked, but it doesn't override the default function so the selection instantly changes. Also tried the SelectAll method but it had some problems that i dont remember (probably didnt work at all).

I have really tried many things, but couldn't find a totally fine way. Please tell me if you can get a working way without breaking other types of controls in the grid.


Solution

  • Answered by Pavel on DevExpress Support (works great):

    The easiest way to achieve this is to use the GridView.ShownEditor event to subscribe to the active editor's MouseUp event. Then, select all text in the MouseUp event handler and detach this handler to avoid subsequent text selection.

    private void GridView_ShownEditor(object sender, EventArgs e)
    {
        GridView view = sender as GridView;
        if (view.ActiveEditor is TextEdit)
            view.ActiveEditor.MouseUp += ActiveEditor_MouseUp;
    }
    
    private void ActiveEditor_MouseUp(object sender, MouseEventArgs e)
    {
        BaseEdit edit = sender as BaseEdit;
        edit.MouseUp -= ActiveEditor_MouseUp;
        edit.SelectAll();
    }