-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathListBox.ObjectCollection.cs
More file actions
448 lines (378 loc) · 14 KB
/
Copy pathListBox.ObjectCollection.cs
File metadata and controls
448 lines (378 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using static System.Windows.Forms.ItemArray;
namespace System.Windows.Forms;
public partial class ListBox
{
/// <summary>
/// A collection that stores objects.
/// </summary>
[ListBindable(false)]
public class ObjectCollection : IList
{
private readonly ListBox _owner;
private ItemArray _items = null!;
public ObjectCollection(ListBox owner)
{
_owner = owner.OrThrowIfNull();
}
/// <summary>
/// Initializes a new instance of ListBox.ObjectCollection based on another ListBox.ObjectCollection.
/// </summary>
public ObjectCollection(ListBox owner, ObjectCollection value)
: this(owner)
{
ArgumentNullException.ThrowIfNull(value);
AddRange(value);
}
/// <summary>
/// Initializes a new instance of ListBox.ObjectCollection containing any array of objects.
/// </summary>
public ObjectCollection(ListBox owner, object[] value)
: this(owner)
{
ArgumentNullException.ThrowIfNull(value);
AddRange(value);
}
/// <summary>
/// Retrieves the number of items.
/// </summary>
public int Count => InnerArray.Count;
/// <summary>
/// Internal access to the actual data store.
/// </summary>
internal ItemArray InnerArray
{
get
{
_items ??= new ItemArray(_owner);
return _items;
}
}
object ICollection.SyncRoot => this;
bool ICollection.IsSynchronized => false;
bool IList.IsFixedSize => false;
public bool IsReadOnly => false;
/// <summary>
/// Adds an item to the List box. For an unsorted List box, the item is
/// added to the end of the existing list of items. For a sorted List box,
/// the item is inserted into the list according to its sorted position.
/// The item's toString() method is called to obtain the string that is
/// displayed in the List box.
/// A SystemException occurs if there is insufficient space available to
/// store the new item.
/// </summary>
public int Add(object item)
{
_owner.CheckNoDataSource();
int index = AddInternal(item);
_owner.UpdateHorizontalExtent();
return index;
}
private int AddInternal(object item)
{
ArgumentNullException.ThrowIfNull(item);
int index = -1;
if (!_owner._sorted)
{
InnerArray.Add(item);
}
else
{
Entry entry = GetEntry(item);
if (Count > 0)
{
index = InnerArray.BinarySearch(entry);
if (index < 0)
{
// getting the index of the first element that is larger than the search value
// this index will be used for insert
index = ~index;
}
}
else
{
index = 0;
}
Debug.Assert(index >= 0 && index <= Count, "Wrong index for insert");
InnerArray.InsertEntry(index, entry);
}
bool successful = false;
try
{
if (_owner._sorted)
{
if (_owner.IsHandleCreated)
{
_owner.NativeInsert(index, item);
_owner.UpdateMaxItemWidth(item, false);
// Sorting may throw the LB contents and the selectedItem array out of synch.
_owner._selectedItems?.Dirty();
}
}
else
{
index = Count - 1;
if (_owner.IsHandleCreated)
{
_owner.NativeAdd(item);
_owner.UpdateMaxItemWidth(item, false);
}
}
successful = true;
}
finally
{
if (!successful)
{
InnerArray.Remove(item);
}
}
return index;
}
int IList.Add(object? item) => Add(item!);
public void AddRange(ObjectCollection value)
{
ArgumentNullException.ThrowIfNull(value);
_owner.CheckNoDataSource();
AddRangeInternal(value);
}
public void AddRange(params object[] items)
{
ArgumentNullException.ThrowIfNull(items);
_owner.CheckNoDataSource();
AddRangeInternal(items);
}
internal void AddRangeInternal(ICollection items)
{
Debug.Assert(items is not null);
_owner.BeginUpdate();
try
{
foreach (object item in items)
{
// adding items one-by-one for performance
// not using sort because after the array is sorted index of each newly added item will need to be found
// AddInternal is based on BinarySearch and finds index without any additional cost
AddInternal(item);
}
}
finally
{
_owner.UpdateHorizontalExtent();
_owner.EndUpdate();
}
}
/// <summary>
/// Retrieves the item with the specified index.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual object this[int index]
{
get
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, InnerArray.Count);
return InnerArray.GetItem(index);
}
set
{
_owner.CheckNoDataSource();
SetItemInternal(index, value);
}
}
object? IList.this[int index]
{
get => this[index];
set => this[index] = value!;
}
/// <summary>
/// Removes all items from the ListBox.
/// </summary>
public virtual void Clear()
{
_owner.CheckNoDataSource();
ClearInternal();
}
/// <summary>
/// Removes all items from the ListBox. Bypasses the data source check.
/// </summary>
internal void ClearInternal()
{
// update the width.. to reset Scrollbars..
// Clear the selection state.
int cnt = _owner.Items.Count;
for (int i = 0; i < cnt; i++)
{
_owner.UpdateMaxItemWidth(InnerArray.GetItem(i), true);
}
if (_owner.IsHandleCreated)
{
_owner.NativeClear();
}
InnerArray.Clear();
_owner._maxWidth = -1;
_owner.UpdateHorizontalExtent();
_owner.ClearListItemAccessibleObjects();
}
public bool Contains(object value)
{
return IndexOf(value) != -1;
}
bool IList.Contains(object? value) => Contains(value!);
/// <summary>
/// Copies the ListBox Items collection to a destination array.
/// </summary>
public void CopyTo(object[] destination, int arrayIndex)
{
ArgumentNullException.ThrowIfNull(destination);
int count = InnerArray.Count;
for (int i = 0; i < count; i++)
{
destination[i + arrayIndex] = InnerArray.GetItem(i);
}
}
void ICollection.CopyTo(Array destination, int index)
{
ArgumentNullException.ThrowIfNull(destination);
int count = InnerArray.Count;
for (int i = 0; i < count; i++)
{
destination.SetValue(InnerArray.GetItem(i), i + index);
}
}
/// <summary>
/// Returns an enumerator for the ListBox Items collection.
/// </summary>
public IEnumerator GetEnumerator() => InnerArray.GetEnumerator(0);
public int IndexOf(object value)
{
ArgumentNullException.ThrowIfNull(value);
return InnerArray.IndexOf(value);
}
int IList.IndexOf(object? value) => IndexOf(value!);
internal int IndexOfIdentifier(object value)
{
ArgumentNullException.ThrowIfNull(value);
return InnerArray.IndexOf(value);
}
/// <summary>
/// Adds an item to the List box. For an unsorted List box, the item is
/// added to the end of the existing list of items. For a sorted List box,
/// the item is inserted into the list according to its sorted position.
/// The item's toString() method is called to obtain the string that is
/// displayed in the List box.
/// A SystemException occurs if there is insufficient space available to
/// store the new item.
/// </summary>
public void Insert(int index, object item)
{
_owner.CheckNoDataSource();
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThan(index, InnerArray.Count);
ArgumentNullException.ThrowIfNull(item);
// If the List box is sorted, then nust treat this like an add
// because we are going to twiddle the index anyway.
if (_owner._sorted)
{
Add(item);
}
else
{
InnerArray.Insert(index, item);
if (_owner.IsHandleCreated)
{
bool successful = false;
try
{
_owner.NativeInsert(index, item);
_owner.UpdateMaxItemWidth(item, false);
successful = true;
}
finally
{
if (!successful)
{
InnerArray.RemoveAt(index);
}
}
}
}
_owner.UpdateHorizontalExtent();
}
void IList.Insert(int index, object? item) => Insert(index, item!);
/// <summary>
/// Removes the given item from the ListBox, provided that it is
/// actually in the list.
/// </summary>
public void Remove(object value)
{
_owner.CheckNoDataSource();
int index = InnerArray.IndexOf(value);
if (index != -1)
{
RemoveAt(index);
}
}
void IList.Remove(object? value) => Remove(value!);
/// <summary>
/// Removes an item from the ListBox at the given index.
/// </summary>
public void RemoveAt(int index)
{
_owner.CheckNoDataSource();
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, InnerArray.Count);
_owner.UpdateMaxItemWidth(InnerArray.GetItem(index), true);
// Remove AccessibleObject before removing item from InnerArray because AccessibleObject relies on
// item's presence in InnerArray
_owner.RemoveListItemAccessibleObjectAt(index);
// Update InnerArray before calling NativeRemoveAt to ensure that when
// SelectedIndexChanged is raised (by NativeRemoveAt), InnerArray's state matches wrapped LB state.
InnerArray.RemoveAt(index);
if (_owner.IsHandleCreated)
{
_owner.NativeRemoveAt(index);
}
_owner.UpdateHorizontalExtent();
}
internal void SetItemInternal(int index, object value)
{
ArgumentNullException.ThrowIfNull(value);
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, InnerArray.Count);
_owner.UpdateMaxItemWidth(InnerArray.GetItem(index), true);
InnerArray.SetItem(index, value);
// If the native control has been created, and the display text of the new list item object
// is different to the current text in the native list item, recreate the native list item...
if (_owner.IsHandleCreated)
{
bool selected = (_owner.SelectedIndex == index);
if (string.Compare(_owner.GetItemText(value), _owner.NativeGetItemText(index), true, CultureInfo.CurrentCulture) != 0)
{
_owner.NativeRemoveAt(index);
_owner.SelectedItems.SetSelected(index, false);
_owner.NativeInsert(index, value);
_owner.UpdateMaxItemWidth(value, false);
if (selected)
{
_owner.SelectedIndex = index;
}
}
else
{
// FOR COMPATIBILITY REASONS
if (selected)
{
_owner.OnSelectedIndexChanged(EventArgs.Empty); // will fire selectedvaluechanged
}
}
}
_owner.UpdateHorizontalExtent();
}
}
}