-
October 23rd, 2011, 03:48 PM
#1
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.
-
October 24th, 2011, 02:25 AM
#2
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!
-
October 24th, 2011, 07:57 AM
#3
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 =)
-
October 24th, 2011, 08:01 AM
#4
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.
-
October 28th, 2011, 07:47 PM
#5
Re: how to override close (X) button in a Windows Form
Originally Posted by foamy
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.
-
October 31st, 2011, 03:06 AM
#6
Re: how to override close (X) button in a Windows Form
Originally Posted by katy_price
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|