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

Trigger form key events from editor

7 Answers 814 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dreyfus
Top achievements
Rank 1
Veteran
Dreyfus asked on 19 Jun 2020, 08:30 AM

Hello. I searched for similar problems in the threads but can't find one that solves my problem.

My form's KeyPreview property is set to true, and I have a code for Form_KeyDown event as follows:

private void Form_KeyDown(Object eventSender, KeyEventArgs eventArgs)
{
        ...some code
}

Now, when I press the ESC key inside one of the cells in edit mode, I expect the Form_KeyDown event to fire. But in this case, it does not. It only fires when the grid is not in edit mode.

How can I automatically trigger these form key events from the editors, without manually calling them from CellEditorInitialized?

 

Thank you in advance.

7 Answers, 1 is accepted

Sort by
0
Dreyfus
Top achievements
Rank 1
Veteran
answered on 23 Jun 2020, 08:07 AM
It would be nice to have some update on this.
0
Nadya | Tech Support Engineer
Telerik team
answered on 23 Jun 2020, 01:20 PM

Hello Jay,

Note that while the RadGridView is not in edit mode, it handles mouse and keyboard input by a GridRowBehavior. Depending on the row type, RadGridView introduces different row behaviors. The following help article demonstrates how to use them: Row behaviors.

Once the cell enters edit mode, the respective editor is activated and RadGridView handles the Esc key and this is the desired behavior. The possible solution that I can suggest in your case is to create a custom GridDataRowBehavior and override the ProcessEscapeKey method. As a result, the KeyDown event of the grid will be triggered. Please refer to the following code snippet:

public RadForm1() { InitializeComponent(); //register the custom row behavior BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior; gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo)); gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new MyBehavior()); this.radGridView1.KeyDown += this.RadGridView1_KeyDown; } private void RadGridView1_KeyDown(object sender, KeyEventArgs e) { }

classMyBehavior : GridDataRowBehavior { protected override bool ProcessEscapeKey(KeyEventArgs keys) { if (this.GridViewElement.IsInEditMode) { this.GridViewElement.CancelEdit(); this.GridViewElement.GridControl.CallOnKeyDown(keys); returntrue; } return false; } }

Offtopic, I would like to note that threads are handled according to license and time of posting, so if it is an urgent problem, we suggest you to use a support ticket, which would be handled before a forum thread. Thank you for your understanding. 

I hope this helps. If you have other questions please let me know.

Regards,
Nadya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Dreyfus
Top achievements
Rank 1
Veteran
answered on 24 Jun 2020, 01:02 PM

Thank you for the response Nadya.

But based on my initial post the problem is actually about the KeyDown event of the form, not the grid.

If the grid is not in edit mode and I press Esc key, Form_KeyDown event is triggered as it should be and as expected because Form.KeyPreview is set to True.

However, this form event is not triggered if I press Esc key while a cell is in edit mode. I hope I made myself clear.

0
Nadya | Tech Support Engineer
Telerik team
answered on 26 Jun 2020, 03:10 PM

Hello Jay,

When RadGridView is in edit mode the Esc key is handled by it. If you do not want to use the grid KeyDown event, the other possible solution is to handle the KeyDown event of the hosted TextBox control within the editor:

private void RadGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{           
    var editor = e.ActiveEditor as RadTextBoxEditor;
    if (editor != null)
    {
        RadTextBoxEditorElement element = editor.EditorElement as RadTextBoxEditorElement;
       
        TextBox tb = (TextBox)element.TextBoxItem.HostedControl;
        tb.PreviewKeyDown -= this.Tb_PreviewKeyDown;
        tb.PreviewKeyDown += this.Tb_PreviewKeyDown;
    }
}
private void Tb_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    
}

This will trigger the Tb_PreviewKeyDown event. In order to trigger the RadForm_KeyDown event you can use the SendKeys class in the Tb_PreviewKeyDown event. In SendKeys.Send method, you can pass the Escape key as a parameter.

I hope this information helps. Let me know if there is anything else I can help with.

Regards,
Nadya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Dreyfus
Top achievements
Rank 1
Veteran
answered on 27 Jun 2020, 01:37 AM

Hi Nadya.

The problem with this solution is that it disregards the purpose of the KeyPreview property of the Form.

The KeyDown event of the Form must be triggered FIRST before the KeyDown/PreviewKeyDown of the grid. This should be the proper sequence.

 

0
Dreyfus
Top achievements
Rank 1
Veteran
answered on 27 Jun 2020, 01:50 AM
Please see attached image to see visual representation of the issue.
0
Nadya | Tech Support Engineer
Telerik team
answered on 01 Jul 2020, 03:54 PM

Hi, Dreyfus,

I understand what you expect to happen and the sequence the events should be triggered. But, note that RadGridView has its specific behavior for handling keys and events. When the cell is in edit mode and the Esc key is pressed it is expected that the grid handles the key and exit edit mode. And this is by design.

However, if you need to handle the Form_KeyDown event you can create a custom RadGridView and override the ProcessDialogKey method:

 public class MyRadGridView : RadGridView
 {
     protected override bool ProcessDialogKey(Keys keyData)
     {
         if (!this.IsDisposed && (IsInEditMode || !GridViewElement.StandardTab) &&
             (keyData == Keys.Tab || keyData == (Keys.Tab | Keys.Shift)))
         {
             KeyEventArgs e = new KeyEventArgs(keyData);
             
             if (this.GridBehavior != null && this.GridBehavior.ProcessKey(e))
             {
                 return true;
             }
         }
         if (this.IsInEditMode && keyData == Keys.Escape)
         {
             return false;
         }

         if (this.IsInEditMode && keyData == Keys.Enter)
         {
             return false;
         }

         return base.ProcessDialogKey(keyData);
     }

     public override string ThemeClassName
     {
         get { return typeof(RadGridView).FullName; }
         set { base.ThemeClassName = value; }
     }
 }

I hope this helps.

Regards,
Nadya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
GridView
Asked by
Dreyfus
Top achievements
Rank 1
Veteran
Answers by
Dreyfus
Top achievements
Rank 1
Veteran
Nadya | Tech Support Engineer
Telerik team
Share this question
or