Are my controls not positioned correctly?

I have the following method:

    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!
            }

        }
    }

Then I called it the following:

   [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");
            }
        }
    }

My question is why, inside my Disposer method, my controls are deleted, but when I check the main method, returns false ?? By the way, the "Test" object is just a class that inherits from UserControl Thank you, hi.

+5
source share
1 answer

There is an error in the Control.ControlCollection class. There is no code to throw an InvalidOperationException that is raised by other collection classes when the collection changes in the foreach loop.

, Dispose(), Controls. , .

, , :

while (controls.Count > 0) controls[0].Dispose();

for(), . , Winforms . , CreateControl() . , Show(). Controls.Remove() Controls.Clear(), .

+6

All Articles