CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2010
    Posts
    139

    how to override close (X) button in a Windows Form

    Hi all,

    I was just wondering if there is a way to determine whether a button has been clicked within a closingform event or within another button_click event? For example, I have an app that has one form with three buttons and a propertygrid, one button is called 'Load', it decrypts and then loads data from a xml file which then populates the propertygrid and the other button called 'Save', saves the changes a user makes to the grid and encrypts the xml file, lastly i have a close button. My problem is this, if a user loads the xml (which gets decrypted) but then decides to close the application before clicking the save button (which encypts the xml) the xml file will be left decrypted.

    If the user presses the (x) button or the close button to close the application, is there a way within those events i can say if load button_click has been clicked - encrypt file and then close, else just close?

    private void LicenseFile_FormClosing(object sender, FormClosingEventArgs e)

    {

    pseudo code

    if BtnLoad_click is pressed

    Encrypt xml file

    close app

    else

    close app

    }

    I would be grateful if someone could help me, thanks in advance.

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: how to override close (X) button in a Windows Form

    What if the user clicks the 'Load' button and then the (X) button of the form? Wouldn't the XML still be left decrypted?

    Also, it seems like poor design to modify the actual file. In stead, I think you should decrypt it into a temporary file or into memory and display that to the user. Then, when the user hits 'Save', simply overwrite the old file.
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: how to override close (X) button in a Windows Form

    Your 'FormClosingEventArgs' contain the answer:

    Code:
    private void LicenseFile_FormClosing(object sender, FormClosingEventArgs e)
    {
         if (e.CloseReason == CloseReason.UserClosing)
        {
            e.Cancel = true; // or false if you want to continue closing
            
            // your code
        }
    }
    I agree with foamy that you should not modify the original file until it is actually saved though.

    Hope it helps =)

  4. #4
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: how to override close (X) button in a Windows Form

    BTW, be careful with the e.Cancel and conditions to set it to true, since the 'FormClosing' event will also be called when you hit your 'Exit' button, and your form might never close. And again, keep in mind that the event will be called, so if you do stuff in your 'Exit' button callback, check that you don't do them again when you close the form.

  5. #5
    Join Date
    Nov 2010
    Posts
    139

    Re: how to override close (X) button in a Windows Form

    Quote Originally Posted by foamy View Post
    In stead, I think you should decrypt it into a temporary file or into memory and display that to the user. Then, when the user hits 'Save', simply overwrite the old file.
    the above sounds good, how would i go about implementing that? the xml file will be saved in a database and i also have a schema. how would i decrypt it into memory? then override the old file? I am using the Cryptography.Xml namespace to encrypt and decrypt a xml element in my file with Asymmetric Keys.
    Last edited by katy_price; October 28th, 2011 at 08:20 PM.

  6. #6
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: how to override close (X) button in a Windows Form

    Quote Originally Posted by katy_price View Post
    the above sounds good, how would i go about implementing that? the xml file will be saved in a database and i also have a schema. how would i decrypt it into memory? then override the old file? I am using the Cryptography.Xml namespace to encrypt and decrypt a xml element in my file with Asymmetric Keys.
    Well, I would simply read the file (however you want) into a variable and then decrypt the data from there. This would leave the source file unchanged until you overwrite it.
    It's not a bug, it's a feature!

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