using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Microsoft.Windows.Forms
{
partial class UIControl
{
///
/// 虚拟控件集合
///
public class UIControlCollection : Collection, IDisposable
{
//集合所属控件
private IUIControl m_Owner;
///
/// 构造函数
///
/// 所属控件
internal UIControlCollection(IUIControl owner)
{
if (owner == null)
throw new ArgumentNullException("owner");
this.m_Owner = owner;
}
///
/// 析构函数
///
~UIControlCollection()
{
this.Dispose(false);
}
///
/// 插入控件
///
/// 索引,值越小 Z 轴顺序越靠后
/// 控件
protected override void InsertItem(int index, UIControl control)
{
if (control == null)
throw new ArgumentNullException("control");
if (control.UIParentInternal == this.m_Owner)
{
control.BringToFront();
}
else
{
if (control.UIParentInternal != null)
control.UIParentInternal.UIControls.Remove(control);
this.m_Owner.SuspendLayout();
try
{
base.InsertItem(index, control);
control.UIParentInternal = this.m_Owner;
}
finally
{
this.m_Owner.ResumeLayout();
}
}
}
///
/// 移除控件
///
/// 索引
protected override void RemoveItem(int index)
{
UIControl control = this[index];
this.m_Owner.SuspendLayout();
try
{
base.RemoveItem(index);
control.UIParentInternal = null;
}
finally
{
this.m_Owner.ResumeLayout();
}
}
///
/// 不支持的操作
///
/// 索引
/// 控件
protected override void SetItem(int index, UIControl control)
{
throw new NotSupportedException("不支持的操作");
}
///
/// 清空所有控件
///
protected override void ClearItems()
{
this.m_Owner.SuspendLayout();
try
{
foreach (UIControl control in this)
control.UIParentInternal = null;
base.ClearItems();
}
finally
{
this.m_Owner.ResumeLayout();
}
}
///
/// 批量添加控件
///
/// 控件集合
public void AddRange(IEnumerable controls)
{
this.m_Owner.SuspendLayout();
try
{
foreach (UIControl control in controls)
this.Add(control);
}
finally
{
this.m_Owner.ResumeLayout();
}
}
///
/// 释放资源
///
/// 释放托管资源为 true,否则为 false
private void Dispose(bool disposing)
{
if (this.m_Owner != null)
{
if (this.Count > 0)
{
this.m_Owner.SuspendLayout();
try
{
UIControl[] array = new UIControl[this.Count];
this.CopyTo(array, 0);
foreach (UIControl control in array)
control.Dispose();
base.ClearItems();
}
finally
{
this.m_Owner.ResumeLayout();
}
}
this.m_Owner = null;
}
}
///
/// 释放资源
///
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
}
}
}