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 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");
92 owner.PerformLayout ();
93 owner.OnItemAdded (new ToolStripItemEventArgs (value));
94 return base.Add (value);
97 public ToolStripItem Add (string text, Image image)
99 ToolStripItem tsb = owner.CreateDefaultItem (text, image, null);
104 public ToolStripItem Add (string text, Image image, EventHandler onClick)
106 ToolStripItem tsb = owner.CreateDefaultItem (text, image, onClick);
111 public void AddRange (ToolStripItem[] toolStripItems)
113 if (toolStripItems == null)
114 throw new ArgumentNullException ("toolStripItems");
116 throw new NotSupportedException ("This collection is read-only");
118 this.owner.SuspendLayout ();
120 foreach (ToolStripItem tsi in toolStripItems)
123 this.owner.ResumeLayout ();
126 public void AddRange (ToolStripItemCollection toolStripItems)
128 if (toolStripItems == null)
129 throw new ArgumentNullException ("toolStripItems");
131 throw new NotSupportedException ("This collection is read-only");
133 this.owner.SuspendLayout ();
135 foreach (ToolStripItem tsi in toolStripItems)
138 this.owner.ResumeLayout ();
141 public virtual void Clear ()
144 throw new NotSupportedException ("This collection is read-only");
147 owner.PerformLayout ();
150 public bool Contains (ToolStripItem value)
152 return base.Contains (value);
155 public virtual bool ContainsKey (string key)
157 return this[key] != null;
160 public void CopyTo (ToolStripItem[] array, int index)
162 base.CopyTo (array, index);
166 public ToolStripItem[] Find (string key, bool searchAllChildren)
169 throw new ArgumentNullException ("key");
171 ArrayList al = new ArrayList ();
173 foreach (ToolStripItem tsi in this) {
174 if (tsi.Name == key) {
177 if (searchAllChildren) {
178 // TODO: tsi does not have an items property yet..
183 return (ToolStripItem[])al.ToArray ();
186 public int IndexOf (ToolStripItem value)
188 return base.IndexOf (value);
191 public virtual int IndexOfKey (string key)
193 ToolStripItem tsi = this[key];
198 return this.IndexOf (tsi);
201 public void Insert (int index, ToolStripItem value)
204 throw new ArgumentNullException ("value");
206 base.Insert (index, value);
207 owner.OnItemAdded (new ToolStripItemEventArgs (value));
208 owner.PerformLayout ();
211 public void Remove (ToolStripItem value)
214 throw new NotSupportedException ("This collection is read-only");
217 owner.OnItemRemoved (new ToolStripItemEventArgs (value));
218 owner.PerformLayout ();
221 public void RemoveAt (int index)
224 throw new NotSupportedException ("This collection is read-only");
226 ToolStripItem tsi = (ToolStripItem)base[index];
227 base.RemoveAt (index);
228 owner.OnItemRemoved (new ToolStripItemEventArgs (tsi));
229 owner.PerformLayout ();
232 public virtual void RemoveByKey (string key)
235 throw new NotSupportedException ("This collection is read-only");
237 ToolStripItem tsi = this[key];
246 #region IList Members
247 int IList.Add (object value)
249 return this.Add ((ToolStripItem)value);
257 bool IList.Contains (object value)
259 return this.Contains ((ToolStripItem)value);
262 int IList.IndexOf (object value)
264 return this.IndexOf ((ToolStripItem)value);
267 void IList.Insert (int index, object value)
269 this.Insert (index, (ToolStripItem)value);
272 bool IList.IsFixedSize {
273 get { return this.IsFixedSize; }
276 bool IList.IsReadOnly {
277 get { return this.IsReadOnly; }
280 void IList.Remove (object value)
282 this.Remove ((ToolStripItem)value); ;
285 void IList.RemoveAt (int index)
287 this.RemoveAt (index);
290 object IList.this[int index] {
291 get { return this[index]; }
292 set { throw new NotSupportedException (); }