开发者

When do I initiate and dispose my UserControls?

In 开发者_开发知识库a winforms application I have an options form. The form has a treview control and a panel control. Depending on the users choice in the treeview I want to load/add a usercontrol to the panel control. When should I be creating/initiating the usercontrols? On the form load eventhandler or once a treeview node is selected? And should I be disposing of the usercontrols in the from closing eventhandler?

This is my code:

public partial class Options : Form
{
    //usercontrols
    Connections _connections;
    Notifications _notifications;
    Proxy _proxy;

    private void Options_Load(object sender, EventArgs e)
    {
        treeViewOptions.ExpandAll();

        _connections = new Connections();
        _notifications = new Notifications();
        _proxy = new Proxy();
    }

    private void treeViewOptions_AfterSelect(object sender, TreeViewEventArgs e)
    {
        switch (treeViewOptions.SelectedNode.Name)
        {

            case "NodeConnection":
                ControlPanel.Controls.Clear();
                ControlPanel.Controls.Add(_connections);
                break;
            case "NodeNotifications":
                ControlPanel.Controls.Clear();
                ControlPanel.Controls.Add(_notifications);
                break;
            case "NodeProxy":
                ControlPanel.Controls.Clear();
                ControlPanel.Controls.Add(_proxy);
                break;
        }
    }
}

Thanks


Yes, you need to fix this. Right now you are leaking the user controls instances, they won't get disposed automatically. Nor does their finalizer take care of the job. After a while, your program will crash when it has consumed 10,000 window handles.

Make it look similar to this:

private void treeViewOptions_AfterSelect(object sender, TreeViewEventArgs e)
{
    foreach (Control ctl in ControlPanel.Controls) ctl.Dispose();
    ControlPanel.Controls.Clear();

    switch (treeViewOptions.SelectedNode.Name)
    {

        case "NodeConnection":
            ControlPanel.Controls.Add(_connections);
            break;
        case "NodeNotifications":
            ControlPanel.Controls.Add(_notifications);
            break;
        case "NodeProxy":
            ControlPanel.Controls.Add(_proxy);
            break;
    }
}


Solution:

public partial class Options : Form
{
    //usercontrols
    Connections _connections;
    Notifications _notifications;
    Proxy _proxy;

private void Options_Load(object sender, EventArgs e)
{
    treeViewOptions.ExpandAll();

    _connections = new Connections();
    _notifications = new Notifications();
    _proxy = new Proxy();
}

private void treeViewOptions_AfterSelect(object sender, TreeViewEventArgs e)
{
    ControlPanel.Controls.Clear();

    switch (treeViewOptions.SelectedNode.Name)
    {

        case "NodeConnection":

            ControlPanel.Controls.Add(_connections);
            break;
        case "NodeNotifications":

            ControlPanel.Controls.Add(_notifications);
            break;
        case "NodeProxy":

            ControlPanel.Controls.Add(_proxy);
            break;
    }
}

   private void Options_FormClosing(object sender, FormClosingEventArgs e)
    {

        _connections.Dispose();          
        _notifications.Dispose();
        _proxy.Dispose();

    }

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜