123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- using System.Collections;
- using System.ComponentModel;
- namespace System.Windows.Forms
- {
- public partial class ListBox
- {
- public class SelectedIndexCollection : IList
- {
- private readonly ListBox _owner;
- public SelectedIndexCollection(ListBox owner)
- {
- _owner = owner ?? throw new ArgumentNullException(nameof(owner));
- }
- /// <summary>
- /// Number of current selected items.
- /// </summary>
- [Browsable(false)]
- public int Count
- {
- get
- {
- return _owner.SelectedItems.Count;
- }
- }
- object ICollection.SyncRoot
- {
- get
- {
- return this;
- }
- }
- bool ICollection.IsSynchronized
- {
- get
- {
- return true;
- }
- }
- bool IList.IsFixedSize
- {
- get
- {
- return true;
- }
- }
- public bool IsReadOnly
- {
- get
- {
- return true;
- }
- }
- public bool Contains(int selectedIndex)
- {
- return IndexOf(selectedIndex) != -1;
- }
- bool IList.Contains(object selectedIndex)
- {
- if (selectedIndex is int)
- {
- return Contains((int)selectedIndex);
- }
- else
- {
- return false;
- }
- }
- public int IndexOf(int selectedIndex)
- {
- // Just what does this do? The selectedIndex parameter above is the index into the
- // main object collection. We look at the state of that item, and if the state indicates
- // that it is selected, we get back the virtualized index into this collection. Indexes on
- // this collection match those on the SelectedObjectCollection.
- if (selectedIndex >= 0 &&
- selectedIndex < InnerArray.GetCount(0) &&
- InnerArray.GetState(selectedIndex, SelectedObjectCollection.SelectedObjectMask))
- {
- return InnerArray.IndexOf(InnerArray.GetItem(selectedIndex, 0), SelectedObjectCollection.SelectedObjectMask);
- }
- return -1;
- }
- int IList.IndexOf(object selectedIndex)
- {
- if (selectedIndex is int)
- {
- return IndexOf((int)selectedIndex);
- }
- else
- {
- return -1;
- }
- }
- int IList.Add(object value)
- {
- throw new NotSupportedException();
- }
- void IList.Clear()
- {
- throw new NotSupportedException();
- }
- void IList.Insert(int index, object value)
- {
- throw new NotSupportedException();
- }
- void IList.Remove(object value)
- {
- throw new NotSupportedException();
- }
- void IList.RemoveAt(int index)
- {
- throw new NotSupportedException();
- }
- /// <summary>
- /// Retrieves the specified selected item.
- /// </summary>
- public int this[int index]
- {
- get
- {
- object identifier = InnerArray.GetEntryObject(index, SelectedObjectCollection.SelectedObjectMask);
- return InnerArray.IndexOfIdentifier(identifier, 0);
- }
- }
- object IList.this[int index]
- {
- get
- {
- return this[index];
- }
- set
- {
- throw new NotSupportedException();
- }
- }
- /// <summary>
- /// This is the item array that stores our data. We share this backing store
- /// with the main object collection.
- /// </summary>
- private ItemArray InnerArray
- {
- get
- {
- _owner.SelectedItems.EnsureUpToDate();
- return ((ObjectCollection)_owner.Items).InnerArray;
- }
- }
- public void CopyTo(Array destination, int index)
- {
- int cnt = Count;
- for (int i = 0; i < cnt; i++)
- {
- destination.SetValue(this[i], i + index);
- }
- }
- public void Clear()
- {
- if (_owner != null)
- {
- _owner.ClearSelected();
- }
- }
- public void Add(int index)
- {
- if (_owner != null)
- {
- ObjectCollection items = _owner.Items;
- if (items != null)
- {
- if (index != -1 && !Contains(index))
- {
- _owner.SetSelected(index, true);
- }
- }
- }
- }
- public void Remove(int index)
- {
- if (_owner != null)
- {
- ObjectCollection items = _owner.Items;
- if (items != null)
- {
- if (index != -1 && Contains(index))
- {
- _owner.SetSelected(index, false);
- }
- }
- }
- }
- public IEnumerator GetEnumerator()
- {
- return new SelectedIndexEnumerator(this);
- }
- /// <summary>
- /// EntryEnumerator is an enumerator that will enumerate over
- /// a given state mask.
- /// </summary>
- private class SelectedIndexEnumerator : IEnumerator
- {
- private readonly SelectedIndexCollection items;
- private int current;
- /// <summary>
- /// Creates a new enumerator that will enumerate over the given state.
- /// </summary>
- public SelectedIndexEnumerator(SelectedIndexCollection items)
- {
- this.items = items;
- current = -1;
- }
- /// <summary>
- /// Moves to the next element, or returns false if at the end.
- /// </summary>
- bool IEnumerator.MoveNext()
- {
- if (current < items.Count - 1)
- {
- current++;
- return true;
- }
- else
- {
- current = items.Count;
- return false;
- }
- }
- /// <summary>
- /// Resets the enumeration back to the beginning.
- /// </summary>
- void IEnumerator.Reset()
- {
- current = -1;
- }
- /// <summary>
- /// Retrieves the current value in the enumerator.
- /// </summary>
- object IEnumerator.Current
- {
- get
- {
- if (current == -1 || current == items.Count)
- {
- throw new InvalidOperationException();
- }
- return items[current];
- }
- }
- }
- }
- }
- }
|