Search code examples
c#winformstelerikdatagridcomboboxcolumn

Positioning cursor in a GridViewComboBoxColumn (Telerik)


I'm using a RadGridView which has a view GridViewComboBoxColumn s inside. The editor itself is a custom editor based upon: RadDropDownListEditor.

I'm currently trying to implement it so that pressing left or right arrow does not affect the cell or the select items but instead moves the cursor inside the editor. Thus my question is how can I access the position of the cursor there.

   class CustomizedDropDownEditor : RadDropDownListEditor
{
    public override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == System.Windows.Forms.Keys.Left || e.KeyCode == System.Windows.Forms.Keys.Right)
        {
            //Customized left right arrow key behaviour

        }
        else
        {
            base.OnKeyDown(e);
        }
    }

I've tried a few things already but didn't come up with a way where I can access the textbox of the editor or the selectionstart in there.

Edit: Despite the above code intercepting the keys the left arrow key still results in a cell leave (the right arrow key does not result in this though). Is there a possibility to avoid this and if so how?

Tnx.


Solution

  • You can access the text in your editor's EditorElement property, once you cast the property to RadDropDownListEditorElement. If you want to do it within the same override:

    class CustomizedDropDownEditor : RadDropDownListEditor
    {
        public override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Left || e.KeyCode == System.Windows.Forms.Keys.Right)
            {
                //Customized left right arrow key behaviour
    
                int selectionStart = ((RadDropDownListEditorElement)this.EditorElement).SelectionStart;
    
                int selectionLength = ((RadDropDownListEditorElement)this.EditorElement).SelectionLength;
    
            }
            else
            {
                base.OnKeyDown(e);
            }
        }
    }
    

    Or if you want to do it from somewhere else, you can do the same thing via the grid's ActiveEditor property (though I don't imagine you'll want to do much else, as the editor will of course close and lose your text selection!):

    private void RadGridView1_OnMouseLeave(object sender, EventArgs e)
    {
        int selectionStart = ((RadDropDownListEditorElement)((CustomizedDropDownEditor)radGridView1.ActiveEditor).EditorElement).SelectionStart;
    }
    

    This Telerik article gives an example of accessing the text when the EndEdit event fires, which you may also be interested in.