It is a common requirement when using User Controls to detect that the Parent Form or main form is closing.
Presently, I am working on a distributed smart client application that is full of user controls. Most, if not all of these user controls will be getting data by calling a Windows Communication Foundation service (A good tutorial on WCF is available in an earlier blog post here).
When you instantiate a Service Reference client object, it is imperative that you call client.Close() to free up any resources when the control is closed. As there is no FormClosing event in a winforms UserControl, you will need to override the ParentForm.FormClosing event. In this example I have a windows form with a user control on it. When you close the form the event in the user control is raised.
Note: Visual basic example is below
C#
using System.Windows.Forms;
namespace UserControlClosingEvent
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
protected override void OnCreateControl()
{
base.OnCreateControl();
this.ParentForm.FormClosing += new FormClosingEventHandler(ParentForm_FormClosing);
}
void ParentForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Would you like to close the parent form?", "Close parent form?",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
}
}
}
}
Visual Basic
Public Class UserControl1
Protected Overloads Overrides Sub OnCreateControl()
MyBase.OnCreateControl()
AddHandler Me.ParentForm.FormClosing, AddressOf ParentForm_FormClosing
End Sub
Private Sub ParentForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
If MessageBox.Show("Would you like to close the parent form?", "Close parent form?", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
e.Cancel = True
End If
End Sub
End Class
You can then call the client.Close() (or whatever action you desire) instead of MessageBox.Show() in ParentForm_FormClosing event
This solution has zero scalability and does not solve the problem of a user control closing…
What if the user control is in another container, in yet another container etc…what if its buried down 5 or more levels?
What if your application uses dockable windows that can be loaded up and destroyed during the lifetime of the application?
The only thing this code does is tell the user control the main outer app window is shutting down…and one can simply call a method in the user control from the form closing event to achieve the same result…
Sorry bud
This does not inform the user control that IT IS closing…
Try again
SW
i’d have to agree with the original commentator. but i don’t think i would be a dick about it.
Yep the 1st post is right.
it worked perfectly for me! and my control is buried within many levels of containers? Sure if you’re talking about a dock control you may need something a little different, but I don’t think this blog post deserved the slamming that he did.
I agree with Dave, as this solves 95% of this problems’ contexts.
Could scott propose a better way, please ?
Charly
excellent idea! creative and works perfecto!
actually i implemented it in WPF, but the idea was enlightening!
I works absolutely perfect! 5 starts from me!
Pingback: User Control Communication Wpf | Alinaideis
Just perfect. Precisely what I needed.