Results 1 to 5 of 5

Thread: prevent form closing when form is closing

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    prevent form closing when form is closing

    I am all about questions today, dont know why!

    I am wondering, when the user clicks on the red X button to close the application, the form closing event is raised.

    I am wondering if there is a way to prevent the application from being closed when the user presses the cross button?

    When my application is doing something and during this process the user wants to close the app - a dialog box tells the user that the application is currently doing a process and asks if they still wanna exit - if the answer is no then I want the application to still be up and running and not exit when the answer is "no"

    Currently I've set the e.Cancel = false, which works but then the application form appears to be disposed/closed but i dont want this

    Code:
    DialogResult confirmExit = MessageBox.Show("It appears that there is a recording in progress, are you sure you wish to exit the application?", "Confirm exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
    
                if (confirmExit == DialogResult.No)
                {
                    e.Cancel = false;
                }
                else
                {//Whatever....}
    Last edited by Techno; Feb 1st, 2006 at 12:24 PM.

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: prevent form closing when form is closing

    e.Cancel is the way to go. How many forms do you have? I use this method with my old app (BinaryEdit and it works great.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: prevent form closing when form is closing

    I only have 1 main form - thats it.

    Sure, the File > Exit thing works fine as I call Application.Exit(); but when the user clicks on the red cross, the formclosing() event is raised by default in .NET and I can catch that but the application runs in the background but the form has been closed already, I want the form to still be there

    is there a way to do this?

  4. #4
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: prevent form closing when form is closing

    I didn't realize you have Canceled = false before.

    If you want to cancel the closing, you make Cancel true otherwise you don't want to cancel (hence the false) and it just closes.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: prevent form closing when form is closing

    awesome! Thanks... that worked
    for some reason I thought when cancel = true then it means "yeh, close the app...we wanna go home" and if cancel = false then "no wait, we still wanna live a bit longer"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width