ToolBarItemCollection.cs
上传用户:nnpulika
上传日期:2013-02-15
资源大小:597k
文件大小:4k
源码类别:

状态条

开发平台:

C#

  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections;
  4. using System.Windows.Forms;
  5. using UtilityLibrary.CommandBars;
  6. namespace UtilityLibrary.Collections
  7. {
  8. /// <summary>
  9. /// Summary description for ToolbarButtonCollection.
  10. /// </summary>
  11. public class ToolBarItemCollection : System.Collections.CollectionBase, IEnumerable
  12. {
  13. #region Events
  14. public event EventHandler Changed;
  15. #endregion
  16. #region Class Variable
  17. ToolBarEx parentToolBar = null;
  18. #endregion
  19. #region Constructors
  20. public ToolBarItemCollection(ToolBarEx parent)
  21. {
  22.             parentToolBar = parent;
  23. }
  24. public ToolBarItemCollection()
  25. {
  26. }
  27. #endregion
  28. #region Methods
  29. public void Add(ToolBarItem item)
  30. {
  31. if ( parentToolBar != null && parentToolBar.PlaceHolderAdded == true )
  32. parentToolBar.RemovePlaceHolderToolBarItem();
  33. InnerList.Add(item);
  34. RaiseChanged();
  35. }
  36. public void AddRange(ToolBarItem[] InnerList)
  37. {
  38. if ( parentToolBar != null && parentToolBar.PlaceHolderAdded == true )
  39. parentToolBar.RemovePlaceHolderToolBarItem();
  40. foreach (ToolBarItem item in InnerList)
  41. this.InnerList.Add(item);
  42. RaiseChanged();
  43. }
  44. public void Insert(int index, ToolBarItem item)
  45. {
  46. if ( parentToolBar != null && parentToolBar.PlaceHolderAdded == true )
  47. parentToolBar.RemovePlaceHolderToolBarItem();
  48. InnerList.Insert(index, item);
  49. RaiseChanged();
  50. }
  51. public void Remove(ToolBarItem item)
  52. {
  53. // Remove the item from the ToolBar first
  54. int index = IndexOf(item);
  55.             if ( parentToolBar != null )
  56. parentToolBar.RemoveToolBarItem(index);
  57. if (!InnerList.Contains(item)) return;
  58.             InnerList.Remove(item);
  59. RaiseChanged();
  60. // If we don't have any items left
  61. if ( InnerList.Count == 0 && parentToolBar.PlaceHolderAdded == false )
  62. parentToolBar.AddPlaceHolderToolBarItem();
  63. }
  64. public new void Clear()
  65. {
  66. // Before wiping out all items from
  67. // the ToolBarItem collection make sure we also
  68. // delete the items from the toolBar itself
  69. for ( int i = 0; i < InnerList.Count; i++ )
  70. {
  71. if ( parentToolBar != null )
  72. parentToolBar.RemoveToolBarItem(i);
  73. }
  74. // Now from the collection
  75. InnerList.Clear();
  76. // Inform listeners to update
  77. RaiseChanged();
  78. // We don't have any bands left, add
  79. // the ToolBar place holder
  80. if ( InnerList.Count == 0 && parentToolBar.PlaceHolderAdded == false )
  81. parentToolBar.AddPlaceHolderToolBarItem();
  82. }
  83. public bool Contains(ToolBarItem item)
  84. {
  85. return InnerList.Contains(item);
  86. }
  87. public int IndexOf(ToolBarItem item)
  88. {
  89. return InnerList.IndexOf(item);
  90. }
  91. public ToolBarItem this[int index]
  92. {
  93. get 
  94. return (ToolBarItem)InnerList[index]; 
  95. }
  96. }
  97. internal ToolBarItem this[Keys shortcut]
  98. {
  99. get
  100. {
  101. foreach (ToolBarItem item in InnerList)
  102. if ( (item.Shortcut == shortcut) && (item.Enabled) && (item.Visible) )
  103. return item;
  104. return null;
  105. }
  106. }
  107. internal ToolBarItem this[char mnemonic]
  108. {
  109. get
  110. {
  111. foreach (ToolBarItem item in InnerList)
  112. {
  113. string text = item.Text;
  114. if ( text != string.Empty && text != null )
  115. {    
  116. for (int i = 0; i < text.Length; i++)
  117. {
  118. if ((text[i] == '&') && (i + 1 < text.Length) && (text[i + 1] != '&'))
  119. if (mnemonic == Char.ToUpper(text[i + 1]))
  120. return item;
  121. }
  122. }
  123. }
  124. return null;
  125. }
  126. }
  127. #endregion
  128. #region Implementation
  129. void RaiseChanged()
  130. {
  131. if (Changed != null) Changed(this, null);
  132. }
  133. #endregion
  134. }
  135. }