ListBox.ObjectCollection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Globalization;
  8. namespace System.Windows.Forms
  9. {
  10. public partial class ListBox
  11. {
  12. public class ObjectCollection : IList
  13. {
  14. private readonly ListBox owner;
  15. private ItemArray items;
  16. public ObjectCollection(ListBox owner)
  17. {
  18. this.owner = owner;
  19. }
  20. /// <summary>
  21. /// Initializes a new instance of ListBox.ObjectCollection based on another ListBox.ObjectCollection.
  22. /// </summary>
  23. public ObjectCollection(ListBox owner, ObjectCollection value)
  24. {
  25. this.owner = owner;
  26. AddRange(value);
  27. }
  28. /// <summary>
  29. /// Initializes a new instance of ListBox.ObjectCollection containing any array of objects.
  30. /// </summary>
  31. public ObjectCollection(ListBox owner, object[] value)
  32. {
  33. this.owner = owner;
  34. AddRange(value);
  35. }
  36. /// <summary>
  37. /// Retrieves the number of items.
  38. /// </summary>
  39. public int Count
  40. {
  41. get
  42. {
  43. return InnerArray.GetCount(0);
  44. }
  45. }
  46. /// <summary>
  47. /// Internal access to the actual data store.
  48. /// </summary>
  49. internal ItemArray InnerArray
  50. {
  51. get
  52. {
  53. if (items == null)
  54. {
  55. items = new ItemArray(owner);
  56. }
  57. return items;
  58. }
  59. }
  60. object ICollection.SyncRoot
  61. {
  62. get
  63. {
  64. return this;
  65. }
  66. }
  67. bool ICollection.IsSynchronized
  68. {
  69. get
  70. {
  71. return false;
  72. }
  73. }
  74. bool IList.IsFixedSize
  75. {
  76. get
  77. {
  78. return false;
  79. }
  80. }
  81. public bool IsReadOnly
  82. {
  83. get
  84. {
  85. return false;
  86. }
  87. }
  88. /// <summary>
  89. /// Adds an item to the List box. For an unsorted List box, the item is
  90. /// added to the end of the existing list of items. For a sorted List box,
  91. /// the item is inserted into the list according to its sorted position.
  92. /// The item's toString() method is called to obtain the string that is
  93. /// displayed in the List box.
  94. /// A SystemException occurs if there is insufficient space available to
  95. /// store the new item.
  96. /// </summary>
  97. public int Add(object item)
  98. {
  99. int index = AddInternal(item);
  100. return index;
  101. }
  102. private int AddInternal(object item)
  103. {
  104. if (item == null)
  105. {
  106. throw new ArgumentNullException(nameof(item));
  107. }
  108. int index = -1;
  109. if (!owner.sorted)
  110. {
  111. InnerArray.Add(item);
  112. }
  113. else
  114. {
  115. if (Count > 0)
  116. {
  117. index = InnerArray.BinarySearch(item);
  118. if (index < 0)
  119. {
  120. index = ~index; // getting the index of the first element that is larger than the search value
  121. //this index will be used for insert
  122. }
  123. }
  124. else
  125. {
  126. index = 0;
  127. }
  128. InnerArray.Insert(index, item);
  129. }
  130. bool successful = false;
  131. try
  132. {
  133. if (owner.sorted)
  134. {
  135. }
  136. else
  137. {
  138. index = Count - 1;
  139. }
  140. successful = true;
  141. }
  142. finally
  143. {
  144. if (!successful)
  145. {
  146. InnerArray.Remove(item);
  147. }
  148. }
  149. return index;
  150. }
  151. int IList.Add(object item)
  152. {
  153. return Add(item);
  154. }
  155. public void AddRange(ObjectCollection value)
  156. {
  157. AddRangeInternal((ICollection)value);
  158. }
  159. public void AddRange(object[] items)
  160. {
  161. AddRangeInternal((ICollection)items);
  162. }
  163. internal void AddRangeInternal(ICollection items)
  164. {
  165. if (items == null)
  166. {
  167. throw new ArgumentNullException(nameof(items));
  168. }
  169. try
  170. {
  171. foreach (object item in items)
  172. {
  173. // adding items one-by-one for performance
  174. // not using sort because after the array is sorted index of each newly added item will need to be found
  175. // AddInternal is based on BinarySearch and finds index without any additional cost
  176. AddInternal(item);
  177. }
  178. }
  179. finally
  180. {
  181. }
  182. }
  183. /// <summary>
  184. /// Retrieves the item with the specified index.
  185. /// </summary>
  186. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  187. public virtual object this[int index]
  188. {
  189. get
  190. {
  191. if (index < 0 || index >= InnerArray.GetCount(0))
  192. {
  193. throw new ArgumentOutOfRangeException();
  194. }
  195. return InnerArray.GetItem(index, 0);
  196. }
  197. set
  198. {
  199. SetItemInternal(index, value);
  200. }
  201. }
  202. /// <summary>
  203. /// Removes all items from the ListBox.
  204. /// </summary>
  205. public virtual void Clear()
  206. {
  207. ClearInternal();
  208. }
  209. /// <summary>
  210. /// Removes all items from the ListBox. Bypasses the data source check.
  211. /// </summary>
  212. internal void ClearInternal()
  213. {
  214. InnerArray.Clear();
  215. }
  216. public bool Contains(object value)
  217. {
  218. return IndexOf(value) != -1;
  219. }
  220. /// <summary>
  221. /// Copies the ListBox Items collection to a destination array.
  222. /// </summary>
  223. public void CopyTo(object[] destination, int arrayIndex)
  224. {
  225. int count = InnerArray.GetCount(0);
  226. for (int i = 0; i < count; i++)
  227. {
  228. destination[i + arrayIndex] = InnerArray.GetItem(i, 0);
  229. }
  230. }
  231. void ICollection.CopyTo(Array destination, int index)
  232. {
  233. int count = InnerArray.GetCount(0);
  234. for (int i = 0; i < count; i++)
  235. {
  236. destination.SetValue(InnerArray.GetItem(i, 0), i + index);
  237. }
  238. }
  239. /// <summary>
  240. /// Returns an enumerator for the ListBox Items collection.
  241. /// </summary>
  242. public IEnumerator GetEnumerator()
  243. {
  244. return InnerArray.GetEnumerator(0);
  245. }
  246. public int IndexOf(object value)
  247. {
  248. if (value == null)
  249. {
  250. throw new ArgumentNullException(nameof(value));
  251. }
  252. return InnerArray.IndexOf(value, 0);
  253. }
  254. internal int IndexOfIdentifier(object value)
  255. {
  256. if (value == null)
  257. {
  258. throw new ArgumentNullException(nameof(value));
  259. }
  260. return InnerArray.IndexOfIdentifier(value, 0);
  261. }
  262. /// <summary>
  263. /// Adds an item to the List box. For an unsorted List box, the item is
  264. /// added to the end of the existing list of items. For a sorted List box,
  265. /// the item is inserted into the list according to its sorted position.
  266. /// The item's toString() method is called to obtain the string that is
  267. /// displayed in the List box.
  268. /// A SystemException occurs if there is insufficient space available to
  269. /// store the new item.
  270. /// </summary>
  271. public void Insert(int index, object item)
  272. {
  273. if (index < 0 || index > InnerArray.GetCount(0))
  274. {
  275. throw new ArgumentOutOfRangeException();
  276. }
  277. if (item == null)
  278. {
  279. throw new ArgumentNullException(nameof(item));
  280. }
  281. // If the List box is sorted, then nust treat this like an add
  282. // because we are going to twiddle the index anyway.
  283. //
  284. if (owner.sorted)
  285. {
  286. Add(item);
  287. }
  288. else
  289. {
  290. InnerArray.Insert(index, item);
  291. }
  292. }
  293. /// <summary>
  294. /// Removes the given item from the ListBox, provided that it is
  295. /// actually in the list.
  296. /// </summary>
  297. public void Remove(object value)
  298. {
  299. int index = InnerArray.IndexOf(value, 0);
  300. if (index != -1)
  301. {
  302. RemoveAt(index);
  303. }
  304. }
  305. /// <summary>
  306. /// Removes an item from the ListBox at the given index.
  307. /// </summary>
  308. public void RemoveAt(int index)
  309. {
  310. if (index < 0 || index >= InnerArray.GetCount(0))
  311. {
  312. throw new ArgumentOutOfRangeException();
  313. }
  314. // Update InnerArray before calling NativeRemoveAt to ensure that when
  315. // SelectedIndexChanged is raised (by NativeRemoveAt), InnerArray's state matches wrapped LB state.
  316. InnerArray.RemoveAt(index);
  317. }
  318. internal void SetItemInternal(int index, object value)
  319. {
  320. if (value == null)
  321. {
  322. throw new ArgumentNullException(nameof(value));
  323. }
  324. if (index < 0 || index >= InnerArray.GetCount(0))
  325. {
  326. throw new ArgumentOutOfRangeException();
  327. }
  328. InnerArray.SetItem(index, value);
  329. }
  330. } // end ObjectCollection}
  331. }
  332. }