2 // ToolStripItemCollection.cs
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
31 using System.Collections;
32 using System.Windows.Forms.Layout;
34 namespace System.Windows.Forms
36 public class ToolStripItemCollection : ArrangedElementCollection, IList, ICollection, IEnumerable
38 private ToolStrip owner;
40 #region Public Constructor
41 public ToolStripItemCollection (ToolStrip owner, ToolStripItem[] value) : base ()
44 throw new ArgumentNullException ("owner");
49 foreach (ToolStripItem tsi in value)
54 #region Public Properties
55 public override bool IsReadOnly { get { return base.IsReadOnly; } }
57 public new virtual ToolStripItem this[int index] { get { return (ToolStripItem)base[index]; } }
59 public virtual ToolStripItem this[string key] {
61 foreach (ToolStripItem tsi in this)
70 #region Public Methods
71 public ToolStripItem Add (Image image)
73 ToolStripItem tsb = owner.CreateDefaultItem (string.Empty, image, null);
78 public ToolStripItem Add (string text)
80 ToolStripItem tsb = owner.CreateDefaultItem (text, null, null);
85 public int Add (ToolStripItem value)
88 throw new ArgumentNullException ("value");
93 int index = base.Add (value);
95 owner.OnItemAdded (new ToolStripItemEventArgs (value));
99 public ToolStripItem Add (string text, Image image)
101 ToolStripItem tsb = owner.CreateDefaultItem (text, image, null);
106 public ToolStripItem Add (string text, Image image, EventHandler onClick)
108 ToolStripItem tsb = owner.CreateDefaultItem (text, image, onClick);
113 public void AddRange (ToolStripItem[] toolStripItems)
115 if (toolStripItems == null)
116 throw new ArgumentNullException ("toolStripItems");
118 throw new NotSupportedException ("This collection is read-only");
120 this.owner.SuspendLayout ();
122 foreach (ToolStripItem tsi in toolStripItems)
125 this.owner.ResumeLayout ();
128 public void AddRange (ToolStripItemCollection toolStripItems)
130 if (toolStripItems == null)
131 throw new ArgumentNullException ("toolStripItems");
133 throw new NotSupportedException ("This collection is read-only");
135 this.owner.SuspendLayout ();
137 foreach (ToolStripItem tsi in toolStripItems)
140 this.owner.ResumeLayout ();
143 public new virtual void Clear ()
146 throw new NotSupportedException ("This collection is read-only");
149 owner.PerformLayout ();
152 public bool Contains (ToolStripItem value)
154 return base.Contains (value);
157 public virtual bool ContainsKey (string key)
159 return this[key] != null;
162 public void CopyTo (ToolStripItem[] array, int index)
164 base.CopyTo (array, index);
168 public ToolStripItem[] Find (string key, bool searchAllChildren)
171 throw new ArgumentNullException ("key");
173 ArrayList al = new ArrayList ();
175 foreach (ToolStripItem tsi in this) {
176 if (tsi.Name == key) {
179 if (searchAllChildren) {
180 // TODO: tsi does not have an items property yet..
185 return (ToolStripItem[])al.ToArray ();
188 public int IndexOf (ToolStripItem value)
190 return base.IndexOf (value);
193 public virtual int IndexOfKey (string key)
195 ToolStripItem tsi = this[key];
200 return this.IndexOf (tsi);
203 public void Insert (int index, ToolStripItem value)
206 throw new ArgumentNullException ("value");
208 base.Insert (index, value);
209 owner.OnItemAdded (new ToolStripItemEventArgs (value));
210 owner.PerformLayout ();
213 public void Remove (ToolStripItem value)
216 throw new NotSupportedException ("This collection is read-only");
219 owner.OnItemRemoved (new ToolStripItemEventArgs (value));
220 owner.PerformLayout ();
223 public new void RemoveAt (int index)
226 throw new NotSupportedException ("This collection is read-only");
228 ToolStripItem tsi = (ToolStripItem)base[index];
229 base.RemoveAt (index);
230 owner.OnItemRemoved (new ToolStripItemEventArgs (tsi));
231 owner.PerformLayout ();
234 public virtual void RemoveByKey (string key)
237 throw new NotSupportedException ("This collection is read-only");
239 ToolStripItem tsi = this[key];
248 #region Internal Methods
249 // When we create DisplayedItems, we don't want to modify the item's
250 // parent or trigger a layout.
251 internal int AddNoOwnerOrLayout (ToolStripItem value)
254 throw new ArgumentNullException ("value");
256 int index = base.Add (value);
261 #region IList Members
262 int IList.Add (object value)
264 return this.Add ((ToolStripItem)value);
272 bool IList.Contains (object value)
274 return this.Contains ((ToolStripItem)value);
277 int IList.IndexOf (object value)
279 return this.IndexOf ((ToolStripItem)value);
282 void IList.Insert (int index, object value)
284 this.Insert (index, (ToolStripItem)value);
287 bool IList.IsFixedSize {
288 get { return this.IsFixedSize; }
291 bool IList.IsReadOnly {
292 get { return this.IsReadOnly; }
295 void IList.Remove (object value)
297 this.Remove ((ToolStripItem)value); ;
300 void IList.RemoveAt (int index)
302 this.RemoveAt (index);
305 object IList.this[int index] {
306 get { return this[index]; }
307 set { throw new NotSupportedException (); }