我需要他们处理后除去控制? [英] Need I remove controls after disposing them?

查看:76
本文介绍了我需要他们处理后除去控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 2

// dynamic textbox adding
myTextBox = new TextBox();
this.Controls.Add(myTextBox);

// ... some code, finally

// dynamic textbox removing
myTextBox.Dispose();

// this.Controls.Remove(myTextBox); ?? is this needed



一点解释




  • 当然,如果我的的Dispose 的控制,我不会再看到它,但无论如何,将继续留在父母一个无控件集合?

  • 需要我也一样,MSDN建议,从控制删除所有处理程序?

  • Little explanation

    • Surely, if I Dispose a control I will not see it anymore, but anyway, will remain a "Nothing" in the parent controls collection?
    • need I also, like MSDN recommends, remove all handlers from the control?
    • 推荐答案

      没有,你不知道。结果
      我试了一下

      No, you don't.
      I tried it.

      您可以将下面的代码粘贴到 LINQPad

      You can paste the following code into LINQPad:

      var form = new Form();
      var b = new Button();
      form.Controls.Add(b);
      b.Click += delegate { b.Dispose(); };
      Application.Run(form);
      

      修改:控制的将会的被删除从表单的控制集合。为了证明这一点,用下面的替换click处理程序:

      EDIT: The control will be removed from the form's Controls collection. To demonstrate this, replace the click handler with the following:

      b.Click += delegate { b.Dispose(); MessageBox.Show(form.Controls.Count.ToString());};
      



      它会显示 0

      2 第二修改 Control.Dispose(布尔处置)包含下面的代码:

      2nd EDIT: Control.Dispose(bool disposing) contains the following code:

                      if (parent != null) { 
                          parent.Controls.Remove(this); 
                      }
      

      这篇关于我需要他们处理后除去控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆