form closing event not going to method
-
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);
}
...
} -
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);
}
...
}The event name is
Closing
. -
The event name is
Closing
.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?
-
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?
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 ;) )
-
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);
}
...
}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[^]
-
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[^]
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);
}
...
} -
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?
MichCl wrote:
property for Closing
Get rid of that.
-
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);
}
...
}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[^]
-
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);
}
...
}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 -
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