diff --git a/ElectricLocomotive/FormLocomotiveCollection.cs b/ElectricLocomotive/FormLocomotiveCollection.cs index 8c29ce8..60aefa7 100644 --- a/ElectricLocomotive/FormLocomotiveCollection.cs +++ b/ElectricLocomotive/FormLocomotiveCollection.cs @@ -24,11 +24,6 @@ namespace ProjectElectricLocomotive ButtonRefreshCollection_Click(sender, e); } - private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) - { - ListBoxObjects_SelectedIndexChanged(sender, e); - } - private void buttonAddObject_Click(object sender, EventArgs e) { ButtonAddObject_Click(sender, e); @@ -38,5 +33,10 @@ namespace ProjectElectricLocomotive { ButtonDelObject_Click(sender, e); } + + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + ListBoxObjects_SelectedIndexChanged(sender, e); + } } } diff --git a/ElectricLocomotive/LogicFormLocomotiveCollection.cs b/ElectricLocomotive/LogicFormLocomotiveCollection.cs index 5acfa68..a39d3fe 100644 --- a/ElectricLocomotive/LogicFormLocomotiveCollection.cs +++ b/ElectricLocomotive/LogicFormLocomotiveCollection.cs @@ -98,6 +98,73 @@ namespace ProjectElectricLocomotive } } + /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() + { + 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; + } + } + /// + /// Добавление набора в коллекцию + /// + /// + /// + 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(); + } + + /// + /// Выбор набора + /// + /// + /// + private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLocomotives(); + } + /// + /// Удаление набора + /// + /// + /// + private void ButtonDelObject_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(); + } + } + /// /// Добавление объекта в набор /// @@ -174,7 +241,10 @@ namespace ProjectElectricLocomotive return; } var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; - + if (obj == null) + { + return; + } pictureBoxCollection.Image = obj.ShowLocomotives(); } }