COP-17/WinFormsProject/WinFormsLibrary/CustomCheckedListBox.cs

79 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WinFormsLibrary
{
public partial class CustomCheckedListBox : UserControl
{
public CustomCheckedListBox()
{
InitializeComponent();
}
//метод для очистки
public void Clear()
{
checkedListBox.Items.Clear();
}
//метод для заполнения
public void FillCheckedListBox(List<string> items)
{
checkedListBox.Items.Clear();
checkedListBox.Items.AddRange(items.ToArray());
}
//публичное свойство
public string Selected
{
get
{
if (checkedListBox.SelectedItems.Count == 0)
{
return "";
}
string result = "";
foreach (object itemChecked in checkedListBox.CheckedItems)
{
result += itemChecked.ToString() + Environment.NewLine;
}
return result;
}
set
{
if (checkedListBox.Items.Contains(value))
{
checkedListBox.SelectedItem = value;
}
}
}
private EventHandler onValueChanged;
public event EventHandler ValueChanged
{
add
{
onValueChanged += value;
}
remove
{
onValueChanged -= value;
}
}
private void checkedListBox_SelectedValueChanged(object sender, EventArgs e)
{
onValueChanged?.Invoke(sender, e);
}
}
}