unload form/dialog

lopband
lopband used Ask the Experts™
on
Hello,
I am a newbie to C# and Visual Studio..and have a question ;))

Double clicking a form you automatically go into the load() event (alternatively using the forms constructor), now i want to fire a few commands as a person closes the form or dialog... so is there anything as an unload() method, something that fires just before the form / dialog closes?

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Use Closing event of Form (from tab at Properties window).
Retired
Distinguished Expert 2017
Commented:
It is the Closing event it has the following signature :

private void FormName_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   // ....
}
Sorry, event is called FormClosing. You can find it in "Bahavior" events group.
Or you can subscribe to this event mannually, e.g. inside constructor or Form_Load event handler.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Author

Commented:
Thanks guys!

A bit puzzled as to who to award this to so am splitting it, because although i got the first correct answer i had no explanation of if, then second answer gave me explanation but based on the first..
Ah man, where have you seen Closing event on Form?
Look:
    public partial class Form1 : Form
    {     
        public Form1()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        }      

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
    }

Open in new window

Author

Commented:
What am i doing wrong guys?


namespace RyanSplashScreen
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("fired!");
        }

Open in new window

Author

Commented:
ok, forgot to add this:
this.FormClosing += new FormClosingEventHandler(SettingsForm_FormClosing);

it works!!!

Thanks!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial