PIbd-21_SagirovM.M_Liner_Base/Liner/FormLinerCollection.cs

196 lines
6.5 KiB
C#
Raw Normal View History

2023-11-24 12:39:11 +04:00
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
{
/// <summary>
/// Форма для работы с набором объектов класса DrawingLiner
/// </summary>
public partial class FormLinerCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
2023-11-24 12:49:48 +04:00
private readonly LinersGenericStorage _storage;
2023-11-24 12:39:11 +04:00
/// <summary>
/// Конструктор
/// </summary>
public FormLinerCollection()
{
InitializeComponent();
2023-11-24 12:49:48 +04:00
_storage = new LinersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
2023-11-24 12:39:11 +04:00
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2023-11-24 12:49:48 +04:00
private void ButtonAddLiner_Click(object sender, EventArgs e)
2023-11-24 12:39:11 +04:00
{
2023-11-24 12:49:48 +04:00
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
2023-11-24 15:14:59 +04:00
var formLinerConfig = new FormLinerConfig();
formLinerConfig.AddEvent(AddLiner);
formLinerConfig.Show();
}
private void AddLiner(DrawingLiner liner)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
2023-11-24 12:49:48 +04:00
if (obj == null)
{
return;
}
2023-11-24 15:14:59 +04:00
if ((obj + liner) != -1)
2023-11-24 12:39:11 +04:00
{
2023-11-24 15:14:59 +04:00
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowLiners();
}
else
{
MessageBox.Show("Не удалось добавить объект");
2023-11-24 12:39:11 +04:00
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2023-11-24 12:49:48 +04:00
private void ButtonRemoveLiner_Click(object sender, EventArgs e)
2023-11-24 12:39:11 +04:00
{
2023-11-24 15:27:59 +04:00
int pos;
2023-11-24 12:49:48 +04:00
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
2023-11-24 12:39:11 +04:00
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
2023-11-24 15:27:59 +04:00
if (textBoxNumber.Text != "")
{
pos = Convert.ToInt32(textBoxNumber.Text);
}
else
{
MessageBox.Show("Не удалось удалить объект");
return;
}
pos = 0;
2023-11-24 12:49:48 +04:00
if (obj - pos)
2023-11-24 12:39:11 +04:00
{
MessageBox.Show("Объект удален");
2023-11-24 12:49:48 +04:00
pictureBoxCollection.Image = obj.ShowLiners();
2023-11-24 12:39:11 +04:00
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
2023-11-24 12:49:48 +04:00
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLiners();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
/// <summary>
2023-11-24 12:39:11 +04:00
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2023-11-24 15:22:12 +04:00
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
2023-11-24 12:49:48 +04:00
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowLiners();
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
2023-11-24 12:39:11 +04:00
{
2023-11-24 12:49:48 +04:00
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;
}
2023-11-24 12:39:11 +04:00
}
}
}