2024-09-06 12:39:51 +04:00
|
|
|
|
namespace Components
|
|
|
|
|
{
|
|
|
|
|
public partial class UserControlCheckedList : UserControl
|
|
|
|
|
{
|
2024-11-27 23:40:10 +04:00
|
|
|
|
private List<int> _lastCheckedIndexes = new List<int>();
|
|
|
|
|
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();
|
2024-11-27 23:40:10 +04:00
|
|
|
|
_lastCheckedIndexes.Clear();
|
2024-09-06 12:39:51 +04:00
|
|
|
|
}
|
|
|
|
|
private void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
|
|
|
|
|
{
|
2024-11-27 23:40:10 +04:00
|
|
|
|
if (e.NewValue == CheckState.Checked)
|
2024-09-06 12:39:51 +04:00
|
|
|
|
{
|
2024-11-27 23:40:10 +04:00
|
|
|
|
_lastCheckedIndexes.Add(e.Index);
|
|
|
|
|
|
|
|
|
|
while (_lastCheckedIndexes.Count > MaxCheckedItemsCount)
|
2024-09-06 12:39:51 +04:00
|
|
|
|
{
|
2024-11-27 23:40:10 +04:00
|
|
|
|
checkedListBox.SetItemCheckState(_lastCheckedIndexes[0], CheckState.Unchecked);
|
2024-09-06 12:39:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-27 23:40:10 +04:00
|
|
|
|
else if (e.NewValue == CheckState.Unchecked)
|
|
|
|
|
{
|
|
|
|
|
_lastCheckedIndexes.Remove(e.Index);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 12:39:51 +04:00
|
|
|
|
_onCheckedItemChangedEvent?.Invoke(sender, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|