52 lines
1.4 KiB
C#
52 lines
1.4 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 WinFormsControlLibrary1;
|
|
|
|
|
|
public partial class ComboSingleAddControl : UserControl
|
|
{
|
|
private readonly ComboBox _combo = new() { Dock = DockStyle.Fill };
|
|
public EventHandler? SelectedValueChanged;
|
|
public ComboSingleAddControl()
|
|
{
|
|
InitializeComponent();
|
|
Controls.Add(_combo);
|
|
_combo.SelectedIndexChanged += (_, __) => SelectedValueChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
|
|
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
|
|
public string SelectedValue
|
|
{
|
|
get => _combo.SelectedItem.ToString() ?? string.Empty;
|
|
set
|
|
{
|
|
if(string.IsNullOrEmpty(value)) { _combo.SelectedIndex = -1; return; }
|
|
int id = _combo.Items.IndexOf(value);
|
|
if(id >= 0) _combo.SelectedIndex = id;
|
|
}
|
|
}
|
|
|
|
public void AddValue(string value)
|
|
{
|
|
if (string.IsNullOrEmpty(value)) return;
|
|
if (_combo.Items.Contains(value)) return;
|
|
_combo.Items.Add(value);
|
|
if(_combo.SelectedIndex < 0) _combo.SelectedIndex = 0;
|
|
}
|
|
|
|
public void ClearItems()
|
|
{
|
|
_combo.Items.Clear();
|
|
_combo.SelectedIndex = -1;
|
|
}
|
|
}
|