2 // System.Web.UI.ControlCollection.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@novell.com)
8 // Copyright (c) 2002-2004 Novell, Inc. (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
35 namespace System.Web.UI {
37 public class ControlCollection : ICollection, IEnumerable
45 public ControlCollection (Control owner)
48 throw new ArgumentException ("owner");
57 public bool IsReadOnly {
58 get { return readOnly; }
61 public bool IsSynchronized {
65 public virtual Control this [int index] {
67 if (index < 0 || index >= count)
68 throw new ArgumentOutOfRangeException ("index");
70 return controls [index];
74 protected Control Owner {
78 public object SyncRoot {
82 void EnsureControls ()
84 if (controls == null) {
85 controls = new Control [5];
86 } else if (controls.Length < count + 1) {
87 int n = controls.Length == 5 ? 3 : 2;
88 Control [] newControls = new Control [controls.Length * n];
89 Array.Copy (controls, 0, newControls, 0, controls.Length);
90 controls = newControls;
94 public virtual void Add (Control child)
97 throw new ArgumentNullException ();
100 throw new HttpException ();
104 controls [count++] = child;
105 owner.AddedControl (child, count - 1);
108 public virtual void AddAt (int index, Control child)
110 if (child == null) // maybe we should check for ! (child is Control)?
111 throw new ArgumentNullException ();
113 if (index < -1 || index > count)
114 throw new ArgumentOutOfRangeException ();
117 throw new HttpException ();
126 Array.Copy (controls, index, controls, index + 1, count - index);
128 controls [index] = child;
129 owner.AddedControl (child, index);
132 public virtual void Clear ()
134 if (controls == null)
138 for (int i = 0; i < count; i++)
139 owner.RemovedControl (controls [i]);
143 owner.ResetChildNames ();
146 public virtual bool Contains (Control c)
148 return (controls != null && Array.IndexOf (controls, c) != -1);
151 public void CopyTo (Array array, int index)
153 if (controls == null)
156 controls.CopyTo (array, index);
159 public IEnumerator GetEnumerator ()
161 return new SimpleEnumerator (this);
164 public virtual int IndexOf (Control c)
166 if (controls == null)
169 return Array.IndexOf (controls, c);
172 public virtual void Remove (Control value)
174 int idx = IndexOf (value);
180 public virtual void RemoveAt (int index)
183 throw new HttpException ();
186 Control ctrl = controls [index];
188 if (count - index > 0)
189 Array.Copy (controls, index + 1, controls, index, count - index);
191 controls [count] = null;
192 owner.RemovedControl (ctrl);
195 // Almost the same as in ArrayList
196 sealed class SimpleEnumerator : IEnumerator
198 ControlCollection coll;
201 object currentElement;
203 public SimpleEnumerator (ControlCollection coll)
207 version = coll.version;
210 public bool MoveNext ()
212 if (version != coll.version)
213 throw new InvalidOperationException ("List has changed.");
215 if (index >= -1 && ++index < coll.Count) {
216 currentElement = coll [index];
224 public object Current {
227 throw new InvalidOperationException (index == -1 ? "Enumerator not started" : "Enumerator ended");
229 return currentElement;
235 if (version != coll.version)
236 throw new InvalidOperationException ("List has changed.");