using Liner.Drawing; using Liner.Generics; using Liner.MovingStrategies; 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 Liner { /// /// Форма для работы с набором объектов класса DrawingLiner /// public partial class FormLinerCollection : Form { /// /// Набор объектов /// private readonly LinersGenericStorage _storage; /// /// Конструктор /// public FormLinerCollection() { InitializeComponent(); _storage = new LinersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } /// /// Добавление объекта в набор /// /// /// private void ButtonAddLiner_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } MainScreen form = new(); if (form.ShowDialog() == DialogResult.OK) { if (obj + form.SelectedLiner != -1) { MessageBox.Show("Объект добавлен"); pictureBoxCollection.Image = obj.ShowLiners(); } else { MessageBox.Show("Не удалось добавить объект"); } } } /// /// Удаление объекта из набора /// /// /// private void ButtonRemoveLiner_Click(object sender, EventArgs e) { int pos; if (listBoxStorages.SelectedIndex == -1) { return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } if (textBoxNumber.Text != "") { pos = Convert.ToInt32(textBoxNumber.Text); } else { MessageBox.Show("Не удалось удалить объект"); return; } pos = 0; if (obj - pos) { MessageBox.Show("Объект удален"); pictureBoxCollection.Image = obj.ShowLiners(); } else { MessageBox.Show("Не удалось удалить объект"); } } /// /// Добавление набора в коллекцию /// /// /// private void ButtonAddObject_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxStorageName.Text)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _storage.AddSet(textBoxStorageName.Text); ReloadObjects(); } /// /// Выбор набора /// /// /// private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) { pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLiners(); } /// /// Удаление набора /// /// /// private void ButtonRemoveObject_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { return; } if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty); ReloadObjects(); } } /// /// Обновление рисунка по набору /// /// /// private void ButtonRefreshCollection_Click(object sender, EventArgs e) { if (listBoxStorages.SelectedIndex == -1) { return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } pictureBoxCollection.Image = obj.ShowLiners(); } /// /// Заполнение listBoxObjects /// private void ReloadObjects() { int index = listBoxStorages.SelectedIndex; listBoxStorages.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { listBoxStorages.Items.Add(_storage.Keys[i]); } if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) { listBoxStorages.SelectedIndex = 0; } else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count) { listBoxStorages.SelectedIndex = index; } } } }