Merge pull request 'lab2_fix' (#2) from lab2_fix into lab2

Reviewed-on: #2
This commit is contained in:
Никита Потапов 2024-11-28 00:04:17 +04:00
commit c4d07c4ee2
3 changed files with 20 additions and 16 deletions

View File

@ -7,7 +7,7 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Authors>ns.potapov</Authors> <Authors>ns.potapov</Authors>
<VersionPrefix>1.0.3</VersionPrefix> <VersionPrefix>1.0.4</VersionPrefix>
<Version>$(VersionPrefix)</Version> <Version>$(VersionPrefix)</Version>
</PropertyGroup> </PropertyGroup>

View File

@ -0,0 +1,13 @@
namespace Components.Exceptions
{
internal class MaxCheckedItemsCountExceedException : Exception
{
public MaxCheckedItemsCountExceedException()
{
}
public MaxCheckedItemsCountExceedException(string? message) : base(message)
{
}
}
}

View File

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