Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. form closing event not going to method

form closing event not going to method

Scheduled Pinned Locked Moved C#
com
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MichCl
    wrote on last edited by
    #1

    I'm trying to add an event handler for form closing event. I'm not very good at doing event handlers. I looked at http://stackoverflow.com/questions/7076751/how-do-i-avoid-validating-cancel-causing-app-exit-to-hang[^], among other places on the internet, but the thread is not getting to my method. It wasn't building until I added the property for FormClosing at the top. Hopefully someone here has an idea why it's not working. Thanks!

    public partial class GenericPC : UserControl
    {
    ...
    public FormClosingEventHandler Closing { get; set; }

    public Control GetPC() //this gets called
    {
    ...
    this.Closing += new System.Windows.Forms.FormClosingEventHandler(this.updateLogsOnClose);
    ...
    }

    private void updateLogsOnClose(object sender, System.ComponentModel.CancelEventArgs e)
    {
    //thread doesn't go here when form closes
    logCountsToFile(logCountFile);
    }
    ...
    }

    P L P 3 Replies Last reply
    0
    • M MichCl

      I'm trying to add an event handler for form closing event. I'm not very good at doing event handlers. I looked at http://stackoverflow.com/questions/7076751/how-do-i-avoid-validating-cancel-causing-app-exit-to-hang[^], among other places on the internet, but the thread is not getting to my method. It wasn't building until I added the property for FormClosing at the top. Hopefully someone here has an idea why it's not working. Thanks!

      public partial class GenericPC : UserControl
      {
      ...
      public FormClosingEventHandler Closing { get; set; }

      public Control GetPC() //this gets called
      {
      ...
      this.Closing += new System.Windows.Forms.FormClosingEventHandler(this.updateLogsOnClose);
      ...
      }

      private void updateLogsOnClose(object sender, System.ComponentModel.CancelEventArgs e)
      {
      //thread doesn't go here when form closes
      logCountsToFile(logCountFile);
      }
      ...
      }

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      The event name is Closing.

      M 1 Reply Last reply
      0
      • P PIEBALDconsult

        The event name is Closing.

        M Offline
        M Offline
        MichCl
        wrote on last edited by
        #3

        I changed it from FormClosing to Closing, as I changed in my code shown above, and it's still not going to my method when the form closes. Any ideas? It doesn't seem the like property for Closing gets called by anything. Do I need to set it somehow? Closing was causing a build error before I defined it. Maybe there's a system method I need to enable somewhere to get it defined by visual studio?

        N P 2 Replies Last reply
        0
        • M MichCl

          I changed it from FormClosing to Closing, as I changed in my code shown above, and it's still not going to my method when the form closes. Any ideas? It doesn't seem the like property for Closing gets called by anything. Do I need to set it somehow? Closing was causing a build error before I defined it. Maybe there's a system method I need to enable somewhere to get it defined by visual studio?

          N Offline
          N Offline
          Nicholas Marty
          wrote on last edited by
          #4

          I'm not sure if an UserControl has a Closing event. Are you sure that you're in the right place with this code (should probably go into the CodeBehind of the Form ;) )

          1 Reply Last reply
          0
          • M MichCl

            I'm trying to add an event handler for form closing event. I'm not very good at doing event handlers. I looked at http://stackoverflow.com/questions/7076751/how-do-i-avoid-validating-cancel-causing-app-exit-to-hang[^], among other places on the internet, but the thread is not getting to my method. It wasn't building until I added the property for FormClosing at the top. Hopefully someone here has an idea why it's not working. Thanks!

            public partial class GenericPC : UserControl
            {
            ...
            public FormClosingEventHandler Closing { get; set; }

            public Control GetPC() //this gets called
            {
            ...
            this.Closing += new System.Windows.Forms.FormClosingEventHandler(this.updateLogsOnClose);
            ...
            }

            private void updateLogsOnClose(object sender, System.ComponentModel.CancelEventArgs e)
            {
            //thread doesn't go here when form closes
            logCountsToFile(logCountFile);
            }
            ...
            }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            public Control GetPC() //this gets called
            {
            ...
            this.Closing += new System.Windows.Forms.FormClosingEventHandler(this.updateLogsOnClose);
            ...
            }

            That's not the form, and your usercontrol will not raise a FromClosingEvent; you'd need a reference to the form if you want to hook it's "Closing" handler (in the same way you need an instance when you use one of it's properties). Alternatively, you could use the "Parent" property of your usercontrol to find the owning form.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            M 1 Reply Last reply
            0
            • L Lost User

              public Control GetPC() //this gets called
              {
              ...
              this.Closing += new System.Windows.Forms.FormClosingEventHandler(this.updateLogsOnClose);
              ...
              }

              That's not the form, and your usercontrol will not raise a FromClosingEvent; you'd need a reference to the form if you want to hook it's "Closing" handler (in the same way you need an instance when you use one of it's properties). Alternatively, you could use the "Parent" property of your usercontrol to find the owning form.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              M Offline
              M Offline
              MichCl
              wrote on last edited by
              #6

              Thanks Eddy. I'm trying to figure out how to use theParent's Form to set me up with the Closing Handler. For some reason, the below code is not building. It says "System.Windows.Forms.FormClosingEventHandler is a type which is not valid in the given context". Any idea?

              public partial class GenericPC : UserControl
              {
              ...
              public FormClosingEventHandler Closing { get; set; }

              public Control GetPC(, Form theParent) //added form here
              {
              ...
              theParent.Closing += new System.Windows.Forms.FormClosingEventHandler(this.updateLogsOnClose); //change here, build issue
              ...
              }

              private void updateLogsOnClose(object sender, System.ComponentModel.CancelEventArgs e)
              {
              //thread doesn't go here when form closes
              logCountsToFile(logCountFile);
              }
              ...
              }

              L 1 Reply Last reply
              0
              • M MichCl

                I changed it from FormClosing to Closing, as I changed in my code shown above, and it's still not going to my method when the form closes. Any ideas? It doesn't seem the like property for Closing gets called by anything. Do I need to set it somehow? Closing was causing a build error before I defined it. Maybe there's a system method I need to enable somewhere to get it defined by visual studio?

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                MichCl wrote:

                property for Closing

                Get rid of that.

                1 Reply Last reply
                0
                • M MichCl

                  Thanks Eddy. I'm trying to figure out how to use theParent's Form to set me up with the Closing Handler. For some reason, the below code is not building. It says "System.Windows.Forms.FormClosingEventHandler is a type which is not valid in the given context". Any idea?

                  public partial class GenericPC : UserControl
                  {
                  ...
                  public FormClosingEventHandler Closing { get; set; }

                  public Control GetPC(, Form theParent) //added form here
                  {
                  ...
                  theParent.Closing += new System.Windows.Forms.FormClosingEventHandler(this.updateLogsOnClose); //change here, build issue
                  ...
                  }

                  private void updateLogsOnClose(object sender, System.ComponentModel.CancelEventArgs e)
                  {
                  //thread doesn't go here when form closes
                  logCountsToFile(logCountFile);
                  }
                  ...
                  }

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Your modification is correct; but it looks like the eventhandler could use a different signature;

                  private void updateLogsOnClose(Object sender, FormClosingEventArgs e)

                  As an alternative solution;

                  using System;
                  using System.Windows.Forms;
                  using System.Collections.Generic;

                  namespace test
                  {
                  class MyControl: UserControl
                  {
                  protected override void OnHandleCreated(EventArgs e)
                  {
                  base.OnHandleCreated(e);
                  FindForm().FormClosing += new FormClosingEventHandler(MyControl_FormClosing);
                  }
                  void MyControl_FormClosing(object sender, FormClosingEventArgs e)
                  {
                  System.Diagnostics.Debugger.Break();
                  }
                  }
                  class Program
                  {
                  public static void Main(string[] args)
                  {
                  using (var f = new Form())
                  {
                  MyControl uc = new MyControl() { Dock = DockStyle.Fill };
                  f.Controls.Add(uc);
                  f.ShowDialog();
                  }
                  }
                  }
                  }

                  The reason I'm not calling the FindForm method in the constructor of the UserControl is because it will not be assigned to a form at that point. (It's created on line 25, and added to the form on the next line)

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  1 Reply Last reply
                  0
                  • M MichCl

                    I'm trying to add an event handler for form closing event. I'm not very good at doing event handlers. I looked at http://stackoverflow.com/questions/7076751/how-do-i-avoid-validating-cancel-causing-app-exit-to-hang[^], among other places on the internet, but the thread is not getting to my method. It wasn't building until I added the property for FormClosing at the top. Hopefully someone here has an idea why it's not working. Thanks!

                    public partial class GenericPC : UserControl
                    {
                    ...
                    public FormClosingEventHandler Closing { get; set; }

                    public Control GetPC() //this gets called
                    {
                    ...
                    this.Closing += new System.Windows.Forms.FormClosingEventHandler(this.updateLogsOnClose);
                    ...
                    }

                    private void updateLogsOnClose(object sender, System.ComponentModel.CancelEventArgs e)
                    {
                    //thread doesn't go here when form closes
                    logCountsToFile(logCountFile);
                    }
                    ...
                    }

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    If you definitely need access to the closing event from your control, you need to walk up the Parent hierarchy until you find the Form. It would look something like this:

                    private Form ParentForm(object item)
                    {
                    if (((Form)item).Parent is Form)
                    return ((Form)item).Parent;
                    return ParentForm(item);
                    }

                    Then, it's a simple matter of hooking into the closing event handler. I really wouldn't recommend doing it this way, but you could.

                    I was brought up to respect my elders. I don't respect many people nowadays.
                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                    M 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      If you definitely need access to the closing event from your control, you need to walk up the Parent hierarchy until you find the Form. It would look something like this:

                      private Form ParentForm(object item)
                      {
                      if (((Form)item).Parent is Form)
                      return ((Form)item).Parent;
                      return ParentForm(item);
                      }

                      Then, it's a simple matter of hooking into the closing event handler. I really wouldn't recommend doing it this way, but you could.

                      I was brought up to respect my elders. I don't respect many people nowadays.
                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                      M Offline
                      M Offline
                      MichCl
                      wrote on last edited by
                      #10

                      I got it working!! Thanks for your help!! :) It turns out I was missing "new":

                      theParent.FormClosing += new FormClosingEventHandler(this.updateLogsOnClose);

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups