tôi có phương pháp sau đây:Điều khiển của tôi không được xử lý đúng cách?
public static void Disposer(Control.ControlCollection controls)
{
foreach (Control c in controls)
{
if (c.HasChildren)
Disposer(c.Controls);
if ((c is UltraNumericEditor)
|| (c is UltraComboEditor)
|| (c is UltraTextEditor)
|| (c is UltraDateTimeEditor)
|| (c is UltraCheckEditor)
|| (c is UltraGrid)
|| (c is UltraStatusBar))
{
c.Dispose();
var x = c.IsDiposed; // this is true!
}
}
}
Sau đó, tôi gọi nó với điều này:
[STAThread]
static void Main()
{
Test forma = new Test();
forma.Controls.Add(new UltraStatusBar());
forma.Controls.Add(new UltraStatusBar());
forma.Controls.Add(new UltraNumericEditor());
forma.Controls.Add(new UltraComboEditor());
forma.Controls.Add(new UltraTextEditor());
forma.Controls.Add(new UltraTextEditor());
forma.Controls.Add(new UltraDateTimeEditor());
forma.Controls.Add(new UltraTimeZoneEditor());
forma.Controls.Add(new UltraGrid());
Disposer(forma.Controls);
foreach (Control control in forma.Controls)
{
if (control.IsDisposed)//this is false now!
{
Debug.Write(control.Name + " was disposed");
}
}
}
Câu hỏi của tôi là, tại sao bên phương pháp tiêu hủy của tôi điều khiển của tôi đang trở nên vứt bỏ, nhưng khi tôi kiểm tra trong phương pháp chính là trở về sai ?? Bằng cách này, đối tượng "Thử nghiệm" chỉ là một lớp đơn giản kế thừa từ UserControl Cảm ơn, liên quan.
Mã của bạn thậm chí sẽ không biên dịch. Lớp 'System.Windows.Forms.Control' không lộ ra phương thức' Dispose() '(mặc dù nó hiện ra' IsDisposed'). –
@EdS bạn có chắc chắn không? –
@Ed Erm có nó http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dispose.aspx –