Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Exiting application stopped by validation?

Exiting application stopped by validation?

Scheduled Pinned Locked Moved .NET (Core and Framework)
question
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    So, Im writing a Forms application, an I am using the Validating event on some TextBoxes (setting e.Cancel = true if bad input). It works just fine until I click the red x in order to exit the application. If a textbox with faulty text has the focus then, the Validating event runs and makes the application not exit. What can I do about it?

    D CHill60C 2 Replies Last reply
    0
    • T TMattC

      So, Im writing a Forms application, an I am using the Validating event on some TextBoxes (setting e.Cancel = true if bad input). It works just fine until I click the red x in order to exit the application. If a textbox with faulty text has the focus then, the Validating event runs and makes the application not exit. What can I do about it?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Set a flag in the Closing event and check the flag for being set in the Validating events.

      A guide to posting questions on CodeProject

      Click this: Asking questions is a skill. Seriously, do it.
      Dave Kreskowiak

      T 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Set a flag in the Closing event and check the flag for being set in the Validating events.

        A guide to posting questions on CodeProject

        Click this: Asking questions is a skill. Seriously, do it.
        Dave Kreskowiak

        T Offline
        T Offline
        TMattC
        wrote on last edited by
        #3

        Doesnt work. The Validating event on the TextBox sets off before the FormClosing event.

        1 Reply Last reply
        0
        • T TMattC

          So, Im writing a Forms application, an I am using the Validating event on some TextBoxes (setting e.Cancel = true if bad input). It works just fine until I click the red x in order to exit the application. If a textbox with faulty text has the focus then, the Validating event runs and makes the application not exit. What can I do about it?

          CHill60C Offline
          CHill60C Offline
          CHill60
          wrote on last edited by
          #4

          I wouldn't normally do this, but I published a tip about this ... Allow Form to close when invalid data is present[^] which might help

          T 1 Reply Last reply
          0
          • CHill60C CHill60

            I wouldn't normally do this, but I published a tip about this ... Allow Form to close when invalid data is present[^] which might help

            T Offline
            T Offline
            TMattC
            wrote on last edited by
            #5

            Thanks, that article is exactly what I experience. Though the trick with the FormClosing event didnt really do it for me. The application does exit, but not until my MessageBox is sprung from the Validating event.

            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
            e.Cancel = false;
            }

            What did work the way I intended (the application exiting without any validating), was the WndProc override.

            private const int WM_CLOSE = 0x0010;
            ...
            protected override void WndProc(ref Message m)
            {
            if (m.Msg == WM_CLOSE) // Attempting to close Form
            AutoValidate = AutoValidate.Disable; //this stops (all) validations

                base.WndProc(ref m);    //call the base method to handle other messages
            

            }

            Great work!

            CHill60C 1 Reply Last reply
            0
            • T TMattC

              Thanks, that article is exactly what I experience. Though the trick with the FormClosing event didnt really do it for me. The application does exit, but not until my MessageBox is sprung from the Validating event.

              private void Form1_FormClosing(object sender, FormClosingEventArgs e)
              {
              e.Cancel = false;
              }

              What did work the way I intended (the application exiting without any validating), was the WndProc override.

              private const int WM_CLOSE = 0x0010;
              ...
              protected override void WndProc(ref Message m)
              {
              if (m.Msg == WM_CLOSE) // Attempting to close Form
              AutoValidate = AutoValidate.Disable; //this stops (all) validations

                  base.WndProc(ref m);    //call the base method to handle other messages
              

              }

              Great work!

              CHill60C Offline
              CHill60C Offline
              CHill60
              wrote on last edited by
              #6

              I'm glad it worked!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups