From 48fc1a8443ad98f68f0cfcc153f7dae5db0dd56e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9F=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BF=D0=BE=D0=B2?= Date: Thu, 28 Nov 2024 00:02:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=B1=D1=80=D0=BE=D1=81=20=D0=B8=D1=81=D0=BA=D0=BB?= =?UTF-8?q?=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=BB=D1=8F=20Che?= =?UTF-8?q?ckedListBox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/UserControlCheckedList.cs | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) 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); } }