This is a migrated thread and some comments may be shown as answers.
RadDropDownListEditorElement KeyDown
3 Answers 21 Views
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 04 Apr 2016, 09:50 PM

 

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.

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Apr 2016, 07:51 AM
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.
0
Chris
Top achievements
Rank 1
answered on 06 Apr 2016, 03:37 PM
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.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Apr 2016, 07:56 AM
Hello Chris,

Thank you for writing back. 

In order to cover the case when selecting the item with the mouse, I would suggest 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.
Asked by
Chris
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Chris
Top achievements
Rank 1
Share this question
or