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
  1. Home
  2. General Programming
  3. C#
  4. trying to prevent user from exiting the application sometimes

trying to prevent user from exiting the application sometimes

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 1 Posters 0 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    There are times in my application that I want to prevent the user from exiting the application window with the exit button (the X button in the upper right hand corner of most window applications). I want in some circumstances to print out a message box and to let the user know that the application is in a state that cannot exit the application and to keep it running. The following code catches that application exiting from clicking on the exit button, and prints out the message box. But how can I stop the application from continuing with the exit ? Actually, by the time the message box is printed, the application window is already closed. I am fairly new at C #, any help is appreciated. static void Main(string [] args) { Application.ApplicationExit += new EventHandler(ApplicationExitEventHandler); Application.Run(new CsMain()); } static void ApplicationExitEventHandler(Object sender, System.EventArgs e) { if (state != CLOSED_STATE) { MessageBox.Show("Error ! \n\nThe line is still open. You must close the \nline before terminating the application", "CsTdrv", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }

    L 1 Reply Last reply
    0
    • L Lost User

      There are times in my application that I want to prevent the user from exiting the application window with the exit button (the X button in the upper right hand corner of most window applications). I want in some circumstances to print out a message box and to let the user know that the application is in a state that cannot exit the application and to keep it running. The following code catches that application exiting from clicking on the exit button, and prints out the message box. But how can I stop the application from continuing with the exit ? Actually, by the time the message box is printed, the application window is already closed. I am fairly new at C #, any help is appreciated. static void Main(string [] args) { Application.ApplicationExit += new EventHandler(ApplicationExitEventHandler); Application.Run(new CsMain()); } static void ApplicationExitEventHandler(Object sender, System.EventArgs e) { if (state != CLOSED_STATE) { MessageBox.Show("Error ! \n\nThe line is still open. You must close the \nline before terminating the application", "CsTdrv", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi, Here I am assuming you are inheriting from the System.Windows.Forms.Form class. In the class add an EventHandler to the Closing Event e.g. Form.Closing += new CancelEventHandler(MyClose); Then create the code in here, you can cancel the event using the CancelEventArgs e.g. MyClose(object sender, CancelEventArgs) { ....... if(shouldNotClose) e.Cancel = true; } Hope this helps, Andy

      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