2024-11-28 00:02:40 +04:00
|
|
|
|
using Components.Exceptions;
|
|
|
|
|
|
|
|
|
|
namespace Components
|
2024-09-06 12:39:51 +04:00
|
|
|
|
{
|
|
|
|
|
public partial class UserControlCheckedList : UserControl
|
|
|
|
|
{
|
2024-11-27 23:40:10 +04:00
|
|
|
|
public int MaxCheckedItemsCount { get; set; } = 1;
|
2024-09-06 12:39:51 +04:00
|
|
|
|
private EventHandler _onCheckedItemChangedEvent;
|
|
|
|
|
public event EventHandler CheckedItemChanged
|
|
|
|
|
{
|
|
|
|
|
add
|
|
|
|
|
{
|
|
|
|
|
_onCheckedItemChangedEvent += value;
|
|
|
|
|
}
|
|
|
|
|
remove
|
|
|
|
|
{
|
|
|
|
|
_onCheckedItemChangedEvent -= value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public string CheckedItem
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return checkedListBox.CheckedItems[0] as string ?? string.Empty;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (checkedListBox.Items.Contains(value))
|
|
|
|
|
{
|
|
|
|
|
checkedListBox.SetItemCheckState(checkedListBox.Items.IndexOf(value), CheckState.Checked);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public UserControlCheckedList()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
public void SetCheckedListBoxValues(List<string> values)
|
|
|
|
|
{
|
|
|
|
|
ClearCheckedListBoxValues();
|
|
|
|
|
foreach (var item in values)
|
|
|
|
|
{
|
|
|
|
|
checkedListBox.Items.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public void ClearCheckedListBoxValues()
|
|
|
|
|
{
|
|
|
|
|
checkedListBox.Items.Clear();
|
|
|
|
|
}
|
|
|
|
|
private void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
|
|
|
|
|
{
|
2024-11-28 00:02:40 +04:00
|
|
|
|
if (checkedListBox.CheckedItems.Count > MaxCheckedItemsCount)
|
2024-09-06 12:39:51 +04:00
|
|
|
|
{
|
2024-11-28 00:02:40 +04:00
|
|
|
|
checkedListBox.SetItemChecked(e.Index, false);
|
|
|
|
|
throw new MaxCheckedItemsCountExceedException("Превышено максимальное число выбранных элементов");
|
2024-09-06 12:39:51 +04:00
|
|
|
|
}
|
|
|
|
|
_onCheckedItemChangedEvent?.Invoke(sender, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|