2 // ListViewGroupCollection.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 Daniel Nauck
26 // Daniel Nauck (dna(at)mono-project(dot)de)
32 using System.Collections;
33 using System.ComponentModel;
35 namespace System.Windows.Forms
38 public class ListViewGroupCollection : IList, ICollection, IEnumerable
40 private ArrayList list = null;
41 private ListView list_view_owner = null;
43 ListViewGroupCollection()
45 list = new ArrayList();
48 internal ListViewGroupCollection(ListView listViewOwner) : this()
50 list_view_owner = listViewOwner;
53 internal ListView ListViewOwner {
54 get { return list_view_owner; }
55 set { list_view_owner = value; }
58 #region IEnumerable Members
60 public IEnumerator GetEnumerator()
62 return list.GetEnumerator();
67 #region ICollection Members
69 public void CopyTo(Array array, int index)
71 list.CopyTo(array, index);
75 get { return list.Count; }
78 bool ICollection.IsSynchronized {
82 object ICollection.SyncRoot {
90 int IList.Add(object value)
92 if (!(value is ListViewGroup))
93 throw new ArgumentException("value");
95 return Add((ListViewGroup)value);
98 public int Add(ListViewGroup value)
103 CheckListViewItemsInGroup(value);
104 value.ListViewOwner = this.list_view_owner;
105 int index = list.Add(value);
107 if (this.list_view_owner != null)
108 list_view_owner.Redraw(true);
113 public ListViewGroup Add(string key, string headerText)
115 ListViewGroup newGroup = new ListViewGroup(key, headerText);
125 if(list_view_owner != null)
126 list_view_owner.Redraw(true);
129 bool IList.Contains(object value)
131 if (value is ListViewGroup)
132 return Contains((ListViewGroup)value);
137 public bool Contains(ListViewGroup value)
139 return list.Contains(value);
142 int IList.IndexOf(object value)
144 if (value is ListViewGroup)
145 return IndexOf((ListViewGroup)value);
150 public int IndexOf(ListViewGroup value)
152 return list.IndexOf(value);
155 void IList.Insert(int index, object value)
157 if (value is ListViewGroup)
158 Insert(index, (ListViewGroup)value);
161 public void Insert(int index, ListViewGroup group)
166 CheckListViewItemsInGroup(group);
167 list.Insert(index, group);
169 if(list_view_owner != null)
170 list_view_owner.Redraw(true);
173 bool IList.IsFixedSize {
174 get { return false; }
177 bool IList.IsReadOnly {
178 get { return false; }
181 void IList.Remove(object value)
183 Remove((ListViewGroup)value);
186 public void Remove(ListViewGroup value)
188 if (!list.Contains(value))
193 if (list_view_owner != null)
194 list_view_owner.Redraw(true);
197 public void RemoveAt(int index)
199 if (list.Count <= index || index < 0)
205 object IList.this[int index] {
206 get { return this[index]; }
208 if (value is ListViewGroup)
209 this[index] = (ListViewGroup)value;
213 public ListViewGroup this[int index] {
215 if (list.Count <= index || index < 0)
216 throw new ArgumentOutOfRangeException("index");
218 return (ListViewGroup)list[index];
221 if (list.Count <= index || index < 0)
222 throw new ArgumentOutOfRangeException("index");
224 if (!Contains(value)) {
225 CheckListViewItemsInGroup(value);
228 if (list_view_owner != null)
229 list_view_owner.Redraw(true);
234 public ListViewGroup this[string key] {
236 foreach (ListViewGroup item in list)
238 if (item.Name.Equals(key))
245 for(int i = 0; i < list.Count; i++)
247 if ((this[i].Name != null) && (this[i].Name.Equals(key))) {
257 public void AddRange(ListViewGroup[] groups)
259 foreach (ListViewGroup item in groups)
265 public void AddRange(ListViewGroupCollection groups)
267 foreach (ListViewGroup item in groups)
273 private void CheckListViewItemsInGroup(ListViewGroup value)
275 //check for correct ListView
276 foreach (ListViewItem item in value.Items)
278 if (item.ListView != null && item.ListView != this.list_view_owner)
279 throw new ArgumentException("ListViewItem belongs to a ListView control other than the one that owns this ListViewGroupCollection.",