This is a migrated thread and some comments may be shown as answers.

How to detect Cancelation in custom Editor

2 Answers 93 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
gipsy
Top achievements
Rank 1
gipsy asked on 09 Dec 2020, 04:32 PM

Hello,

I have a custom Editor derived from BaseInputEditor based on a RadButtonTextBoxElement.

In the "EndEdit()" function I change another property of the selected object, how can I see if the user canceled the edit by pressing ESC, so I can prevent the change of the other property?

2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 10 Dec 2020, 06:49 AM
Hello, Andreas,    

RadPropertyGrid handles pressing the Escape key in its ProcessDialogKey method:
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (this.IsEditing && ((keyData == Keys.Return) || (keyData == Keys.Escape)))
            {
                if (keyData == Keys.Return)
                {
                    this.EndEdit();
                }
                else if (keyData == Keys.Escape)
                {
                    this.CancelEdit();
                }

                return true;
            }

            return base.ProcessDialogKey(keyData);
        }

There is no dedicated event for handling when the edit operation is rejected by pressing the Escape key. You can create a derivative of RadPropertyGrid and override its ProcessDialogKey method to detect the specific key. 

An alternative approach is to handle the keyboard input in the activated editor. Here is demonstrated a sample code snippet:

        public RadForm1()
        {
            InitializeComponent();

            this.radPropertyGrid1.SelectedObject = this;
            this.radPropertyGrid1.EditorInitialized+=radPropertyGrid1_EditorInitialized;
        }

        private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
        {
            PropertyGridTextBoxEditor textBoxEditor = e.Editor as PropertyGridTextBoxEditor;
            if (textBoxEditor!=null)
            {
                BaseTextBoxEditorElement el = textBoxEditor.EditorElement as BaseTextBoxEditorElement;
                el.TextBoxItem.TextBoxControl.PreviewKeyDown+=TextBoxControl_PreviewKeyDown;
            }
        }

        private void TextBoxControl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyData== Keys.Escape)
            {
                Console.WriteLine("Escape");
            }
        }

Feel free to use this approach which suits your custom requirements best.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
gipsy
Top achievements
Rank 1
answered on 10 Dec 2020, 08:56 AM
Thanks, this is solves my problem. I was using the KeyDown event to try the same result, but there the Escape Key is already filtered out, the PreviewKeyDown event is working!
Tags
PropertyGrid
Asked by
gipsy
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
gipsy
Top achievements
Rank 1
Share this question
or