TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Software World > Programming

Notices

C# Form Closing Event

Programming


Reply
 
Thread Tools Display Modes
Old 03-06-2009, 10:52 AM   #1
Newbie
 
Join Date: May 2009
Age: 25
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Rep Power: 0 Smilodon is an unknown quantity at this point


OS: Windows XP Windows Vista


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.
Smilodon is offline   Reply With Quote
Old 03-06-2009, 07:29 PM   #2
ƒ(ψ)=ΘΊΧφ
 
bakuryu's Avatar
 
Join Date: May 2006
Location: India
Age: 23
Posts: 6,621
Thanks: 19
Thanked 645 Times in 603 Posts
Rep Power: 87 bakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant future


OS: Windows XP Windows Vista Windows 7


Send a message via Yahoo to bakuryu
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
bakuryu is offline   Reply With Quote
Old 03-06-2009, 09:57 PM   #3
Newbie
 
Join Date: May 2009
Age: 25
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Rep Power: 0 Smilodon is an unknown quantity at this point


OS: Windows XP Windows Vista


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
Smilodon is offline   Reply With Quote
Old 03-06-2009, 11:26 PM   #4
ƒ(ψ)=ΘΊΧφ
 
bakuryu's Avatar
 
Join Date: May 2006
Location: India
Age: 23
Posts: 6,621
Thanks: 19
Thanked 645 Times in 603 Posts
Rep Power: 87 bakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant future


OS: Windows XP Windows Vista Windows 7


Send a message via Yahoo to bakuryu
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); 
        }
got the message box as expected.
bakuryu is offline   Reply With Quote
Old 04-06-2009, 03:58 AM   #5
Newbie
 
Join Date: May 2009
Age: 25
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Rep Power: 0 Smilodon is an unknown quantity at this point


OS: Windows XP Windows Vista


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
Smilodon is offline   Reply With Quote
Old 04-06-2009, 06:07 AM   #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 AdvancedSoftware is an unknown quantity at this point


OS: Windows XP


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
AdvancedSoftware is offline   Reply With Quote
Thanked Users:
Smilodon (04-06-2009)
Old 04-06-2009, 09:53 AM   #7
Newbie
 
Join Date: May 2009
Age: 25
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Rep Power: 0 Smilodon is an unknown quantity at this point


OS: Windows XP Windows Vista


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.
Smilodon is offline   Reply With Quote
Old 04-06-2009, 10:41 AM   #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 AdvancedSoftware is an unknown quantity at this point


OS: Windows XP


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.
AdvancedSoftware is offline   Reply With Quote
Old 04-06-2009, 06:27 PM   #9
ƒ(ψ)=ΘΊΧφ
 
bakuryu's Avatar
 
Join Date: May 2006
Location: India
Age: 23
Posts: 6,621
Thanks: 19
Thanked 645 Times in 603 Posts
Rep Power: 87 bakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant futurebakuryu has a brilliant future


OS: Windows XP Windows Vista Windows 7


Send a message via Yahoo to bakuryu
Re: C# Form Closing Event

Quote:
Am I putting the method in the wrong place? It's inside of the Form1 class.
it has to be inside the the Form1 class.

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); 

}
also if it still doesn't work, attach the entire projects folder.
Btw .. I am testing in Windows 7 RC x64 with VS 2008 SP1
bakuryu is offline   Reply With Quote
Old 05-06-2009, 05:50 AM   #10
Newbie
 
Join Date: May 2009
Age: 25
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Rep Power: 0 Smilodon is an unknown quantity at this point


OS: Windows XP Windows Vista


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).
Attached Files
File Type: zip Grade Tracker.zip (230.9 KB, 5 views)
Smilodon is offline   Reply With Quote
Reply

Thread Tools
Display Modes



< Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 09:49 PM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional