PIbd-32_Artamonova_T.V._COP_2/COP/UserCheckedListBox.cs

60 lines
1.6 KiB
C#

using DocumentFormat.OpenXml.Spreadsheet;
namespace COP
{
public partial class UserCheckedListBox : UserControl
{
private event EventHandler? _selectedValueChanged;
public UserCheckedListBox()
{
InitializeComponent();
}
public void FillList(List<string> items)
{
if (items.Count == 0 || items is null)
{
return;
}
checkedListBox.Items.Clear();
checkedListBox.Items.AddRange(items.ToArray());
}
public void ClearList()
{
checkedListBox.Items.Clear();
}
public string SelectedValue
{
get
{
return string.Join(";", checkedListBox.CheckedItems.Cast<string>());
}
set
{
if (!string.IsNullOrEmpty(value))
{
string[] selectedValues = value.Split(';');
for (int i = 0; i < checkedListBox.Items.Count; i++)
{
checkedListBox.SetItemChecked(i, selectedValues.Contains(checkedListBox.Items[i].ToString()) || checkedListBox.GetItemChecked(i));
}
}
}
}
public event EventHandler SelectedValueChanged
{
add { _selectedValueChanged += value; }
remove { _selectedValueChanged += value; }
}
private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
{
_selectedValueChanged?.Invoke(this, e);
}
}
}