Trigger form key events from editor

8 posts, 0 answers
  1. Dreyfus
    Dreyfus avatar
    17 posts
    Member since:
    May 2019

    Posted 19 Jun Link to this post

    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.

  2. Dreyfus
    Dreyfus avatar
    17 posts
    Member since:
    May 2019

    Posted 23 Jun in reply to Dreyfus Link to this post

    It would be nice to have some update on this.
  3. Nadya
    Admin
    Nadya avatar
    379 posts

    Posted 23 Jun Link to this post

    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.
  4. Dreyfus
    Dreyfus avatar
    17 posts
    Member since:
    May 2019

    Posted 24 Jun in reply to Nadya Link to this post

    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.

  5. Nadya
    Admin
    Nadya avatar
    379 posts

    Posted 26 Jun Link to this post

    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.
  6. Dreyfus
    Dreyfus avatar
    17 posts
    Member since:
    May 2019

    Posted 26 Jun in reply to Nadya Link to this post

    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.

     

  7. Dreyfus
    Dreyfus avatar
    17 posts
    Member since:
    May 2019

    Posted 26 Jun in reply to Dreyfus Link to this post

    Please see attached image to see visual representation of the issue.
  8. Nadya
    Admin
    Nadya avatar
    379 posts

    Posted 01 Jul Link to this post

    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.
Back to Top