KOP/Components/UserControlStringsListBox.cs

71 lines
1.8 KiB
C#
Raw Normal View History

2024-10-03 23:07:05 +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 WinFormsLibrary1
{
public partial class UserControlStringsListBox : UserControl
{
private event EventHandler? _listChanged;
private event Action? _errorOccured;
public string Error { get; private set; }
public string ListElement
{
get
{
return listBoxStrings.SelectedItem?.ToString();
}
set
{
if (listBoxStrings.Items.Contains(value))
{
listBoxStrings.SelectedItem = value;
}
}
}
public event EventHandler ListChanged
{
add { _listChanged += value; }
remove { _listChanged -= value; }
}
public event Action AnErrorOccurred
{
add { _errorOccured += value; }
remove { _errorOccured -= value; }
}
public UserControlStringsListBox()
{
InitializeComponent();
Error = string.Empty;
}
public void AddList(List<string> l)
{
try
{
listBoxStrings.Items.AddRange(l.ToArray());
}
catch (Exception ex)
{
Error = ex.Message;
_errorOccured?.Invoke();
}
}
public void ClearList()
{
listBoxStrings.Items.Clear();
}
private void listBoxStrings_SelectedValueChanged(object sender, EventArgs e)
{
_listChanged?.Invoke(sender, e);
}
}
}