PIbd-23_Vrazhkin_S_A_Electr.../lab1/FormLocomotivCollection.cs
2023-10-31 13:25:13 +04:00

106 lines
3.8 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.

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;
}
FormLocomotiv form = new();
if (form.ShowDialog() == DialogResult.OK) {
if (obj + form.SelectedLocomotiv) {
MessageBox.Show("Объект добавлен");
collectionPictureBox.Image = obj.ShowLocos();
}
else {
MessageBox.Show("Не удалось добавить объект");
}
}
}
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();
}
}