diff --git a/WinFormSolution/Components/UserControlCheckedList.cs b/WinFormSolution/Components/UserControlCheckedList.cs index e18ff56..c37cf14 100644 --- a/WinFormSolution/Components/UserControlCheckedList.cs +++ b/WinFormSolution/Components/UserControlCheckedList.cs @@ -1,8 +1,9 @@ -namespace Components +using Components.Exceptions; + +namespace Components { public partial class UserControlCheckedList : UserControl { - private List _lastCheckedIndexes = new List(); public int MaxCheckedItemsCount { get; set; } = 1; private EventHandler _onCheckedItemChangedEvent; public event EventHandler CheckedItemChanged @@ -45,24 +46,14 @@ public void ClearCheckedListBoxValues() { checkedListBox.Items.Clear(); - _lastCheckedIndexes.Clear(); } private void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e) { - if (e.NewValue == CheckState.Checked) + if (checkedListBox.CheckedItems.Count > MaxCheckedItemsCount) { - _lastCheckedIndexes.Add(e.Index); - - while (_lastCheckedIndexes.Count > MaxCheckedItemsCount) - { - checkedListBox.SetItemCheckState(_lastCheckedIndexes[0], CheckState.Unchecked); - } + checkedListBox.SetItemChecked(e.Index, false); + throw new MaxCheckedItemsCountExceedException("Превышено максимальное число выбранных элементов"); } - else if (e.NewValue == CheckState.Unchecked) - { - _lastCheckedIndexes.Remove(e.Index); - } - _onCheckedItemChangedEvent?.Invoke(sender, e); } }