Code/C#

[C#] Detect UserControl's Closing

Hide Code 2009. 1. 7. 19:36

using System.Windows.Forms;

namespace DetectUserControlClosing
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.ParentForm.FormClosing += new FormClosingEventHandler(ParentForm_FormClosing);
        }

        private void ParentForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult myResult = MessageBox.Show("Would you like the parent form to close?",
                "Allow Close?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (myResult == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
    }
}