PIbd-32_Isaeva_A.I._COP_2/CustomCheckedListBox.cs

70 lines
1.5 KiB
C#

namespace CustomsComponentsVar2;
public partial class CustomCheckedListBox : UserControl
{
private event EventHandler? _selectedChanged;
public CustomCheckedListBox()
{
InitializeComponent();
}
public string SelectedElement
{
get
{
if (checkedListBox.Items.Count == 0 || checkedListBox.Items == null)
{
return String.Empty;
}
return checkedListBox.Items.ToString();
}
set
{
if (checkedListBox.Items.Contains(value))
{
checkedListBox.SelectedItem = value;
}
}
}
/// <summary>
/// Добавить элементы
/// </summary>
/// <param name="s"></param>
public void addElements(List<string> s)
{
checkedListBox.Items.Clear();
checkedListBox.Items.AddRange(s.ToArray());
}
/// <summary>
/// Выбран элемент
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void onItemCheck(object sender, ItemCheckEventArgs e)
{
_selectedChanged?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Изменение индекса выбранного элемента
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkedListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (checkedListBox.CheckedItems.Count > 1)
{
foreach (int index in checkedListBox.CheckedIndices)
{
if (index != checkedListBox.SelectedIndex)
{
checkedListBox.SetItemChecked(index, false);
}
}
}
_selectedChanged?.Invoke(sender, e);
}
}