User Control Closing Event

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


One Response to User Control Closing Event

  1. scott says:

    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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>