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 if (value is ToolStripMenuItem && (value as ToolStripMenuItem).ShortcutKeys != Keys.None)
94 ToolStripManager.AddToolStripMenuItem ((ToolStripMenuItem)value);
96 int index = base.Add (value);
98 owner.OnItemAdded (new ToolStripItemEventArgs (value));
102 public ToolStripItem Add (string text, Image image)
104 ToolStripItem tsb = owner.CreateDefaultItem (text, image, null);
109 public ToolStripItem Add (string text, Image image, EventHandler onClick)
111 ToolStripItem tsb = owner.CreateDefaultItem (text, image, onClick);
116 public void AddRange (ToolStripItem[] toolStripItems)
118 if (toolStripItems == null)
119 throw new ArgumentNullException ("toolStripItems");
121 throw new NotSupportedException ("This collection is read-only");
123 this.owner.SuspendLayout ();
125 foreach (ToolStripItem tsi in toolStripItems)
128 this.owner.ResumeLayout ();
131 public void AddRange (ToolStripItemCollection toolStripItems)
133 if (toolStripItems == null)
134 throw new ArgumentNullException ("toolStripItems");
136 throw new NotSupportedException ("This collection is read-only");
138 this.owner.SuspendLayout ();
140 foreach (ToolStripItem tsi in toolStripItems)
143 this.owner.ResumeLayout ();
146 public new virtual void Clear ()
149 throw new NotSupportedException ("This collection is read-only");
152 owner.PerformLayout ();
155 public bool Contains (ToolStripItem value)
157 return base.Contains (value);
160 public virtual bool ContainsKey (string key)
162 return this[key] != null;
165 public void CopyTo (ToolStripItem[] array, int index)
167 base.CopyTo (array, index);
171 public ToolStripItem[] Find (string key, bool searchAllChildren)
174 throw new ArgumentNullException ("key");
176 ArrayList al = new ArrayList ();
178 foreach (ToolStripItem tsi in this) {
179 if (tsi.Name == key) {
182 if (searchAllChildren) {
183 // TODO: tsi does not have an items property yet..
188 return (ToolStripItem[])al.ToArray ();
191 public int IndexOf (ToolStripItem value)
193 return base.IndexOf (value);
196 public virtual int IndexOfKey (string key)
198 ToolStripItem tsi = this[key];
203 return this.IndexOf (tsi);
206 public void Insert (int index, ToolStripItem value)
209 throw new ArgumentNullException ("value");
211 base.Insert (index, value);
212 owner.OnItemAdded (new ToolStripItemEventArgs (value));
213 owner.PerformLayout ();
216 public void Remove (ToolStripItem value)
219 throw new NotSupportedException ("This collection is read-only");
222 owner.OnItemRemoved (new ToolStripItemEventArgs (value));
223 owner.PerformLayout ();
226 public new void RemoveAt (int index)
229 throw new NotSupportedException ("This collection is read-only");
231 ToolStripItem tsi = (ToolStripItem)base[index];
232 base.RemoveAt (index);
233 owner.OnItemRemoved (new ToolStripItemEventArgs (tsi));
234 owner.PerformLayout ();
237 public virtual void RemoveByKey (string key)
240 throw new NotSupportedException ("This collection is read-only");
242 ToolStripItem tsi = this[key];
251 #region Internal Methods
252 // When we create DisplayedItems, we don't want to modify the item's
253 // parent or trigger a layout.
254 internal int AddNoOwnerOrLayout (ToolStripItem value)
257 throw new ArgumentNullException ("value");
259 int index = base.Add (value);
263 internal void InsertNoOwnerOrLayout (int index, ToolStripItem value)
266 throw new ArgumentNullException ("value");
268 base.Insert (index, value);
271 internal void RemoveNoOwnerOrLayout (ToolStripItem value)
274 throw new ArgumentNullException ("value");
280 #region IList Members
281 int IList.Add (object value)
283 return this.Add ((ToolStripItem)value);
291 bool IList.Contains (object value)
293 return this.Contains ((ToolStripItem)value);
296 int IList.IndexOf (object value)
298 return this.IndexOf ((ToolStripItem)value);
301 void IList.Insert (int index, object value)
303 this.Insert (index, (ToolStripItem)value);
306 bool IList.IsFixedSize {
307 get { return this.IsFixedSize; }
310 bool IList.IsReadOnly {
311 get { return this.IsReadOnly; }
314 void IList.Remove (object value)
316 this.Remove ((ToolStripItem)value); ;
319 void IList.RemoveAt (int index)
321 this.RemoveAt (index);
324 object IList.this[int index] {
325 get { return this[index]; }
326 set { throw new NotSupportedException (); }