VisualComponents/VisualComponentsLib/MyListBox.cs

72 lines
1.6 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 VisualComponentsLib.CustomListBox
{
[DefaultEvent(nameof(TextChanged))]
public partial class MyListBox : UserControl
{
[Browsable(true)]
public string SelectedString
{
get
{
if (listBox.SelectedItems.Count == 0)
{
return null;
}
else
{
return listBox.SelectedItem.ToString();
}
}
set
{
if (!string.IsNullOrEmpty(value) && listBox.Items.IndexOf(value) != -1)
{
listBox.SelectedItem = value;
}
}
}
public MyListBox()
{
InitializeComponent();
}
public void AddItem(string item)
{
if (!string.IsNullOrEmpty(item))
{
listBox.Items.Add(item);
}
}
public void ClearAll()
{
listBox.Items.Clear();
}
private EventHandler _textChanged;
public event EventHandler? TextChanged
{
add => _textChanged += value;
remove => _textChanged -= value;
}
private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
_textChanged?.Invoke(sender, e);
}
}
}