3

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.

Reg Edit
  • 6,719
  • 1
  • 35
  • 46
Thomas
  • 2,886
  • 3
  • 34
  • 78

1 Answers1

1

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.

Reg Edit
  • 6,719
  • 1
  • 35
  • 46
  • tnx that works mostly (setting selectionstart there +1 if right key, or -1 if left key (down to value 0 or up to text.length) but just found out that the left key still triggers that it leaves the cell (after moving left by 1 character). do you happen to know how to stop that behaviour? – Thomas Jun 11 '14 at 09:31
  • Make sure you're overriding `OnKeyUp` as well, in exactly the same way, to defeat the base behaviour. – Reg Edit Jun 11 '14 at 17:56
  • tnx functioned fine with that. I'll mark that answer as accepted and upvote it. Seemingly as the original bounty had expired (due to first my RL issues not being able to test it because of that) I would need 200 rep to be able to give out a reward for you there (seemingly no way around that). tnx still. – Thomas Jun 13 '14 at 06:07