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)]
|
2023-09-21 14:27:34 +04:00
|
|
|
|
public string SelectedString
|
2023-09-13 09:58:42 +04:00
|
|
|
|
{
|
2023-09-21 14:27:34 +04:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (listBox.SelectedItems.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return listBox.SelectedItem.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-13 14:47:07 +04:00
|
|
|
|
|
2023-09-21 14:27:34 +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-21 14:27:34 +04:00
|
|
|
|
}
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 14:27:34 +04:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 14:27:34 +04:00
|
|
|
|
public void ClearAll()
|
2023-09-13 10:30:37 +04:00
|
|
|
|
{
|
|
|
|
|
listBox.Items.Clear();
|
|
|
|
|
}
|
2023-09-21 14:27:34 +04:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|