ComboBox.ObjectCollection.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 ComboBox
  11. {
  12. public class ObjectCollection : IList
  13. {
  14. private readonly ComboBox owner;
  15. private ArrayList innerList;
  16. private IComparer comparer;
  17. public ObjectCollection(ComboBox owner)
  18. {
  19. this.owner = owner;
  20. }
  21. private IComparer Comparer
  22. {
  23. get
  24. {
  25. if (comparer == null)
  26. {
  27. comparer = new ItemComparer(owner);
  28. }
  29. return comparer;
  30. }
  31. }
  32. private ArrayList InnerList
  33. {
  34. get
  35. {
  36. if (innerList == null)
  37. {
  38. innerList = new ArrayList();
  39. }
  40. return innerList;
  41. }
  42. }
  43. /// <summary>
  44. /// Retrieves the number of items.
  45. /// </summary>
  46. public int Count
  47. {
  48. get
  49. {
  50. return InnerList.Count;
  51. }
  52. }
  53. object ICollection.SyncRoot
  54. {
  55. get
  56. {
  57. return this;
  58. }
  59. }
  60. bool ICollection.IsSynchronized
  61. {
  62. get
  63. {
  64. return false;
  65. }
  66. }
  67. bool IList.IsFixedSize
  68. {
  69. get
  70. {
  71. return false;
  72. }
  73. }
  74. public bool IsReadOnly
  75. {
  76. get
  77. {
  78. return false;
  79. }
  80. }
  81. /// <summary>
  82. /// Adds an item to the combo box. For an unsorted combo box, the item is
  83. /// added to the end of the existing list of items. For a sorted combo box,
  84. /// the item is inserted into the list according to its sorted position.
  85. /// The item's toString() method is called to obtain the string that is
  86. /// displayed in the combo box.
  87. /// A SystemException occurs if there is insufficient space available to
  88. /// store the new item.
  89. /// </summary>
  90. public int Add(object item)
  91. {
  92. int index = AddInternal(item);
  93. return index;
  94. }
  95. private int AddInternal(object item)
  96. {
  97. if (item == null)
  98. {
  99. throw new ArgumentNullException(nameof(item));
  100. }
  101. int index = -1;
  102. if (!owner.sorted)
  103. {
  104. InnerList.Add(item);
  105. }
  106. else
  107. {
  108. index = InnerList.BinarySearch(item, Comparer);
  109. if (index < 0)
  110. {
  111. index = ~index; // getting the index of the first element that is larger than the search value
  112. }
  113. InnerList.Insert(index, item);
  114. }
  115. bool successful = false;
  116. try
  117. {
  118. if (owner.sorted)
  119. {
  120. }
  121. else
  122. {
  123. index = InnerList.Count - 1;
  124. }
  125. successful = true;
  126. }
  127. finally
  128. {
  129. if (!successful)
  130. {
  131. InnerList.Remove(item);
  132. }
  133. }
  134. return index;
  135. }
  136. int IList.Add(object item)
  137. {
  138. return Add(item);
  139. }
  140. /// <summary>
  141. /// Retrieves the item with the specified index.
  142. /// </summary>
  143. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  144. public virtual object this[int index]
  145. {
  146. get
  147. {
  148. if (index < 0 || index >= InnerList.Count)
  149. {
  150. throw new ArgumentOutOfRangeException();
  151. }
  152. return InnerList[index];
  153. }
  154. set
  155. {
  156. SetItemInternal(index, value);
  157. }
  158. }
  159. public void AddRange(object[] items)
  160. {
  161. try
  162. {
  163. AddRangeInternal(items);
  164. }
  165. finally
  166. {
  167. }
  168. }
  169. internal void AddRangeInternal(IList items)
  170. {
  171. if (items == null)
  172. {
  173. throw new ArgumentNullException(nameof(items));
  174. }
  175. foreach (object item in items)
  176. {
  177. // adding items one-by-one for performance (especially for sorted combobox)
  178. // we can not rely on ArrayList.Sort since its worst case complexity is n*n
  179. // AddInternal is based on BinarySearch and ensures n*log(n) complexity
  180. AddInternal(item);
  181. }
  182. }
  183. /// <summary>
  184. /// Removes all items from the ComboBox.
  185. /// </summary>
  186. public void Clear()
  187. {
  188. ClearInternal();
  189. }
  190. internal void ClearInternal()
  191. {
  192. InnerList.Clear();
  193. owner.selectedIndex = -1;
  194. }
  195. public bool Contains(object value)
  196. {
  197. return IndexOf(value) != -1;
  198. }
  199. /// <summary>
  200. /// Copies the ComboBox Items collection to a destination array.
  201. /// </summary>
  202. public void CopyTo(object[] destination, int arrayIndex)
  203. {
  204. InnerList.CopyTo(destination, arrayIndex);
  205. }
  206. void ICollection.CopyTo(Array destination, int index)
  207. {
  208. InnerList.CopyTo(destination, index);
  209. }
  210. /// <summary>
  211. /// Returns an enumerator for the ComboBox Items collection.
  212. /// </summary>
  213. public IEnumerator GetEnumerator()
  214. {
  215. return InnerList.GetEnumerator();
  216. }
  217. public int IndexOf(object value)
  218. {
  219. if (value == null)
  220. {
  221. throw new ArgumentNullException(nameof(value));
  222. }
  223. return InnerList.IndexOf(value);
  224. }
  225. /// <summary>
  226. /// Adds an item to the combo box. For an unsorted combo box, the item is
  227. /// added to the end of the existing list of items. For a sorted combo box,
  228. /// the item is inserted into the list according to its sorted position.
  229. /// The item's toString() method is called to obtain the string that is
  230. /// displayed in the combo box.
  231. /// A SystemException occurs if there is insufficient space available to
  232. /// store the new item.
  233. /// </summary>
  234. public void Insert(int index, object item)
  235. {
  236. if (item == null)
  237. {
  238. throw new ArgumentNullException(nameof(item));
  239. }
  240. if (index < 0 || index > InnerList.Count)
  241. {
  242. throw new ArgumentOutOfRangeException();
  243. }
  244. // If the combo box is sorted, then nust treat this like an add
  245. // because we are going to twiddle the index anyway.
  246. //
  247. if (owner.sorted)
  248. {
  249. Add(item);
  250. }
  251. else
  252. {
  253. InnerList.Insert(index, item);
  254. }
  255. }
  256. /// <summary>
  257. /// Removes an item from the ComboBox at the given index.
  258. /// </summary>
  259. public void RemoveAt(int index)
  260. {
  261. if (index < 0 || index >= InnerList.Count)
  262. {
  263. throw new ArgumentOutOfRangeException();
  264. }
  265. InnerList.RemoveAt(index);
  266. if (index < owner.selectedIndex)
  267. {
  268. owner.selectedIndex--;
  269. }
  270. }
  271. /// <summary>
  272. /// Removes the given item from the ComboBox, provided that it is
  273. /// actually in the list.
  274. /// </summary>
  275. public void Remove(object value)
  276. {
  277. int index = InnerList.IndexOf(value);
  278. if (index != -1)
  279. {
  280. RemoveAt(index);
  281. }
  282. }
  283. internal void SetItemInternal(int index, object value)
  284. {
  285. if (index < 0 || index >= InnerList.Count)
  286. {
  287. throw new ArgumentOutOfRangeException();
  288. }
  289. InnerList[index] = value ?? throw new ArgumentNullException(nameof(value));
  290. }
  291. }
  292. private sealed class ItemComparer : IComparer
  293. {
  294. private readonly ComboBox comboBox;
  295. public ItemComparer(ComboBox comboBox)
  296. {
  297. this.comboBox = comboBox;
  298. }
  299. public int Compare(object item1, object item2)
  300. {
  301. if (item1 == null)
  302. {
  303. if (item2 == null)
  304. {
  305. return 0; //both null, then they are equal
  306. }
  307. return -1; //item1 is null, but item2 is valid (greater)
  308. }
  309. if (item2 == null)
  310. {
  311. return 1; //item2 is null, so item 1 is greater
  312. }
  313. string itemName1 = comboBox.GetItemText(item1);
  314. string itemName2 = comboBox.GetItemText(item2);
  315. CompareInfo compInfo = CultureInfo.CurrentCulture.CompareInfo;
  316. return compInfo.Compare(itemName1, itemName2, CompareOptions.StringSort);
  317. }
  318. }
  319. }
  320. }