Clear controls does not dispose them - what is the risk?

前端 未结 3 1429
遇见更好的自我
遇见更好的自我 2020-11-29 11:15

There are multiple threads(a, b, c etc.) about the fact that Clear() ing items in the .NET component containers does not Dispose them(by ca

3条回答
  •  半阙折子戏
    2020-11-29 12:04

    Asking for modifications like this is pointless, the Windows Forms team has been disbanded quite a while ago. It is in maintenance mode, only security issues and OS incompatibilities are considered.

    It is otherwise simple enough to create your own method to do this:

      public static class ExtensionMethods {
        public static void Clear(this Control.ControlCollection controls, bool dispose) {
          for (int ix = controls.Count - 1; ix >= 0; --ix) {
            if (dispose) controls[ix].Dispose();
            else controls.RemoveAt(ix);
          }
        }
      }
    

    Now you can write:

      panel1.Controls.Clear(true);
    

提交回复
热议问题