2024-11-28 00:02:40 +04:00
|
|
|
|
using Components.Exceptions;
|
2024-12-11 14:39:27 +04:00
|
|
|
|
using System.Windows.Forms;
|
2024-11-28 00:02:40 +04:00
|
|
|
|
|
|
|
|
|
namespace Components
|
2024-09-06 12:39:51 +04:00
|
|
|
|
{
|
2024-11-28 13:15:44 +04:00
|
|
|
|
public enum Types : int
|
|
|
|
|
{
|
|
|
|
|
Exception = 0,
|
|
|
|
|
DeleteFirst = 1,
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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-11-28 13:15:44 +04:00
|
|
|
|
private Types typeOfMaxChecked = Types.DeleteFirst;
|
2024-09-06 12:39:51 +04:00
|
|
|
|
private EventHandler _onCheckedItemChangedEvent;
|
2024-11-28 13:15:44 +04:00
|
|
|
|
public void ChangeTypeOfMaxChecked(Types type)
|
|
|
|
|
{
|
|
|
|
|
typeOfMaxChecked = type;
|
|
|
|
|
}
|
2024-09-06 12:39:51 +04:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-11 14:39:27 +04:00
|
|
|
|
public List<string> CheckedItemss
|
2024-11-28 13:15:44 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
List<string> list = new List<string>();
|
2024-12-11 14:39:27 +04:00
|
|
|
|
foreach (var item in checkedListBox.CheckedItems)
|
2024-11-28 13:15:44 +04:00
|
|
|
|
{
|
|
|
|
|
list.Add(item.ToString());
|
2024-12-11 14:39:27 +04:00
|
|
|
|
}
|
2024-11-28 13:15:44 +04:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2024-12-04 17:35:25 +04:00
|
|
|
|
foreach (var item in value)
|
2024-11-28 13:15:44 +04:00
|
|
|
|
{
|
2024-12-04 17:35:25 +04:00
|
|
|
|
if (checkedListBox.Items.Contains(item))
|
|
|
|
|
{
|
|
|
|
|
checkedListBox.SetItemCheckState(checkedListBox.Items.IndexOf(item), CheckState.Checked);
|
|
|
|
|
}
|
2024-11-28 13:15:44 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-06 12:39:51 +04:00
|
|
|
|
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-12-11 14:39:27 +04:00
|
|
|
|
this.BeginInvoke((MethodInvoker)(
|
|
|
|
|
() =>
|
2024-09-06 12:39:51 +04:00
|
|
|
|
{
|
2024-12-11 14:39:27 +04:00
|
|
|
|
if (checkedListBox.CheckedItems.Count > MaxCheckedItemsCount)
|
2024-11-28 13:15:44 +04:00
|
|
|
|
{
|
2024-12-11 14:39:27 +04:00
|
|
|
|
if (typeOfMaxChecked == Types.Exception)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Превышено максимальное число выбранных элементов");
|
|
|
|
|
checkedListBox.SetItemChecked(e.Index, false);
|
|
|
|
|
}
|
|
|
|
|
else if (typeOfMaxChecked == Types.DeleteFirst)
|
|
|
|
|
{
|
|
|
|
|
checkedListBox.SetItemCheckState(checkedListBox.Items.IndexOf(CheckedItem), CheckState.Unchecked);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 13:15:44 +04:00
|
|
|
|
}
|
2024-12-11 14:39:27 +04:00
|
|
|
|
_onCheckedItemChangedEvent?.Invoke(sender, e);
|
|
|
|
|
}));
|
2024-09-06 12:39:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|