English 中文(简体)
妥善处置/披露用户控制
原标题:Properly disposing/closing an user control

我有一个像2003年《展望》那样的视窗表屏幕,在表格左侧有一只菜单,并有一个小组,以表格的右侧。

My left menu contains items. Upon clicking on those items, I want to load the appropriate user control (acting as "sub-forms") on the right-hand side panel. I have managed to do that and it is working well.

不过,我担心的是,我的用户控制是否得到妥善处置,或者当用户点击另一个菜单项目时,他们是否仍然记忆犹新(因此,给另一个用户控制/代谢)。

该法典与此类似:

    private void ultraExplorerBar1_ItemClick(object sender, Infragistics.Win.UltraWinExplorerBar.ItemEventArgs e)
    {
        panel1.Controls.Clear();

        if (e.Item.Key == "Action")
        {
            ActionUserControl subForm = new ActionUserControl();
            panel1.Controls.Add(subForm);
            subForm.Show();                
        }
        else if (e.Item.Key == "Options")
        {
            OptionsUserControl subForm = new OptionsUserControl();
            panel1.Controls.Add(subForm);
            subForm.Show();
        }
    }

我无法确定小组1。 控制 这种方法一开始,只需一米,用户控制就足够了,无法在适当时间收集垃圾?

根据我的理解,在申请中,一旦从小组获得许可,就不应提及它,但我不敢肯定,我是否应当做其他事情,或用一个合理的轻重工具来确认,我的分局是按应该处置的,它不会造成某种记忆/资源泄露?

最佳回答

在打电话panel1.Controls.Clear()之前使用这一通道,并确保用户控制中的<代码>Dispose()方法反过来处置任何可支配的资源。

foreach (Control control in panel1.Controls)
{
   control.Dispose();
}
问题回答

每个休息室都不做工作。 由于每个要素和指数都做了调整。

你们需要的是,每次都把最高控制放在首位。

In the following code that I use, I have a panel with user controls in it. I go into the first control and dispose of its controls then dispose of the control itself.

在处置所有控制之后,可以明确重新制定指数。

While pnlMain.Controls.Count > 0

    While pnlMain.Controls(0).Controls.Count > 0
        pnlMain.Controls(0).Controls(0).Dispose()
    End While

    pnlMain.Controls(0).Dispose()
End While
pnlMain.Controls.Clear()

我以动态内容这样做,即把这种方法添加到我的基页类别:

/// <summary>
/// Dispose a control and all its children
/// </summary>
public void DisposeControl(Control c)
{
    if (null != c)
        using (c)
            DisposeChildControlsOf(c);
}

/// <summary>
/// Dispose (and remove) all the children of a control
/// </summary>
public void DisposeChildControlsOf(Control c)
{
    if (null != c)
        if (null != c.Controls)
            while (c.Controls.Count > 0)
            {
                Control child = c.Controls[0];
                c.Controls.RemoveAt(0);
                DisposeControl(child);
            }
}

这种做法也令人厌恶。 那么,它就这样说(如果你不拥有控制权):

DisposeChildControlsOf(panel1);

DisposeControl(myDynamicallyGeneratedControl);




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签