![]() |
|
|
#1 |
|
Newbie
Join Date: May 2009
Age: 25
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Rep Power: 0
![]() OS:
|
C# Form Closing Event
So I want to perform an event whenever the user closes the program. Everything that I'm finding on the net is showing me that the following method should be included:
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // do whatever... } I currently have a call to a method there (a writefile method named 'writeCourseList()), but it doesn't perform it every time. I added a menuItem (labeled 'Quit') and specified in that event handler that the write method should be carried out before application.exit(), and that works perfectly. But if I click on the red X or press ALT+F4, it doesn't call the writefile method, just closes. I've found forum posts that say the best way to add this FormClosing method is to go to the properties box and double click on Events, but I don't see an Events anything in the properties box (I'm using VS08). How do I get my writefile method to execute upon form closing? The above isn't doing it. |
|
|
|
|
|
#2 |
|
ƒ(ψ)=ΘΊΧφ
![]() |
Re: C# Form Closing Event
Change the MinimizeBox property of the Form to False and see if the FormClosing event get's fired.
__________________
Please don't click here |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2009
Age: 25
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Rep Power: 0
![]() OS:
|
Re: C# Form Closing Event
No go. BTW I typed the formclosing event by hand, if that may have done anything (I didn't click on anything to add it, so I'm not sure if there's a prototype that needs to be added somewhere).
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { writeCourseList(); } is all I wrote in there. I know that writeCourseList() is functional because I use it elsewhere, it's just not getting called when the form is closing. *Edit* For 'object sender' I also tried 'Object sender', as that's what it shows on msdn website. Still no go. Last edited by Smilodon; 03-06-2009 at 09:59 PM.. Reason: Added text |
|
|
|
|
|
#4 |
|
ƒ(ψ)=ΘΊΧφ
![]() |
Re: C# Form Closing Event
FormClosing even gets fired correctly for me.
Code:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Closing", "Closing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: May 2009
Age: 25
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Rep Power: 0
![]() OS:
|
Re: C# Form Closing Event
Am I putting the method in the wrong place? It's inside of the Form1 class. I just copied/pasted exactly that into the class over what I had before, and no message box.
*Edit* I even copied that into a whole new project (blank, except for that), and got nothing. Last edited by Smilodon; 04-06-2009 at 04:01 AM.. Reason: Added text |
|
|
|
|
|
#6 |
|
Junior Member (25+)
Join Date: Jun 2009
Location: Mesquite, NV (about 75 mi from Las Vegas)
Age: 15
Posts: 25
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 0
![]() OS:
|
Re: C# Form Closing Event
do you have a button for it if so (i will be using button1 as an example) this might help this is what i do:
private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Close(); } it works for everything i use it for...then again i typically use toolstrips and menus... btw if it does indeed work for you mind giving me a kudos/rep |
|
|
|
| Thanked Users: | Smilodon (04-06-2009) |
|
|
#7 |
|
Newbie
Join Date: May 2009
Age: 25
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Rep Power: 0
![]() OS:
|
Re: C# Form Closing Event
I already have an exitToolStripMenuItem; as I mentioned earlier, the writefile function does work, as I call it elsewhere. That is the place where I call it and it works fine.
What I'm trying to do is get the program to save the current work no matter how it is closed (Alt+F4, Red X, File menu -> Exit, etc). I would even be happy with writing a different method for each exit reason. So my File -> Exit works fine, but I can't get the program to save when Alt+F4 or the Red X are pressed. I'll give you a kudos anyway for recommending the one thing that has worked for me. |
|
|
|
|
|
#8 |
|
Junior Member (25+)
Join Date: Jun 2009
Location: Mesquite, NV (about 75 mi from Las Vegas)
Age: 15
Posts: 25
Thanks: 0
Thanked 2 Times in 2 Posts
Rep Power: 0
![]() OS:
|
Re: C# Form Closing Event
sorry bro I'm trying to figure out the whole Key thing for my C# too. If I figure it out first I'll let you know how.
|
|
|
|
|
|
#9 | |
|
ƒ(ψ)=ΘΊΧφ
![]() |
Re: C# Form Closing Event
Quote:
try overriding the OnFormClosing method and see if it get's called. Code:
protected void OnFormClosing(FormClosingEventArgs e)
{
MessageBox.Show("Closing", "Closing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Btw .. I am testing in Windows 7 RC x64 with VS 2008 SP1 |
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: May 2009
Age: 25
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Rep Power: 0
![]() OS:
|
Re: C# Form Closing Event
Some methods aren't finished, not all buttons, etc work yet, but the program compiles. Both Form1_Closing and OnFormClosing are exactly as you wrote them, but I get no message box on closing, unless I go to File -> Quit (but that's a different process).
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |