Handling the Escape key within the CellType.GeneralEditor

Posted by: j2associates on 8 September 2017, 1:28 pm EST

  • Posted 8 September 2017, 1:28 pm EST

    Hello all,


    I have derived a custom NumberCellType where warning dialogs are displayed under certain circumstances. The problem I have encountered is that the warning dialogs are displaying even when the user presses the Escape key. I tried trapping the Escape key in a WithEvents class level GeneralEditor but it doesn't handle the Escape key. Searching the forums talks about changing input maps but all of the examples I saw pertained to code to put in the spread. I need the code to be in the editor. For simplicities sake, I would prefer not having a reference to the parent sheet within my custom cell type. Thanks in advance for any ideas and/or suggestions!

  • Replied 8 September 2017, 1:28 pm EST

    Hello,


    By default, the WhenAncestorOfFocus input map binds the Escape key to the CancelEditing action.  The WhenAncestorOfFocus input map is used to implement the spread control's ProcessDialogKey method which occurs both when the spread control has focus and when the cell editor control has focus. Thus the editor will not get the key stroke. You would need to either add a map to the WhenAncestorOfFocus to the SpreadActions.None action. Or you can inherit your own Spread control and override the ProcessDialogKey method. There is nothing you can do in the CellType code to catch this keystroke until you stop the key from processing in the Spread's preprocessing of the keystroke.

  • Replied 8 September 2017, 1:28 pm EST

    j2 -

    I believe the Escape key is similiar to the Delete key.  I will forward this on to a developer to see if he has some insight.

  • Replied 8 September 2017, 1:28 pm EST

    Hello BobM,


    The KeyUp and KeyDown events both trap the Delete key. The Escape key does not fire either KeyUp or KeyDown so they are different in that regard.

  • Replied 8 September 2017, 1:28 pm EST

    j2 -

    Scott is out next week so I have forwarded this to a developer to see if he has some insight.  I believe the Escape key is consummed by the spread so it may be that you have to override the ProcessDialogKey but I want to make sure.

  • Replied 8 September 2017, 1:28 pm EST

    Hello BobM,


    I didn't see ProcessDialogKey available in the OverRides list for the NumberCellType. Would this instead be an override in the GeneralEditor? I'm willing to try some things later this weekend if I get a chance. Thanks for all of your help!

  • Replied 8 September 2017, 1:28 pm EST

    Thanks for the explanation ScottS! Perhaps I should have explained more fully what I was trying to do. What is happening is that my warning dialog is displaying twice if the user hits the Escape key while in EditMode. Are there any other work-arounds that I could use to stop the warning dialog from displaying twice.
  • Replied 8 September 2017, 1:28 pm EST

    Hello,


    The following code prevents my warning dialog from displaying twice when the user presses the Escape key while in EditMode with a NumberCellType. Am I missing anything here or are there any side effects of which I should be aware? Thanks in advance for any ideas and/or suggestions!


    NOTE: I have not been able to get this code to work reliably! See later post for stable code to accomplish this.


    A. Define class level variable


        Private m_EscapeKeyPressed As Boolean


    B. Set variable in CancelEditing override


        Public Overrides Sub CancelEditing()
            m_EscapeKeyPressed = True
            MyBase.CancelEditing()
        End Sub 'CancelEditing


    C. Check value in Editor Validating event code before setting Cancel


        Private Sub EditorValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
            Try
                ' Skip processing the Escape key.
                If m_EscapeKeyPressed Then
                    m_EscapeKeyPressed = False
                    Return
                End If


                e.Cancel = MyBooleanFunction
            Catch ex As Exception
                Throw
            End Try
        End Sub 'm_Editor_Validating

  • Replied 8 September 2017, 1:28 pm EST

    j2 -

    I don't see any issues with this code that should cause problems.

  • Replied 8 September 2017, 1:28 pm EST

    Hello all,


    Here is stable code that will allow the CellType GeneralEditor to detect the Escape key. Enjoy...


    VB


    A. Define class level variables


        Private WithEvents m_ParentSpread As FpSpread = Nothing


        Private m_EscapeKeyPressed As Boolean


    B. Set up parent spread so we can use its events to trap the Escape key


        Public Overrides Function GetEditorControl(ByVal parent As System.Windows.Forms.Control, ByVal appearance As FarPoint.Win.Spread.Appearance, ByVal zoomFactor As Single) As System.Windows.Forms.Control
            ' Set up parent spread so its events can be used to trap the Escape key.
            m_ParentSpread = CType(parent, FpSpread)
            Return MyBase.GetEditorControl(parent, appearance, zoomFactor)
        End Function


    C. Set variable in parent spread DialogKey event


        Private Sub m_ParentSpread_DialogKey(ByVal sender As Object, ByVal e As FarPoint.Win.Spread.DialogKeyEventArgs) Handles m_ParentSpread.DialogKey
            m_EscapeKeyPressed = (e.KeyCode = Keys.Escape)
        End Sub


    D. Check value in EditorValidating event code before setting Cancel


        Private Sub EditorValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
            Try
                ' Skip processing the Escape key.
                If m_EscapeKeyPressed Then
                    m_EscapeKeyPressed = False
                    Return
                End If


                e.Cancel = MyBooleanFunction
            Catch ex As Exception
                Throw
            End Try
        End Sub


    E. Clear parent spread when editing is completed so its events do not continue to fire


        Public Overrides Function StopEditing() As Boolean
            m_ParentSpread = Nothing
            Return MyBase.StopEditing()
        End Function


    C#


    A. Define class level variables


        private FpSpread m_ParentSpread = null;


        private bool m_EscapeKeyPressed;


    B. Set up parent spread so we can use its events to trap the Escape key


        public override System.Windows.Forms.Control GetEditorControl(System.Windows.Forms.Control parent, FarPoint.Win.Spread.Appearance appearance, float zoomFactor) {
            // Set up parent spread so its events can be used to trap the Escape key.
            m_ParentSpread = (FpSpread)parent;
            return base.GetEditorControl(parent, appearance, zoomFactor);
        }


    C. Set variable in parent spread DialogKey event


        private void m_ParentSpread_DialogKey(object sender, FarPoint.Win.Spread.DialogKeyEventArgs e) {
            m_EscapeKeyPressed = (e.KeyCode == Keys.Escape);
        }


    D. Check value in EditorValidating event code before setting Cancel


        private void EditorValidating(object sender, System.ComponentModel.CancelEventArgs e) {
            try {
                // Skip processing the Escape key.
                if (m_EscapeKeyPressed) {
                    m_EscapeKeyPressed = false;
                    return;
                }

           
                e.Cancel = MyBooleanFunction;
            }
            catch (Exception ex) {
                throw;
            }
        }


    E. Clear parent spread when editing is completed so its events do not continue to fire


        public override bool StopEditing() {
            m_ParentSpread = null;
            return base.StopEditing();
        }

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels