2 // ToolStripItemCollection.cs
\r
4 // Permission is hereby granted, free of charge, to any person obtaining
\r
5 // a copy of this software and associated documentation files (the
\r
6 // "Software"), to deal in the Software without restriction, including
\r
7 // without limitation the rights to use, copy, modify, merge, publish,
\r
8 // distribute, sublicense, and/or sell copies of the Software, and to
\r
9 // permit persons to whom the Software is furnished to do so, subject to
\r
10 // the following conditions:
\r
12 // The above copyright notice and this permission notice shall be
\r
13 // included in all copies or substantial portions of the Software.
\r
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 // Copyright (c) 2006 Jonathan Pobst
\r
26 // Jonathan Pobst (monkey@jpobst.com)
\r
30 using System.Drawing;
\r
31 using System.Collections;
\r
32 using System.Windows.Forms.Layout;
\r
34 namespace System.Windows.Forms
\r
36 public class ToolStripItemCollection : ArrangedElementCollection, IList, ICollection, IEnumerable
\r
38 private ToolStrip owner;
\r
40 #region Public Constructor
\r
41 public ToolStripItemCollection (ToolStrip owner, ToolStripItem[] value) : base ()
\r
44 throw new ArgumentNullException ("owner");
\r
49 foreach (ToolStripItem tsi in value)
\r
54 #region Public Properties
\r
55 public override bool IsReadOnly { get { return base.IsReadOnly; } }
\r
57 public virtual ToolStripItem this[int index] { get { return (ToolStripItem)base[index]; } }
\r
59 public virtual ToolStripItem this[string key] {
\r
61 foreach (ToolStripItem tsi in this)
\r
62 if (tsi.Name == key)
\r
70 #region Public Methods
\r
71 public ToolStripItem Add (Image image)
\r
73 ToolStripButton tsb = new ToolStripButton (image);
\r
78 public ToolStripItem Add (string text)
\r
80 ToolStripButton tsb = new ToolStripButton (text);
\r
85 public int Add (ToolStripItem value)
\r
88 throw new ArgumentNullException ("value");
\r
90 value.Owner = owner;
\r
91 value.Parent = owner;
\r
92 owner.PerformLayout ();
\r
93 owner.OnItemAdded (new ToolStripItemEventArgs (value));
\r
94 return base.Add (value);
\r
97 public ToolStripItem Add (string text, Image image)
\r
99 ToolStripButton tsb = new ToolStripButton (text, image);
\r
104 public ToolStripItem Add (string text, Image image, EventHandler onClick)
\r
106 ToolStripButton tsb = new ToolStripButton (text, image, onClick);
\r
111 public void AddRange (ToolStripItem[] toolStripItems)
\r
113 if (toolStripItems == null)
\r
114 throw new ArgumentNullException ("toolStripItems");
\r
115 if (this.IsReadOnly)
\r
116 throw new NotSupportedException ("This collection is read-only");
\r
118 this.owner.SuspendLayout ();
\r
120 foreach (ToolStripItem tsi in toolStripItems)
\r
123 this.owner.ResumeLayout ();
\r
126 public void AddRange (ToolStripItemCollection toolStripItems)
\r
128 if (toolStripItems == null)
\r
129 throw new ArgumentNullException ("toolStripItems");
\r
130 if (this.IsReadOnly)
\r
131 throw new NotSupportedException ("This collection is read-only");
\r
133 this.owner.SuspendLayout ();
\r
135 foreach (ToolStripItem tsi in toolStripItems)
\r
138 this.owner.ResumeLayout ();
\r
141 public virtual void Clear ()
\r
143 if (this.IsReadOnly)
\r
144 throw new NotSupportedException ("This collection is read-only");
\r
147 owner.PerformLayout ();
\r
150 public bool Contains (ToolStripItem value)
\r
152 return base.Contains (value);
\r
155 public virtual bool ContainsKey (string key)
\r
157 return this[key] != null;
\r
160 public void CopyTo (ToolStripItem[] array, int index)
\r
162 base.CopyTo (array, index);
\r
166 public ToolStripItem[] Find (string key, bool searchAllChildren)
\r
169 throw new ArgumentNullException ("key");
\r
171 ArrayList al = new ArrayList ();
\r
173 foreach (ToolStripItem tsi in this) {
\r
174 if (tsi.Name == key) {
\r
177 if (searchAllChildren) {
\r
178 // TODO: tsi does not have an items property yet..
\r
183 return (ToolStripItem[])al.ToArray ();
\r
186 public int IndexOf (ToolStripItem value)
\r
188 return base.IndexOf (value);
\r
191 public virtual int IndexOfKey (string key)
\r
193 ToolStripItem tsi = this[key];
\r
198 return this.IndexOf (tsi);
\r
201 public void Insert (int index, ToolStripItem value)
\r
204 throw new ArgumentNullException ("value");
\r
206 base.Insert (index, value);
\r
207 owner.OnItemAdded (new ToolStripItemEventArgs (value));
\r
208 owner.PerformLayout ();
\r
211 public void Remove (ToolStripItem value)
\r
213 if (this.IsReadOnly)
\r
214 throw new NotSupportedException ("This collection is read-only");
\r
216 base.Remove (value);
\r
217 owner.OnItemRemoved (new ToolStripItemEventArgs (value));
\r
218 owner.PerformLayout ();
\r
221 public void RemoveAt (int index)
\r
223 if (this.IsReadOnly)
\r
224 throw new NotSupportedException ("This collection is read-only");
\r
226 ToolStripItem tsi = (ToolStripItem)base[index];
\r
227 base.RemoveAt (index);
\r
228 owner.OnItemRemoved (new ToolStripItemEventArgs (tsi));
\r
229 owner.PerformLayout ();
\r
232 public virtual void RemoveByKey (string key)
\r
234 if (this.IsReadOnly)
\r
235 throw new NotSupportedException ("This collection is read-only");
\r
237 ToolStripItem tsi = this[key];
\r
246 #region IList Members
\r
247 int IList.Add (object value)
\r
249 return this.Add ((ToolStripItem)value);
\r
252 void IList.Clear ()
\r
257 bool IList.Contains (object value)
\r
259 return this.Contains ((ToolStripItem)value);
\r
262 int IList.IndexOf (object value)
\r
264 return this.IndexOf ((ToolStripItem)value);
\r
267 void IList.Insert (int index, object value)
\r
269 this.Insert (index, (ToolStripItem)value);
\r
272 bool IList.IsFixedSize {
\r
273 get { return this.IsFixedSize; }
\r
276 bool IList.IsReadOnly {
\r
277 get { return this.IsReadOnly; }
\r
280 void IList.Remove (object value)
\r
282 this.Remove ((ToolStripItem)value); ;
\r
285 void IList.RemoveAt (int index)
\r
287 this.RemoveAt (index);
\r
290 object IList.this[int index] {
\r
291 get { return this[index]; }
\r
292 set { throw new NotSupportedException (); }
\r