3

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:

Here is a list of all events available in the designer

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
  • Possible duplicate of [How to know user has clicked "X" or the "Close" button?](https://stackoverflow.com/questions/2683679/how-to-know-user-has-clicked-x-or-the-close-button) – waka Sep 20 '17 at 14:22
  • 4
    Possible duplicate of [What event signals that a UserControl is being destroyed?](https://stackoverflow.com/questions/12474566/what-event-signals-that-a-usercontrol-is-being-destroyed) – Sinatr Sep 20 '17 at 14:26
  • 2
    You can "close" a user control, its lifetime is determined by the Form that it is hosted on. It raises the Disposed event when it gets destroyed. – Hans Passant Sep 20 '17 at 14:30
  • 1
    @waka: As I said, Form.Closing is not available. – Al Lelopath Sep 20 '17 at 14:37
  • OnParenVisibilityChanged can be also used. – melya Sep 20 '17 at 14:45

1 Answers1

3
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;
    }
}
melya
  • 578
  • 5
  • 24
  • 1
    In my case, `FindForm()` returns null. – Al Lelopath Sep 20 '17 at 15:09
  • 1
    If the control is not (or not yet) added to a form this function will return null. In the constructor of a control this.FindForm () will return null. Use an event like OnCreateControl to check on which form the control is shown. – melya Sep 20 '17 at 15:11
  • 1
    override OnVisibleChanged event handler and there you can get the owner form. – melya Sep 20 '17 at 15:23
  • @AlLelopath A UserControl can use `this.ParentForm` to reference the form that is hosting it. – LarsTech Sep 20 '17 at 15:36
  • This is really close. In `_owner_FormClosing` (or `_owner_FormClosed`), after I do what I need to do, what super method do I have to call to close `SomeControl`? – Al Lelopath Sep 20 '17 at 15:48
  • @AlLelopath What are you doing? A control doesn't close. – LarsTech Sep 20 '17 at 15:52
  • When not handling these events myself, clicking on the X closes the control. Now I have the code above, but replacing the `throw` with a `System.Diagnostics.Trace.WriteLine` for the moment and the control does not close, i.e., is not displayed anymore. – Al Lelopath Sep 20 '17 at 15:54
  • Oops, that should be "i.e., is still displayed." – Al Lelopath Sep 20 '17 at 16:17
  • see the _owner_FormClosing handler in answer. I edited it again. – melya Sep 20 '17 at 16:24
  • I'm not clears on that code. Could you add it to your answer? – Al Lelopath Sep 20 '17 at 16:35
  • I'm only using `_owner_FormClosing`, no need for both. Something is still not right. I don't know why but `_owner_FormClosing` is getting called 6 times when clicking the X. There are 20+ components on the control. So I've added an `if (_owner != null)` to guard on the multiple calls. Now the behavior is that `_owner_FormClosing` gets called and the control is *still* displayed. Then on the second click of X, it is not displayed, because the event handler has been removed and it is calling whatever needs to be called to remove it. – Al Lelopath Sep 20 '17 at 17:19
  • How many UserControls you have on your form? it is not so easy to understand what is going on without code. – melya Sep 21 '17 at 08:44