123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- using System;
- using System.Collections;
- namespace LYFZ.ComponentLibrary.ChatListControl
- {
- public class ChatListItemCollection : IList, ICollection, IEnumerable
- {
- private int count;
- private ChatListItem[] m_arrItem;
- private ChatListBox owner;
- public int Count
- {
- get
- {
- return this.count;
- }
- }
- public ChatListItem this[int index]
- {
- get
- {
- if (index < 0 || index >= this.count)
- {
- throw new IndexOutOfRangeException("索引在阵列的界限之外");
- }
- return this.m_arrItem[index];
- }
- set
- {
- if (index < 0 || index >= this.count)
- {
- throw new IndexOutOfRangeException("索引在阵列的界限之外");
- }
- this.m_arrItem[index] = value;
- }
- }
- bool IList.IsFixedSize
- {
- get
- {
- return false;
- }
- }
- bool IList.IsReadOnly
- {
- get
- {
- return false;
- }
- }
- object IList.this[int index]
- {
- get
- {
- return this[index];
- }
- set
- {
- if (!(value is ChatListItem))
- {
- throw new ArgumentException("Value cannot convert to ListItem");
- }
- this[index] = (ChatListItem)value;
- }
- }
- int ICollection.Count
- {
- get
- {
- return this.count;
- }
- }
- bool ICollection.IsSynchronized
- {
- get
- {
- return true;
- }
- }
- object ICollection.SyncRoot
- {
- get
- {
- return this;
- }
- }
- public ChatListItemCollection(ChatListBox owner)
- {
- this.owner = owner;
- }
- private void EnsureSpace(int elements)
- {
- if (this.m_arrItem == null)
- {
- this.m_arrItem = new ChatListItem[Math.Max(elements, 4)];
- return;
- }
- if (this.count + elements > this.m_arrItem.Length)
- {
- ChatListItem[] arrTemp = new ChatListItem[Math.Max(this.count + elements, this.m_arrItem.Length * 2)];
- this.m_arrItem.CopyTo(arrTemp, 0);
- this.m_arrItem = arrTemp;
- }
- }
- public int IndexOf(ChatListItem item)
- {
- return Array.IndexOf<ChatListItem>(this.m_arrItem, item);
- }
- public void Add(ChatListItem item)
- {
- if (item == null)
- {
- throw new ArgumentNullException("Item cannot be null");
- }
- this.EnsureSpace(1);
- if (-1 == this.IndexOf(item))
- {
- item.OwnerChatListBox = this.owner;
- this.m_arrItem[this.count++] = item;
- this.owner.Invalidate();
- }
- }
- public void AddRange(ChatListItem[] items)
- {
- if (items == null)
- {
- throw new ArgumentNullException("Items cannot be null");
- }
- this.EnsureSpace(items.Length);
- try
- {
- for (int i = 0; i < items.Length; i++)
- {
- ChatListItem item = items[i];
- if (item == null)
- {
- throw new ArgumentNullException("Item cannot be null");
- }
- if (-1 == this.IndexOf(item))
- {
- item.OwnerChatListBox = this.owner;
- this.m_arrItem[this.count++] = item;
- }
- }
- }
- finally
- {
- this.owner.Invalidate();
- }
- }
- public void Remove(ChatListItem item)
- {
- int index = this.IndexOf(item);
- if (-1 != index)
- {
- this.RemoveAt(index);
- }
- }
- public void RemoveAt(int index)
- {
- if (index < 0 || index >= this.count)
- {
- throw new IndexOutOfRangeException("索引在阵列的界限之外");
- }
- this.count--;
- int i = index;
- int Len = this.count;
- while (i < Len)
- {
- this.m_arrItem[i] = this.m_arrItem[i + 1];
- i++;
- }
- this.owner.Invalidate();
- }
- public void Clear()
- {
- this.count = 0;
- this.m_arrItem = null;
- this.owner.Invalidate();
- }
- public void Insert(int index, ChatListItem item)
- {
- if (index < 0 || index >= this.count)
- {
- throw new IndexOutOfRangeException("索引在阵列的界限之外");
- }
- if (item == null)
- {
- throw new ArgumentNullException("Item cannot be null");
- }
- this.EnsureSpace(1);
- for (int i = this.count; i > index; i--)
- {
- this.m_arrItem[i] = this.m_arrItem[i - 1];
- }
- item.OwnerChatListBox = this.owner;
- this.m_arrItem[index] = item;
- this.count++;
- this.owner.Invalidate();
- }
- public bool Contains(ChatListItem item)
- {
- return this.IndexOf(item) != -1;
- }
- public void CopyTo(Array array, int index)
- {
- if (array == null)
- {
- throw new ArgumentNullException("array cannot be null");
- }
- this.m_arrItem.CopyTo(array, index);
- }
- int IList.Add(object value)
- {
- if (!(value is ChatListItem))
- {
- throw new ArgumentException("Value cannot convert to ListItem");
- }
- this.Add((ChatListItem)value);
- return this.IndexOf((ChatListItem)value);
- }
- void IList.Clear()
- {
- this.Clear();
- }
- bool IList.Contains(object value)
- {
- if (!(value is ChatListItem))
- {
- throw new ArgumentException("Value cannot convert to ListItem");
- }
- return this.Contains((ChatListItem)value);
- }
- int IList.IndexOf(object value)
- {
- if (!(value is ChatListItem))
- {
- throw new ArgumentException("Value cannot convert to ListItem");
- }
- return this.IndexOf((ChatListItem)value);
- }
- void IList.Insert(int index, object value)
- {
- if (!(value is ChatListItem))
- {
- throw new ArgumentException("Value cannot convert to ListItem");
- }
- this.Insert(index, (ChatListItem)value);
- }
- void IList.Remove(object value)
- {
- if (!(value is ChatListItem))
- {
- throw new ArgumentException("Value cannot convert to ListItem");
- }
- this.Remove((ChatListItem)value);
- }
- void IList.RemoveAt(int index)
- {
- this.RemoveAt(index);
- }
- void ICollection.CopyTo(Array array, int index)
- {
- this.CopyTo(array, index);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- int i = 0;
- int num = this.count;
- while (i < num)
- {
- yield return this.m_arrItem[i];
- i++;
- }
- yield break;
- }
- }
- }
|