77 lines
1.8 KiB
C#
77 lines
1.8 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;
|
|
|
|
namespace ComponentProgramming
|
|
{
|
|
public partial class UserCheckedListBox : UserControl
|
|
{
|
|
private EventHandler _changeEvent;
|
|
public string SelectedElement
|
|
{
|
|
get
|
|
{
|
|
return checkedListBox.SelectedItem?.ToString() ?? string.Empty;
|
|
}
|
|
set
|
|
{
|
|
if (checkedListBox.Items.Contains(value))
|
|
{
|
|
checkedListBox.SelectedItem = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public event EventHandler ChangeEvent
|
|
{
|
|
add
|
|
{
|
|
_changeEvent += value;
|
|
}
|
|
remove
|
|
{
|
|
_changeEvent -= value;
|
|
}
|
|
|
|
}
|
|
public void Clear()
|
|
{
|
|
checkedListBox.Items.Clear();
|
|
}
|
|
public void AddToList(string Value)
|
|
{
|
|
if (Value == null)
|
|
{
|
|
return;
|
|
}
|
|
checkedListBox.Items.Add(Value);
|
|
}
|
|
public UserCheckedListBox()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|
|
_changeEvent?.Invoke(sender, e);
|
|
}
|
|
}
|
|
}
|