PIbd-23_Dyakonov_R_R_Tank/ProjectTank/FormTankCollection.cs

144 lines
5.0 KiB
C#
Raw Normal View History

2023-10-31 12:26:25 +04:00
using ProjectTank.DrawningObjects;
using ProjectTank.Generics;
using ProjectTank.MovementStrategy;
namespace ProjectTank
{
public partial class FormTankCollection : Form
{
2023-10-31 12:39:26 +04:00
private readonly TanksGenericStorage _storage;
2023-10-31 12:26:25 +04:00
/// <summary>
/// Конструктор
/// </summary>
public FormTankCollection()
{
InitializeComponent();
2023-10-31 12:39:26 +04:00
_storage = new TanksGenericStorage(pictureBoxCollection.Width,
2023-10-31 12:26:25 +04:00
pictureBoxCollection.Height);
}
2023-10-31 12:39:26 +04:00
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)
{
string objName = textBoxStorageName.Text;
if (string.IsNullOrEmpty(objName))
{
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]?.ShowTanks();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
}
2023-10-31 12:26:25 +04:00
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddTank_Click(object sender, EventArgs e)
{
2023-10-31 12:39:26 +04:00
if (listBoxStorages.SelectedIndex == -1) return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null) return;
2023-10-31 12:26:25 +04:00
FormTank form = new();
if (form.ShowDialog() == DialogResult.OK)
{
2023-10-31 12:39:26 +04:00
if (obj + form.SelectedTank)
2023-10-31 12:26:25 +04:00
{
MessageBox.Show("Объект добавлен");
2023-10-31 12:39:26 +04:00
pictureBoxCollection.Image = obj.ShowTanks();
2023-10-31 12:26:25 +04:00
}
2023-10-31 12:39:26 +04:00
else MessageBox.Show("Не удалось добавить объект");
2023-10-31 12:26:25 +04:00
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveTank_Click(object sender, EventArgs e)
{
2023-10-31 12:39:26 +04:00
if (listBoxStorages.SelectedIndex == -1) return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null) return;
2023-10-31 12:26:25 +04:00
if (MessageBox.Show("Удалить объект?", "Удаление",
2023-10-31 12:39:26 +04:00
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
2023-10-31 12:26:25 +04:00
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
2023-10-31 12:39:26 +04:00
if (obj - pos != null)
2023-10-31 12:26:25 +04:00
{
MessageBox.Show("Объект удален");
2023-10-31 12:39:26 +04:00
pictureBoxCollection.Image = obj.ShowTanks();
2023-10-31 12:26:25 +04:00
}
2023-10-31 12:39:26 +04:00
else MessageBox.Show("Не удалось удалить объект");
2023-10-31 12:26:25 +04:00
}
2023-10-31 12:39:26 +04:00
2023-10-31 12:26:25 +04:00
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2023-10-31 12:39:26 +04:00
2023-10-31 12:26:25 +04:00
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
2023-10-31 12:39:26 +04:00
if (listBoxStorages.SelectedIndex == -1) return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null) return;
pictureBoxCollection.Image = obj.ShowTanks();
2023-10-31 12:26:25 +04:00
}
2023-10-31 12:39:26 +04:00
2023-10-31 12:26:25 +04:00
}
}