CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    341

    Force Windows Form to stay Open even if attempted to be Closed?

    I am trying to cancel Close form option for the user. For example when user clicks on “X” close the form I want the to give user Message that the form can not be closed
    and keep it open. I tried to catch Form.Closing event and I inputted message there but I am not sure how to force it stay open.

    Can you please help

    Thanks
    "We act as though comfort and luxury were the chief requirements of
    life, when all that we need to make us happy is something to be
    enthusiastic about."

    - Einstein

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Force Windows Form to stay Open even if attempted to be Closed?

    If you don't want to close the form, then in your Closing event write
    Code:
    //This will force the form to stay open
    e.Cancel=True

  3. #3
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    341

    Re: Force Windows Form to stay Open even if attempted to be Closed?

    Thanks A LOT. I have done it once but forgot. I new it was so easy but could not remember.

    Thanks!!!
    "We act as though comfort and luxury were the chief requirements of
    life, when all that we need to make us happy is something to be
    enthusiastic about."

    - Einstein

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Force Windows Form to stay Open even if attempted to be Closed?

    Quote Originally Posted by stardv
    Thanks A LOT. I have done it once but forgot. I new it was so easy but could not remember.

    Thanks!!!
    I am glad it helped.

  5. #5
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Force Windows Form to stay Open even if attempted to be Closed?

    If you specifically want to prevent the user from closing the form using the Close button on the title bar then I suggest you disable it. I don't know why so many people seem to want to distinguish between that and other methods of closing a form, by I've written a helper class that you can use to disable the Close button and also to make a form immovable. It edits the system menu to achieve this, but that is all hidden. You simply create an instance of the class in your form with the appropriate constructor and you're done.

    SystemMenuManager class
    Last edited by jmcilhinney; September 30th, 2005 at 05:14 PM. Reason: Fix incorrect link

  6. #6
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Force Windows Form to stay Open even if attempted to be Closed?

    jmcilhinney, your helper class sounds interesting, but the link you gave states:
    Edit:
    Attachment removed. See post #1.
    So, is the class at some other url? Is there a C# version?

  7. #7
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Force Windows Form to stay Open even if attempted to be Closed?

    Sorry, my mistake. Here's a link to the entire thread. The class file is attached, as per that other link, to post #1.

    SystemMenuManager class

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

Featured