tags:

views:

775

answers:

3

Hey! I am not trying to push my luck here but I have another c# question. I have tried every possible event I found using google. Here is the code:

 private void Form1_OnClose()
        {
            MessageBox.Show("I was closed -2");
        }

        private void Form1_Exit(object sender, EventArgs evArgs)
        {
            MessageBox.Show("I was closed -1");         
        }
        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBox.Show("I was closed 0");
        }     

        private void Form1_Closed(object sender, EventArgs e)
        {
            MessageBox.Show("I was closed 1");
        }
        private void Form1_FormClosed(Object sender, FormClosedEventArgs e)
        {

            MessageBox.Show("I was closed 2");
        }

Not one of these trigger anything when I either do Alt+f4 or click on the X button. What am I doing wrong here?

+8  A: 

You might be missing actual subscription code, which is something along these lines:

this.Closing += Form1_Closing;

Instead, try overriding OnXXX methods - this is the preferred way of doing things.

Anton Gogolev
But I also have an on load event and that occurs without any thing else, just the event.
AntonioCS
The Load event must have been wired up by the designer. Open up "xxx.Designer.cs" and look for "this.Load += Form_Load;"
Anton Gogolev
I believe double-clicking on the form adds this.Load += new EventHandler(Form1_Load); or something similar to the designer file. I was confused about this at first when I started c#. There's nothing special about the name Form1_Closed or anything, it just happened to be the name the designer chose
Davy8
When it created the event, and what makes it worse is that it hides it in the designer file, which if you don't know about partial classes can be misleading. The designer file and the .cs file you're editing are partial classes, meaning that your class is actually the combination of both files.
Davy8
For events other than the default, if you create the event from the Properties pane, VS will wire the events for you. Otherwise, you have to do it yourself.
R. Bemrose
Yes yes I have read the InitializeComponent code and it's there.But I am just manually trying to add the on close event and when I try to do: this.OnFormClosed = this.Form1_Closed;I get this:Error 1 Cannot assign to 'OnFormClosed' because it is a 'method group'
AntonioCS
For that you need to use the += operator instead of =
CodeMonkey1
I see no this.Closed in the little intellisense box
AntonioCS
this.FormClosed = this.Form1_Closed;
Arjan Einbu
I did the this.Closing += this.Form1_Closed; and it worked :) Don't really see anything in the intellisense. Anyone know a msdn page I can read?Thanks
AntonioCS
A: 

Are these methods actually assigned as event handlers? Go to design mode, select the form, then click the little lightning bolt above the properties window. Then find the event you want (Closing probably) and double click it.

CodeMonkey1
There is no Closing event nor is there an exit event :(
AntonioCS
You probably don't have the form itself selected.
CodeMonkey1
Wait wait I see a FormClosed and also a FormClosing :)
AntonioCS
There you go, sorry I got the name wrong.
CodeMonkey1
+2  A: 

The error is likely that you aren't wiring the events at the right time. Check your program.cs file. It should look something like this:

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace Test
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Form form = new Form2();
            form.Closing += form_Closing;
            Application.Run(form);
        }
        private static void form_Closing(object sender, CancelEventArgs e)
        {
            MessageBox.Show("Closing");
        }
    }
}

I just ran this and the event fired.

Chris Holmes