VisualComponents/VisualComponentsLib/MyListBox.cs

72 lines
1.6 KiB
C#
Raw Normal View History

2023-09-13 09:58:42 +04:00
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))]
2023-09-13 13:48:16 +04:00
public partial class MyListBox : UserControl
2023-09-13 09:58:42 +04:00
{
[Browsable(true)]
public string SelectedString
2023-09-13 09:58:42 +04:00
{
get
{
if (listBox.SelectedItems.Count == 0)
{
return null;
}
else
{
return listBox.SelectedItem.ToString();
}
}
2023-09-13 14:47:07 +04:00
set
2023-09-13 14:47:07 +04:00
{
if (!string.IsNullOrEmpty(value) && listBox.Items.IndexOf(value) != -1)
{
listBox.SelectedItem = value;
}
}
2023-09-13 09:58:42 +04:00
}
2023-09-13 13:48:16 +04:00
public MyListBox()
2023-09-13 09:58:42 +04:00
{
InitializeComponent();
}
public void AddItem(string item)
2023-09-13 10:30:37 +04:00
{
2023-09-13 14:47:07 +04:00
if (!string.IsNullOrEmpty(item))
2023-09-13 10:30:37 +04:00
{
2023-09-13 14:47:07 +04:00
listBox.Items.Add(item);
2023-09-13 10:30:37 +04:00
}
}
public void ClearAll()
2023-09-13 10:30:37 +04:00
{
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);
}
2023-09-13 09:58:42 +04:00
}
}