Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who owns controls?

Let's say I have some component like this:

class SomeForm : Form
{
    private Control example;

    public void Stuff()
    {
        this.example = new ComboBox();
        // ...
        this.Controls.Add(example);
    }

    public void OtherStuff()
    {
        this.Controls.Remove(example);
    }
}

Who is responsible for calling Dispose on the example control? Does removing it from this.Controls cause it to be disposed? Or does this leak bunches of window handles backing the controls?

(For reference, I'm asking this because I don't see where the Windows Forms Designer generates code to call Dispose on a Form's children)

like image 521
Billy ONeal Avatar asked Mar 01 '13 21:03

Billy ONeal


1 Answers

Form.Dispose() will dispose of the controls within the Controls collection. So removing the control from Controls will require you to dispose of the control yourself.

like image 107
D Stanley Avatar answered Sep 21 '22 16:09

D Stanley