PIbd-31_Afanasev.S.S._COP_5/Component/ComponentProgramming/ControlListBox.cs

70 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;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace ComponentProgramming
{
public partial class ControlListBox : UserControl
{
private event EventHandler? _listBoxChanged;
public string SelectedItem
{
get
{
if (listBoxElements.SelectedItem == null)
{
return "";
}
return listBoxElements.SelectedItem.ToString()!;
}
set
{
listBoxElements.SelectedItem = value;
}
}
public event EventHandler ListBoxChanged
{
add { _listBoxChanged += value; }
remove { _listBoxChanged -= value; }
}
public ControlListBox()
{
InitializeComponent();
}
public ListBox.ObjectCollection ListBoxItems
{
get { return listBoxElements.Items; }
}
public void ClearListBox()
{
listBoxElements.Items.Clear();
}
public void FillListBox(List<string> items)
{
listBoxElements.Items.Clear();
foreach (var item in items)
{
listBoxElements.Items.Add(item);
}
}
private void listBoxElements_SelectedIndexChanged(object sender, EventArgs e)
{
_listBoxChanged?.Invoke(this, e);
}
}
}