PIbd-23-Radaev-A.V.-20/Catamaran/FormCatamaranCollection.cs

190 lines
6.4 KiB
C#
Raw Normal View History

2023-11-12 11:28:15 +04:00
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 Catamaran
{
public partial class FormCatamaranCollection : Form
{
2023-11-19 12:36:59 +04:00
private readonly CatamaransGenericStorage _storage;
2023-11-28 13:45:08 +04:00
2023-11-12 11:28:15 +04:00
public FormCatamaranCollection()
{
InitializeComponent();
2023-11-19 12:36:59 +04:00
_storage = new CatamaransGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
2023-11-12 11:28:15 +04:00
}
2023-11-28 13:45:08 +04:00
2023-11-19 12:36:59 +04:00
private void ReloadObjects()
{
int index = SetslistBox.SelectedIndex;
SetslistBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
SetslistBox.Items.Add(_storage.Keys[i]);
}
if (SetslistBox.Items.Count > 0 && (index == -1 || index
>= SetslistBox.Items.Count))
{
SetslistBox.SelectedIndex = 0;
}
else if (SetslistBox.Items.Count > 0 && index > -1 &&
index < SetslistBox.Items.Count)
{
SetslistBox.SelectedIndex = index;
}
}
private void ButtonAddStorage_Click(object sender, EventArgs e)
{
string storname = setAddBox.Text;
if (string.IsNullOrEmpty(setAddBox.Text) || _storage.Keys.Contains(storname))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(setAddBox.Text);
ReloadObjects();
}
2023-11-28 13:45:08 +04:00
2023-11-19 12:36:59 +04:00
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[SetslistBox.SelectedItem?.ToString() ?? string.Empty]?.ShowCats();
}
2023-11-28 13:45:08 +04:00
2023-11-19 12:36:59 +04:00
private void ButtonDeleteStorage_Click(object sender, EventArgs e)
{
if (SetslistBox.SelectedIndex == -1)
{
return;
}
2023-11-28 13:45:08 +04:00
if (MessageBox.Show($"Удалить набор {SetslistBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
2023-11-19 12:36:59 +04:00
_storage.DelSet(SetslistBox.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
2023-11-28 13:45:08 +04:00
2023-11-19 12:36:59 +04:00
private void ButtonAddCatamaran_Click(object sender, EventArgs e)
2023-11-12 11:28:15 +04:00
{
2023-11-19 12:36:59 +04:00
if (SetslistBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[SetslistBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
2023-11-25 20:02:11 +04:00
FormCatamaranConfig form = new();
form.Show();
2023-11-28 13:45:08 +04:00
Action<DrawningCatamaran>? catamaranDelegate = new((c) =>
{
2023-11-25 20:02:11 +04:00
bool q = (obj + c);
if (q)
2023-11-12 11:28:15 +04:00
{
MessageBox.Show("Объект добавлен");
2023-11-25 20:02:11 +04:00
c.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height);
2023-11-19 12:36:59 +04:00
pictureBoxCollection.Image = obj.ShowCats();
2023-11-12 11:28:15 +04:00
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
2023-11-25 20:02:11 +04:00
});
form.AddEvent(catamaranDelegate);
2023-11-12 11:28:15 +04:00
}
2023-11-28 13:45:08 +04:00
2023-11-19 12:36:59 +04:00
private void ButtonRemoveCatamaran_Click(object sender, EventArgs e)
2023-11-12 11:28:15 +04:00
{
2023-11-19 12:36:59 +04:00
if (SetslistBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[SetslistBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
2023-11-12 11:28:15 +04:00
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBox.Text);
2023-11-19 12:36:59 +04:00
if (obj - pos != null)
2023-11-12 11:28:15 +04:00
{
MessageBox.Show("Объект удален");
2023-11-19 12:36:59 +04:00
pictureBoxCollection.Image = obj.ShowCats();
2023-11-12 11:28:15 +04:00
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
2023-11-19 12:36:59 +04:00
if (SetslistBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[SetslistBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowCats();
2023-11-12 11:28:15 +04:00
}
2023-11-19 12:36:59 +04:00
2023-11-28 13:45:08 +04:00
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.SaveData(saveFileDialog.FileName))
{
MessageBox.Show("Сохранение прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Не сохранилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
if (_storage.LoadData(openFileDialog.FileName))
{
MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach (var collection in _storage.Keys)
{
SetslistBox.Items.Add(collection);
}
}
else
{
MessageBox.Show("Не загрузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
2023-11-12 11:28:15 +04:00
}
}