Avoiding validation when closing a window...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom

    #1

    Avoiding validation when closing a window...

    How is the best way to avoid validation when closing a
    window? For instance, I have a Windows Forms window which
    has a validation event for a text box. However, if one
    enters invalid data in then and then attempts to close the
    window (either via my custom 'Close' box or by clicking the
    close 'X' in the upper right window corner), the validation
    event still triggers and it tells the user that they have
    invalid data. Which of course means they need to clear that
    data out then close the window.

    Sure, I could trap the close event (both in my custom CLOSE
    box and also via WndProc), set a form level 'Closing'
    variable and check that in my validate code, but there has
    GOT to be an easier way. Why does Windows insist on firing
    the validate event when a window is closing?

    Is there no easier way to avoid the Validate event when
    closing? Thanks.

    Tom
    --

  • Larry Lard

    #2
    Re: Avoiding validation when closing a window...


    Tom wrote:[color=blue]
    > How is the best way to avoid validation when closing a
    > window? For instance, I have a Windows Forms window which
    > has a validation event for a text box. However, if one
    > enters invalid data in then and then attempts to close the
    > window (either via my custom 'Close' box or by clicking the
    > close 'X' in the upper right window corner), the validation
    > event still triggers and it tells the user that they have
    > invalid data. Which of course means they need to clear that
    > data out then close the window.
    >
    > Sure, I could trap the close event (both in my custom CLOSE
    > box and also via WndProc), set a form level 'Closing'
    > variable and check that in my validate code, but there has
    > GOT to be an easier way. Why does Windows insist on firing
    > the validate event when a window is closing?
    >
    > Is there no easier way to avoid the Validate event when
    > closing? Thanks.[/color]
    [color=blue]
    >From the help:[/color]

    Closing The Form and Overriding Validation
    A side effect of the control maintaining focus when data is invalid is
    that it is impossible to close the parent form in any of the usual ways
    you close a form:

    * Clicking the Close box
    * Via the System menu that appears when you right-click the title bar
    * Calling the Close method programmaticall y
    However, in some cases, you might want to allow the user to close the
    form regardless of whether the values in the controls are valid. You
    can override validation and close a form that still contains invalid
    data by creating a handler for the form's Closing event. In the event,
    set the Cancel property to False. This forces the form to close.

    Note If you force the form to close in this way, any information in
    the controls that has not already been saved is lost.
    Note Modal forms do not validate the contents of controls when
    closed. You can still use control validation to lock focus to a
    control, but you do not need to be concerned with the behavior with
    regard to closing the form.

    --
    Larry Lard
    Replies to group please

    Comment

    • Tom

      #3
      Re: Avoiding validation when closing a window...

      Larry: Thanks for the info... This works, yet it doesn't
      work. Yes, it does cause the form the end even with invalid
      data in a field; however the validation routine still runs
      so you still get an error message (then the form closes).
      Again, although this works it is still ugly. I would rather
      just have it close the window and NOT give any error
      routine.

      Too bad there isn't any way to actually look to see if a
      window is closing from the validation routine.

      Tom


      Larry Lard wrote:
      [color=blue]
      >
      > Tom wrote:[color=green]
      > > How is the best way to avoid validation when closing a
      > > window? For instance, I have a Windows Forms window
      > > which has a validation event for a text box. However,
      > > if one enters invalid data in then and then attempts to
      > > close the window (either via my custom 'Close' box or
      > > by clicking the close 'X' in the upper right window
      > > corner), the validation event still triggers and it
      > > tells the user that they have invalid data. Which of
      > > course means they need to clear that data out then
      > > close the window.
      > >
      > > Sure, I could trap the close event (both in my custom
      > > CLOSE box and also via WndProc), set a form level
      > > 'Closing' variable and check that in my validate code,
      > > but there has GOT to be an easier way. Why does Windows
      > > insist on firing the validate event when a window is
      > > closing?
      > >
      > > Is there no easier way to avoid the Validate event when
      > > closing? Thanks.[/color]
      >[color=green]
      > > From the help:[/color]
      >
      > Closing The Form and Overriding Validation
      > A side effect of the control maintaining focus when data
      > is invalid is that it is impossible to close the parent
      > form in any of the usual ways you close a form:
      >
      > * Clicking the Close box
      > * Via the System menu that appears when you right-click
      > the title bar * Calling the Close method programmaticall y
      > However, in some cases, you might want to allow the user
      > to close the form regardless of whether the values in the
      > controls are valid. You can override validation and close
      > a form that still contains invalid data by creating a
      > handler for the form's Closing event. In the event, set
      > the Cancel property to False. This forces the form to
      > close.
      >
      > Note If you force the form to close in this way, any
      > information in the controls that has not already been
      > saved is lost. Note Modal forms do not validate the
      > contents of controls when closed. You can still use
      > control validation to lock focus to a control, but you do
      > not need to be concerned with the behavior with regard to
      > closing the form.[/color]



      --

      Comment

      Working...