180 lines
5.9 KiB
C#
180 lines
5.9 KiB
C#
using Lab.DrawningObjects;
|
||
using Lab.Generics;
|
||
using Lab.MovementStrategy;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace Lab
|
||
{
|
||
public partial class CollectionsFrame : Form
|
||
{
|
||
private readonly CarsGenericStorage _storage;
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public CollectionsFrame()
|
||
{
|
||
InitializeComponent();
|
||
_storage = new CarsGenericStorage(DrawTank.Width, DrawTank.Height);
|
||
}
|
||
/// <summary>
|
||
/// Заполнение listBoxObjects
|
||
/// </summary>
|
||
private void ReloadObjects()
|
||
{
|
||
int index = CollectionListBox.SelectedIndex;
|
||
CollectionListBox.Items.Clear();
|
||
foreach (var key in _storage.Keys)
|
||
{
|
||
CollectionListBox.Items.Add(key);
|
||
}
|
||
if (CollectionListBox.Items.Count > 0 && (index == -1 || index
|
||
>= CollectionListBox.Items.Count))
|
||
{
|
||
CollectionListBox.SelectedIndex = 0;
|
||
}
|
||
else if (CollectionListBox.Items.Count > 0 && index > -1 &&
|
||
index < CollectionListBox.Items.Count)
|
||
{
|
||
CollectionListBox.SelectedIndex = index;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Добавление набора в коллекцию
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrEmpty(SetTextBox.Text))
|
||
{
|
||
MessageBox.Show("Не все данные заполнены", "Ошибка",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
_storage.AddSet(SetTextBox.Text);
|
||
ReloadObjects();
|
||
}
|
||
/// <summary>
|
||
/// Выбор набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ListBoxObjects_SelectedIndexChanged(object sender,
|
||
EventArgs e)
|
||
{
|
||
DrawTank.Image =
|
||
_storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowCars();
|
||
}
|
||
/// <summary>
|
||
/// Удаление набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonDelObject_Click(object sender, EventArgs e)
|
||
{
|
||
if (CollectionListBox.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show($"Удалить объект {CollectionListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
|
||
MessageBoxIcon.Question) == DialogResult.Yes)
|
||
{
|
||
_storage.DelSet(CollectionListBox.SelectedItem.ToString()
|
||
?? string.Empty);
|
||
ReloadObjects();
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Добавление объекта в набор
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonAddCar_Click(object sender, EventArgs e)
|
||
{
|
||
if (CollectionListBox.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
Frame form = new();
|
||
if (form.ShowDialog() == DialogResult.OK)
|
||
{
|
||
if (obj + form.SelectedCar)
|
||
{
|
||
MessageBox.Show("Объект добавлен");
|
||
DrawTank.Image = obj.ShowCars();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось добавить объект");
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Удаление объекта из набора
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonRemoveCar_Click(object sender, EventArgs e)
|
||
{
|
||
if (CollectionListBox.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
if (MessageBox.Show("Удалить объект?", "Удаление",
|
||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||
{
|
||
return;
|
||
}
|
||
int pos = Convert.ToInt32(CarTextBox.Text);
|
||
if (obj - pos != null)
|
||
{
|
||
MessageBox.Show("Объект удален");
|
||
DrawTank.Image = obj.ShowCars();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось удалить объект");
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Обновление рисунка по набору
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void ButtonRefreshCollection_Click(object sender, EventArgs
|
||
e)
|
||
{
|
||
if (CollectionListBox.SelectedIndex == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
|
||
string.Empty];
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
DrawTank.Image = obj.ShowCars();
|
||
}
|
||
}
|
||
}
|