Reputation: 6778
I have a class that inherits from UserControl
:
public partial class MyView : System.Windows.Forms.UserControl
I want to handle the event that occurs when the user clicks on the X in the upper right corner. Maybe it is Form.Closing? But I don't see that as an option in the designer. What event is it?
Edit:
Upvotes: 6
Views: 9742
Reputation: 596
class SomeControl : UserControl
{
Form _owner;
public SomeControl()
{
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (Visible)
{
_owner = FindForm();
//_owner = ParentForm;
_owner.FormClosing += _owner_FormClosing;
_owner.FormClosed += _owner_FormClosed;
}
}
private void _owner_FormClosed(object sender, FormClosedEventArgs e)
{
throw new NotImplementedException();
}
private void _owner_FormClosing(object sender, FormClosingEventArgs e)
{
Hide();
_owner.FormClosing -= _owner_FormClosing;
_owner.FormClosed -= _owner_FormClosed;
Parent.Controls.Remove(this);
_owner = null;
}
}
Upvotes: 4