UIControl.UIControlCollection.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. namespace Microsoft.Windows.Forms
  5. {
  6. partial class UIControl
  7. {
  8. /// <summary>
  9. /// 虚拟控件集合
  10. /// </summary>
  11. public class UIControlCollection : Collection<UIControl>, IDisposable
  12. {
  13. //集合所属控件
  14. private IUIControl m_Owner;
  15. /// <summary>
  16. /// 构造函数
  17. /// </summary>
  18. /// <param name="owner">所属控件</param>
  19. internal UIControlCollection(IUIControl owner)
  20. {
  21. if (owner == null)
  22. throw new ArgumentNullException("owner");
  23. this.m_Owner = owner;
  24. }
  25. /// <summary>
  26. /// 析构函数
  27. /// </summary>
  28. ~UIControlCollection()
  29. {
  30. this.Dispose(false);
  31. }
  32. /// <summary>
  33. /// 插入控件
  34. /// </summary>
  35. /// <param name="index">索引,值越小 Z 轴顺序越靠后</param>
  36. /// <param name="control">控件</param>
  37. protected override void InsertItem(int index, UIControl control)
  38. {
  39. if (control == null)
  40. throw new ArgumentNullException("control");
  41. if (control.UIParentInternal == this.m_Owner)
  42. {
  43. control.BringToFront();
  44. }
  45. else
  46. {
  47. if (control.UIParentInternal != null)
  48. control.UIParentInternal.UIControls.Remove(control);
  49. this.m_Owner.SuspendLayout();
  50. try
  51. {
  52. base.InsertItem(index, control);
  53. control.UIParentInternal = this.m_Owner;
  54. }
  55. finally
  56. {
  57. this.m_Owner.ResumeLayout();
  58. }
  59. }
  60. }
  61. /// <summary>
  62. /// 移除控件
  63. /// </summary>
  64. /// <param name="index">索引</param>
  65. protected override void RemoveItem(int index)
  66. {
  67. UIControl control = this[index];
  68. this.m_Owner.SuspendLayout();
  69. try
  70. {
  71. base.RemoveItem(index);
  72. control.UIParentInternal = null;
  73. }
  74. finally
  75. {
  76. this.m_Owner.ResumeLayout();
  77. }
  78. }
  79. /// <summary>
  80. /// 不支持的操作
  81. /// </summary>
  82. /// <param name="index">索引</param>
  83. /// <param name="control">控件</param>
  84. protected override void SetItem(int index, UIControl control)
  85. {
  86. throw new NotSupportedException("不支持的操作");
  87. }
  88. /// <summary>
  89. /// 清空所有控件
  90. /// </summary>
  91. protected override void ClearItems()
  92. {
  93. this.m_Owner.SuspendLayout();
  94. try
  95. {
  96. foreach (UIControl control in this)
  97. control.UIParentInternal = null;
  98. base.ClearItems();
  99. }
  100. finally
  101. {
  102. this.m_Owner.ResumeLayout();
  103. }
  104. }
  105. /// <summary>
  106. /// 批量添加控件
  107. /// </summary>
  108. /// <param name="controls">控件集合</param>
  109. public void AddRange(IEnumerable<UIControl> controls)
  110. {
  111. this.m_Owner.SuspendLayout();
  112. try
  113. {
  114. foreach (UIControl control in controls)
  115. this.Add(control);
  116. }
  117. finally
  118. {
  119. this.m_Owner.ResumeLayout();
  120. }
  121. }
  122. /// <summary>
  123. /// 释放资源
  124. /// </summary>
  125. /// <param name="disposing">释放托管资源为 true,否则为 false</param>
  126. private void Dispose(bool disposing)
  127. {
  128. if (this.m_Owner != null)
  129. {
  130. if (this.Count > 0)
  131. {
  132. this.m_Owner.SuspendLayout();
  133. try
  134. {
  135. UIControl[] array = new UIControl[this.Count];
  136. this.CopyTo(array, 0);
  137. foreach (UIControl control in array)
  138. control.Dispose();
  139. base.ClearItems();
  140. }
  141. finally
  142. {
  143. this.m_Owner.ResumeLayout();
  144. }
  145. }
  146. this.m_Owner = null;
  147. }
  148. }
  149. /// <summary>
  150. /// 释放资源
  151. /// </summary>
  152. public void Dispose()
  153. {
  154. this.Dispose(true);
  155. GC.SuppressFinalize(this);
  156. }
  157. }
  158. }
  159. }