Catching the close event on a c# form

后端 未结 3 1084
借酒劲吻你
借酒劲吻你 2021-01-21 20:55

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         


        
相关标签:
3条回答
  • 2021-01-21 21:36

    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.

    0 讨论(0)
  • 2021-01-21 21:52

    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.

    0 讨论(0)
  • 2021-01-21 21:54

    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.

    0 讨论(0)
提交回复
热议问题