RadDropDownListEditorElement KeyDown

4 posts, 0 answers
  1. Chris
    Chris avatar
    9 posts
    Member since:
    Jun 2014

    Posted 04 Apr Link to this post

     

    I've got some weird custom logic to implement on a text field, for which the best solution seems to be a Custom Editor using a RadDropDownListEditorElement. I want the Enter key to commit the current selection and end editing.  The default behavior when Enter is pressed is to close the Popup, but leave the editor focused in edit mode.

    I tried the following (inside the Custom Editor class I defined):

    radDropDownListEditorElement.KeyDown += (o, args) =>
    {
        if (args.KeyCode == Keys.Enter)
        {
            var cell = parentGridView.CurrentCell;
            parentGridView.EndEdit();
            cell.ViewInfo.Refresh();
        }
    };

     

    This works fine if the popup is already closed (for example, by pressing Enter twice, or clicking the mouse and THEN pressing Enter).  But when the popup is open, KeyDown never gets fired.  I've tried capturing radDropDownListEditorElement.Popup.KeyDown and PopupForm.KeyDown, with no luck.  Any idea how to get keypresses when the Popup is open, or a better way to achieve this functionality?  Using a Custom Editor is required for other reasons.

  2. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 06 Apr Link to this post

    Hello Chris,

    Thank you for writing.

    You can handle the RadDropDownListEditorElement.PopupClosed event and check whether the RadPopupClosedEventArgsCloseReason is RadPopupCloseReason.Keyboard. Then, you can call the EndEdit method of RadGridView.
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadDropDownListEditor ddl = e.ActiveEditor as RadDropDownListEditor;
        if (ddl != null)
        {
            RadDropDownListEditorElement el = ddl.EditorElement as RadDropDownListEditorElement;
            el.Popup.PopupClosed -= Popup_PopupClosed;
            el.Popup.PopupClosed += Popup_PopupClosed;
        }
    }
     
    private void Popup_PopupClosed(object sender, RadPopupClosedEventArgs args)
    {
        if (args.CloseReason == RadPopupCloseReason.Keyboard)
        {
            this.radGridView1.EndEdit();
        }
    }

    I hope this information helps. Should you have further questions I would be glad to help.

     Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Chris
    Chris avatar
    9 posts
    Member since:
    Jun 2014

    Posted 06 Apr in reply to Dess Link to this post

    Thanks for the reply.  I had tried using PopupClosed, but the problem was that when the RadPopupCloseReason was Mouse, it wasn't saving the selection (it was reverting back to whatever was previously selected).  However, it's not absolutely essential that editing ends when the mouse is clicked, since if they're using the mouse, they can just click the next cell they want to edit, so your answer will work for me.  Thank you.
  5. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 07 Apr Link to this post

    Hello Chris,

    Thank you for writing back. 

    In order to cover the case when selecting the item with the mouse, I would suggest you to end the edit operation in the RadDropDownListEditor.Popup.MouseUp event when the selection is already changed:
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadDropDownListEditor ddl = e.ActiveEditor as RadDropDownListEditor;
        if (ddl != null)
        {
            RadDropDownListEditorElement el = ddl.EditorElement as RadDropDownListEditorElement;
            el.Popup.PopupClosed -= Popup_PopupClosed;
            el.Popup.PopupClosed += Popup_PopupClosed;
            el.Popup.MouseUp -= Popup_MouseUp;
            el.Popup.MouseUp += Popup_MouseUp;
        }
    }
     
    private void Popup_MouseUp(object sender, MouseEventArgs e)
    {
        DropDownPopupForm form = sender as DropDownPopupForm;
     
        RadListVisualItem elementUnderMouse = form.ElementTree.GetElementAtPoint(e.Location) as RadListVisualItem;
        if (elementUnderMouse != null)
        {
            this.radGridView1.EndEdit();
        }
    }
     
    private void Popup_PopupClosed(object sender, RadPopupClosedEventArgs args)
    {
        if (args.CloseReason == RadPopupCloseReason.Keyboard)
        {
            this.radGridView1.EndEdit();
        }
    }

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top