Overriding a 'Leave' event

We have certain fields on forms designated with a 'Leave' event so that in once the user leaves the field the field is validated.

In the event that the field is deemed to be in error focus is returned to the field and the user then re-enters the data.

I have a problem in that the user wishes to leave the form by way of an Exit button. Clicking on the button when a field is in error though generates a 'leave' event for the incorrect field even though there is a 'Click' event attached to the Exit button which says 'Get Out of form' and we go round in a circle with the user continually being presented with the field in error in focus instead of the 'Exit button' event being done.

How do I overide one event with another so that the Exit button 'Click' event supercedes the 'Leave' event of the data field on the form? 

  • Verified Answer

    Take a look at the KB article here as I believe it applies to your situation.

    The demo in the article doesn't use the Leave event but uses the Validating event instead as the textbox has its CausesValidation property set to true.

    Thanks.

  • Hi Chris,

    I've cut & pasted the code into a new Windows Form app, created the correct 'validating event' and my code reads like so...

          class-id Override.Form1 is partial

                    inherits type System.Windows.Forms.Form.

          working-storage section.

          method-id NEW.

          procedure division.

              invoke self::InitializeComponent

              invoke textBox1::Focus

              goback.

          end method.

          method-id IsFormClosing public.

          local-storage section.

          78 WMCLOSE value "WmClose".

          01 stackTrace type System.Diagnostics.StackTrace.

          01 stframes type System.Diagnostics.StackFrame occurs any.

          01 MethodName type System.Reflection.MethodBase.

          procedure division returning FormIsClosing as condition-value.

             set FormIsClosing to false

             set stackTrace to new System.Diagnostics.StackTrace

             set stframes to stackTrace::GetFrames

             perform varying sf as type System.Diagnostics.StackFrame thru stframes

                set MethodName to sf::GetMethod

                if MethodName::Name = WMCLOSE

                   set FormIsClosing to true

                   exit perform

                end-if

             end-perform

             goback.

          end method.

          method-id textBox1_Validating final private.

          procedure division using by value sender as object e as type System.ComponentModel.CancelEventArgs.

               if self::IsFormClosing = false

                     if textBox1::Text = "Yes" or "No"

                        continue

                     else

                        invoke type MessageBox::Show("Must be Yes or No")

                        set e::Cancel to true

                     end-if

                  end-if.

          end method.

          method-id buttonCancel_Click final private.

          procedure division using by value sender as object e as type System.EventArgs.

              invoke self::Close

          end method.

          end class.

    I think you'll find its EXACTLY the same as the proposed solution.

    Doesn't seem to work.

    I still get the message box appearing DESPITE me pressing the Cancel button.

    Can you check this please?

  • Hi Chris,

    I've cut & pasted the code into a new Windows Form app, created the correct 'validating event' and my code reads like so...

          class-id Override.Form1 is partial

                    inherits type System.Windows.Forms.Form.

          working-storage section.

          method-id NEW.

          procedure division.

              invoke self::InitializeComponent

              invoke textBox1::Focus

              goback.

          end method.

          method-id IsFormClosing public.

          local-storage section.

          78 WMCLOSE value "WmClose".

          01 stackTrace type System.Diagnostics.StackTrace.

          01 stframes type System.Diagnostics.StackFrame occurs any.

          01 MethodName type System.Reflection.MethodBase.

          procedure division returning FormIsClosing as condition-value.

             set FormIsClosing to false

             set stackTrace to new System.Diagnostics.StackTrace

             set stframes to stackTrace::GetFrames

             perform varying sf as type System.Diagnostics.StackFrame thru stframes

                set MethodName to sf::GetMethod

                if MethodName::Name = WMCLOSE

                   set FormIsClosing to true

                   exit perform

                end-if

             end-perform

             goback.

          end method.

          method-id textBox1_Validating final private.

          procedure division using by value sender as object e as type System.ComponentModel.CancelEventArgs.

               if self::IsFormClosing = false

                     if textBox1::Text = "Yes" or "No"

                        continue

                     else

                        invoke type MessageBox::Show("Must be Yes or No")

                        set e::Cancel to true

                     end-if

                  end-if.

          end method.

          method-id buttonCancel_Click final private.

          procedure division using by value sender as object e as type System.EventArgs.

              invoke self::Close

          end method.

          end class.

    I think you'll find its EXACTLY the same as the proposed solution.

    Doesn't seem to work.

    I still get the message box appearing DESPITE me pressing the Cancel button.

    Can you check this please?

  • There is an attachment on the article which contains a full solution and project zipped up.

    Please download this and step through it with the debugger and compare the code execution path with yours.

    Perhaps one of the properties for the controls has not been set properly?

  • Sorted.

    The 'Exit Button' on screen should be set to CausesValidation=False in the button properties!

    Easy when you know how!!