KOP_Library_v5/Component_1/List_with_choice.cs

126 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Component_1
{
public partial class List_with_choice : UserControl
{
private event EventHandler? _checkedlistChanged;
private event Action? _errorOccured;
public string Error { get; private set; }
public String Element
{
get
{
if (checkedListBox1.SelectedItem != null) return checkedListBox1.SelectedItem.ToString();
else return string.Empty;
}
set
{
checkedListBox1.SelectedItem = value;
}
}
public event EventHandler Checkedlist
{
add { _checkedlistChanged += value; }
remove { _checkedlistChanged -= value; }
}
public event Action AnErrorOccurred
{
add { _errorOccured += value; }
remove { _errorOccured -= value; }
}
public List_with_choice()
{
InitializeComponent();
Error = string.Empty;
}
public void Fill_List(string str) //метод заполнения списка
{
if (str == null)
{
MessageBox.Show("Вы не ввели строку");
return;
}
string[] list = str.Split(' ');
foreach (string s in list)
{
checkedListBox1.Items.Add(s);
}
}
public void Clean_List() //метод очистки списка
{
checkedListBox1.Items.Clear();
}
private void button_Load_Click(object sender, EventArgs e)
{
string value = "";
if (InputBox("Заполнение списка", "Пожалуйста введите строку", ref value) == DialogResult.OK)
{
try
{
Fill_List(value);
_checkedlistChanged?.Invoke(this, e);
}
catch (Exception ex)
{
Error = ex.Message;
_errorOccured?.Invoke();
}
}
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public static DialogResult InputBox(string title, string promptText, ref string value)//диалоговое окно
{
Form form = new Form();
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();
form.Text = title;
label.Text = promptText;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(36, 36, 372, 13);
textBox.SetBounds(36, 86, 700, 20);
buttonOk.SetBounds(228, 160, 160, 60);
buttonCancel.SetBounds(400, 160, 160, 60);
label.AutoSize = true;
form.ClientSize = new Size(796, 307);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
return dialogResult;
}
private void button_Clean_Click(object sender, EventArgs e)
{
Clean_List();
}
}
}