【问题标题】:C# removing a control from a parent container and ListC# 从父容器和列表中删除控件
【发布时间】:2016-03-23 08:50:01
【问题描述】:

我有 2 个控件继承 UserControl 一个是容器,另一个是基本文本框标签等的集合。特此标记为ContainerControlComboControl

ContainerControl 包含一个List<ComboControl> 和一个将它们添加到FlowLayoutPanel 的foreach 循环。 ComboControl 有一个按钮,我想用它来将自己从其父级列表中清除。

我不确定最好的方法是什么。 this.parent 并转换为 ContainerControl 还是 Dispose() 工作?我相当确定我可以将 ref 传递给 List,但这听起来不必要的混乱......

 public partial class ContainerControl : UserControl
 {
    List<ComboControl> ComboControls = new List<ComboControl>();
    ...
    //procederaly generate and fill ComboControls here
    ...
    foreach (ComboControl value in ComboControls)
    {
        this.flowLayoutTable.Controls.Add(value);
    }
 ...
 }

 public partial class ComboControl : UserControl
 {
     private void BtnDel_Click(object sender, EventArgs e)
    {
        //what goes here
    }
 ...
 }

【问题讨论】:

  • 您必须发布一些代码,以便有人可以帮助您。
  • ok 放置了一些模型代码。
  • this.flowLayoutTable.Controls.Clear()ComboControls.Clear() 应该足够了。但是,如果有其他对组合控件的引用(例如向它们注册的事件),您也需要清理它,否则可能会出现内存泄漏。
  • 好吧,这会不会也将它们从列表中清除?

标签: c# forms winforms user-controls


【解决方案1】:

按照Zohar Peled 所说的那样,删除控件以避免资源泄漏。

private void cleanup(Control c)
{          
    foreach(Control child in c.Controls)        
        cleanup(child);         

    if (c.Parent != null)
    {
        c.Parent.Controls.Remove(c);
        c.Dispose();
    }
}

【讨论】:

    【解决方案2】:

    对于这样的场景,我会使用自定义事件向父控件发送删除请求:

    您的 ComboControl 与自定义事件:

     //Create Custom Event
        public delegate void DeleteControlDelegate(object sender);
    
        public partial class ComboControl : UserControl
        {
            //Custom Event to send Delete request
            public event DeleteControlDelegate DeleteControlDelegate;
    
            public ComboControl()
            {
                InitializeComponent();
            }
    
            //Invoke Custom Event
            private void OnDeleteControl(object sender)
            {
                DeleteControlDelegate?.Invoke(sender);
            }
    
            private void BtnDel_Click(object sender, EventArgs e)
            {
                //On ButtonClick send Delete request
               OnDeleteControl(this);
            }
        }
    

    并在您的 ContainerControl 中订阅每个 ComboControl 的事件:

    List<ComboControl> _comboControls = new List<ComboControl>();
    
            public ContainerControl()
            {
                InitializeComponent();
            }
    
            private void ContainerControl_Load(object sender, EventArgs e)
            {
                _comboControls.Add(new ComboControl());
                _comboControls.Add(new ComboControl());
                _comboControls.Add(new ComboControl());
    
                foreach (ComboControl value in _comboControls)
                {
                    flowLayoutPanel.Controls.Add(value);
                    //Subscribe to Custom Event here
                    value.DeleteControlDelegate += Value_DeleteControlDelegate;
                }
            }
    
            private void Value_DeleteControlDelegate(object sender)
            {
                //When Raised Delete ComboControl
                flowLayoutPanel.Controls.Remove((Control) sender);
            }
        }
    

    【讨论】:

    • 我喜欢这种方法,但是我得到一个 DeleteControlDelegate 是一种类型,但用作这两行的值 value.DeleteControlDelegate += Value_DeleteControlDelegate; DeleteControlDelegate?.Invoke(sender);会戳到我弄明白。 (还没有真正关注 Deligates)
    • 我做了,但我不得不替换:public delegate void DeleteControlDelegate(object sender);与:公共事件 EventHandler DeleteControlDelegate(object sender);我认为还有一些其他的小改动
    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    相关资源
    最近更新 更多