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

69 lines
1.8 KiB
C#
Raw Permalink Normal View History

2023-10-12 18:40:10 +04:00
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
{
string checkedItems = "";
for (int i = 0; i < checkedListBox.Items.Count; i++)
{
if (checkedListBox.GetItemChecked(i))
{
checkedItems += checkedListBox.Items[i].ToString() + " ";
}
}
if (checkedListBox.CheckedItems != null)
return checkedItems;
else
return "";
}
set
{
for (int i = 0; i < checkedListBox.Items.Count; i++)
{
if (checkedListBox.Items[i].ToString() == value)
{
checkedListBox.SetItemChecked(i, true);
break;
}
}
}
}
public event EventHandler SelectedValueChanged
{
add { _selectedValueChanged += value; }
remove { _selectedValueChanged += value; }
}
private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
{
_selectedValueChanged?.Invoke(this, e);
}
}
}