PIbd-23_Vrazhkin_S_A_Electr.../lab1/FormLocomotivCollection.cs

140 lines
5.3 KiB
C#
Raw Permalink Normal View History

2023-11-28 12:26:51 +04:00
using System.Windows.Forms;
namespace ElectricLocomotive;
2023-10-17 13:49:31 +04:00
public partial class FormLocomotivCollection : Form {
2023-10-31 13:25:13 +04:00
private readonly LocosGenericStorage _storage;
2023-10-17 13:49:31 +04:00
public FormLocomotivCollection() {
InitializeComponent();
2023-10-31 13:25:13 +04:00
_storage = new(collectionPictureBox.Width, collectionPictureBox.Height);
}
private void ReloadObjects() {
int index = storageListBox.SelectedIndex;
storageListBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++) {
storageListBox.Items.Add(_storage.Keys[i]);
}
if (storageListBox.Items.Count > 0 && (index == -1 || index
>= storageListBox.Items.Count)) {
storageListBox.SelectedIndex = 0;
}
else if (storageListBox.Items.Count > 0 && index > -1 &&
index < storageListBox.Items.Count) {
storageListBox.SelectedIndex = index;
}
2023-10-17 13:49:31 +04:00
}
private void addLocomotiv_Click(object sender, EventArgs e) {
2023-10-31 13:25:13 +04:00
if (storageListBox.SelectedIndex == -1) {
return;
}
var obj = _storage[storageListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null) {
return;
}
2023-11-14 12:17:37 +04:00
FormLocoConfig form = new();
form.Show();
Action<DrawingLocomotiv>? monorailDelegate = new((m) => {
bool q = (obj + m);
2023-11-28 12:26:51 +04:00
if (q) {
2023-10-17 13:49:31 +04:00
MessageBox.Show("Объект добавлен");
2023-11-14 12:17:37 +04:00
m.ChangePictureBoxSize(collectionPictureBox.Width, collectionPictureBox.Height);
2023-10-31 13:25:13 +04:00
collectionPictureBox.Image = obj.ShowLocos();
2023-10-17 13:49:31 +04:00
}
2023-11-28 12:26:51 +04:00
else {
2023-10-17 13:49:31 +04:00
MessageBox.Show("Не удалось добавить объект");
}
2023-11-14 12:17:37 +04:00
});
form.AddEvent(monorailDelegate);
2023-10-17 13:49:31 +04:00
}
private void deleteLoco_Click(object sender, EventArgs e) {
2023-10-31 13:25:13 +04:00
if (storageListBox.SelectedIndex == -1) {
return;
}
var obj = _storage[storageListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null) {
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
2023-10-17 13:49:31 +04:00
return;
}
2023-10-31 13:25:13 +04:00
int pos = Convert.ToInt32(locoIndexInput.Text);
if (obj - pos != null) {
2023-10-17 13:49:31 +04:00
MessageBox.Show("Объект удален");
2023-10-31 13:25:13 +04:00
collectionPictureBox.Image = obj.ShowLocos();
2023-10-17 13:49:31 +04:00
}
else {
MessageBox.Show("Не удалось удалить объект");
}
}
private void refreshCollection_Click(object sender, EventArgs e) {
2023-10-31 13:25:13 +04:00
if (storageListBox.SelectedIndex == -1) {
return;
}
var obj = _storage[storageListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null) {
return;
}
collectionPictureBox.Image = obj.ShowLocos();
}
private void storagesListBox_SelectedIndexChanged(object sender, EventArgs e) {
collectionPictureBox.Image =
_storage[storageListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowLocos();
}
private void delStorageButton_Click(object sender, EventArgs e) {
if (storageListBox.SelectedIndex == -1) {
return;
}
if (MessageBox.Show($"Удалить объект{storageListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes) {
_storage.DelSet(storageListBox.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
2023-10-17 13:49:31 +04:00
}
2023-10-31 13:25:13 +04:00
private void addStorageButton_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(storageIndexInput.Text)) {
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(storageIndexInput.Text);
ReloadObjects();
}
2023-11-28 12:26:51 +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 (loadFileDialog.ShowDialog() == DialogResult.OK) {
if (_storage.LoadData(loadFileDialog.FileName)) {
MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
foreach (var collection in _storage.Keys) {
storageListBox.Items.Add(collection);
}
}
else {
MessageBox.Show("Не загрузилось", "Результат",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
2023-10-17 13:49:31 +04:00
}