PIbd-23_Vrazhkin_S_A_Electr.../lab1/FormLocomotivCollection.cs
2023-11-28 12:26:51 +04:00

140 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Windows.Forms;
namespace ElectricLocomotive;
public partial class FormLocomotivCollection : Form {
private readonly LocosGenericStorage _storage;
public FormLocomotivCollection() {
InitializeComponent();
_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;
}
}
private void addLocomotiv_Click(object sender, EventArgs e) {
if (storageListBox.SelectedIndex == -1) {
return;
}
var obj = _storage[storageListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null) {
return;
}
FormLocoConfig form = new();
form.Show();
Action<DrawingLocomotiv>? monorailDelegate = new((m) => {
bool q = (obj + m);
if (q) {
MessageBox.Show("Объект добавлен");
m.ChangePictureBoxSize(collectionPictureBox.Width, collectionPictureBox.Height);
collectionPictureBox.Image = obj.ShowLocos();
}
else {
MessageBox.Show("Не удалось добавить объект");
}
});
form.AddEvent(monorailDelegate);
}
private void deleteLoco_Click(object sender, EventArgs e) {
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) {
return;
}
int pos = Convert.ToInt32(locoIndexInput.Text);
if (obj - pos != null) {
MessageBox.Show("Объект удален");
collectionPictureBox.Image = obj.ShowLocos();
}
else {
MessageBox.Show("Не удалось удалить объект");
}
}
private void refreshCollection_Click(object sender, EventArgs e) {
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();
}
}
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();
}
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);
}
}
}
}