Петрушин Егор ПИбд-22 Лабораторная работа №4 "Самоходная артиллерийская установка" #9
@ -33,7 +33,15 @@
|
||||
ButtonAddSPAU = new Button();
|
||||
ButtonRemoveSPAU = new Button();
|
||||
ButtonRefreshCollection = new Button();
|
||||
panel1 = new Panel();
|
||||
panel2 = new Panel();
|
||||
listBoxStorages = new ListBox();
|
||||
ButtonDelObject = new Button();
|
||||
ButtonAddObject = new Button();
|
||||
textBoxStorageName = new TextBox();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||
panel1.SuspendLayout();
|
||||
panel2.SuspendLayout();
|
||||
SuspendLayout();
|
||||
//
|
||||
// pictureBoxCollection
|
||||
@ -46,14 +54,14 @@
|
||||
//
|
||||
// maskedTextBoxNumber
|
||||
//
|
||||
maskedTextBoxNumber.Location = new Point(729, 51);
|
||||
maskedTextBoxNumber.Location = new Point(18, 71);
|
||||
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||
maskedTextBoxNumber.Size = new Size(143, 27);
|
||||
maskedTextBoxNumber.TabIndex = 1;
|
||||
//
|
||||
// ButtonAddSPAU
|
||||
//
|
||||
ButtonAddSPAU.Location = new Point(748, 102);
|
||||
ButtonAddSPAU.Location = new Point(44, 19);
|
||||
ButtonAddSPAU.Name = "ButtonAddSPAU";
|
||||
ButtonAddSPAU.Size = new Size(102, 33);
|
||||
ButtonAddSPAU.TabIndex = 2;
|
||||
@ -63,7 +71,7 @@
|
||||
//
|
||||
// ButtonRemoveSPAU
|
||||
//
|
||||
ButtonRemoveSPAU.Location = new Point(750, 141);
|
||||
ButtonRemoveSPAU.Location = new Point(44, 118);
|
||||
ButtonRemoveSPAU.Name = "ButtonRemoveSPAU";
|
||||
ButtonRemoveSPAU.Size = new Size(100, 37);
|
||||
ButtonRemoveSPAU.TabIndex = 3;
|
||||
@ -73,7 +81,7 @@
|
||||
//
|
||||
// ButtonRefreshCollection
|
||||
//
|
||||
ButtonRefreshCollection.Location = new Point(750, 195);
|
||||
ButtonRefreshCollection.Location = new Point(44, 172);
|
||||
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
|
||||
ButtonRefreshCollection.Size = new Size(100, 35);
|
||||
ButtonRefreshCollection.TabIndex = 4;
|
||||
@ -81,21 +89,81 @@
|
||||
ButtonRefreshCollection.UseVisualStyleBackColor = true;
|
||||
ButtonRefreshCollection.Click += ButtonRefreshCollection_Click;
|
||||
//
|
||||
// panel1
|
||||
//
|
||||
panel1.Controls.Add(ButtonRefreshCollection);
|
||||
panel1.Controls.Add(ButtonRemoveSPAU);
|
||||
panel1.Controls.Add(ButtonAddSPAU);
|
||||
panel1.Controls.Add(maskedTextBoxNumber);
|
||||
panel1.Location = new Point(711, 217);
|
||||
panel1.Name = "panel1";
|
||||
panel1.Size = new Size(176, 227);
|
||||
panel1.TabIndex = 5;
|
||||
//
|
||||
// panel2
|
||||
//
|
||||
panel2.Controls.Add(listBoxStorages);
|
||||
panel2.Controls.Add(ButtonDelObject);
|
||||
panel2.Controls.Add(ButtonAddObject);
|
||||
panel2.Controls.Add(textBoxStorageName);
|
||||
panel2.Location = new Point(711, 12);
|
||||
panel2.Name = "panel2";
|
||||
panel2.Size = new Size(174, 198);
|
||||
panel2.TabIndex = 6;
|
||||
//
|
||||
// listBoxStorages
|
||||
//
|
||||
listBoxStorages.FormattingEnabled = true;
|
||||
listBoxStorages.ItemHeight = 20;
|
||||
listBoxStorages.Items.AddRange(new object[] { "Ans", "Second", "Dry", "Quadro" });
|
||||
listBoxStorages.Location = new Point(18, 87);
|
||||
listBoxStorages.Name = "listBoxStorages";
|
||||
listBoxStorages.Size = new Size(143, 64);
|
||||
listBoxStorages.TabIndex = 7;
|
||||
//
|
||||
// ButtonDelObject
|
||||
//
|
||||
ButtonDelObject.Location = new Point(18, 168);
|
||||
ButtonDelObject.Name = "ButtonDelObject";
|
||||
ButtonDelObject.Size = new Size(143, 28);
|
||||
ButtonDelObject.TabIndex = 3;
|
||||
ButtonDelObject.Text = "Удалить набор";
|
||||
ButtonDelObject.UseVisualStyleBackColor = true;
|
||||
ButtonDelObject.Click += ButtonDelObject_Click;
|
||||
//
|
||||
// ButtonAddObject
|
||||
//
|
||||
ButtonAddObject.Location = new Point(18, 53);
|
||||
ButtonAddObject.Name = "ButtonAddObject";
|
||||
ButtonAddObject.Size = new Size(143, 28);
|
||||
ButtonAddObject.TabIndex = 1;
|
||||
ButtonAddObject.Text = "Добавить набор";
|
||||
ButtonAddObject.UseVisualStyleBackColor = true;
|
||||
ButtonAddObject.Click += ButtonAddObject_Click;
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
textBoxStorageName.Location = new Point(18, 20);
|
||||
textBoxStorageName.Name = "textBoxStorageName";
|
||||
textBoxStorageName.Size = new Size(143, 27);
|
||||
textBoxStorageName.TabIndex = 0;
|
||||
//
|
||||
// FormSPAUCollection
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(899, 456);
|
||||
Controls.Add(ButtonRefreshCollection);
|
||||
Controls.Add(ButtonRemoveSPAU);
|
||||
Controls.Add(ButtonAddSPAU);
|
||||
Controls.Add(maskedTextBoxNumber);
|
||||
Controls.Add(panel2);
|
||||
Controls.Add(panel1);
|
||||
Controls.Add(pictureBoxCollection);
|
||||
Name = "FormSPAUCollection";
|
||||
Text = "FormSPAUCollection";
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
||||
panel1.ResumeLayout(false);
|
||||
panel1.PerformLayout();
|
||||
panel2.ResumeLayout(false);
|
||||
panel2.PerformLayout();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -105,5 +173,11 @@
|
||||
private Button ButtonAddSPAU;
|
||||
private Button ButtonRemoveSPAU;
|
||||
private Button ButtonRefreshCollection;
|
||||
private Panel panel1;
|
||||
private Panel panel2;
|
||||
private Button ButtonAddObject;
|
||||
private TextBox textBoxStorageName;
|
||||
private Button ButtonDelObject;
|
||||
private ListBox listBoxStorages;
|
||||
}
|
||||
}
|
@ -23,14 +23,77 @@ namespace SelfPropelledArtilleryUnit
|
||||
/// <summary>
|
||||
/// Набор объектов
|
||||
/// </summary>
|
||||
private readonly SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU> _SPAUs;
|
||||
//private readonly SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU> _SPAUs;
|
||||
private readonly SPAUGenericStorage _storage;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
public FormSPAUCollection()
|
||||
{
|
||||
InitializeComponent();
|
||||
_SPAUs = new SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
_storage = new SPAUGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
}
|
||||
/// <summary>
|
||||
/// Заполнение listBoxObjects
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление набора в коллекцию
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonAddObject_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (string.IsNullOrEmpty(listBoxStorages.Text))
|
||||
{
|
||||
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
_storage.AddSet(listBoxStorages.Text);
|
||||
ReloadObjects();
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Выбор набора
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image =
|
||||
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowSPAUs();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
@ -39,16 +102,24 @@ namespace SelfPropelledArtilleryUnit
|
||||
/// <param name="e"></param>
|
||||
private void ButtonAddSPAU_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FormSPAU form = new();
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
int addedIndex = _SPAUs + form.SelectedSPAU;
|
||||
//MessageBox.Show(addedIndex.ToString());
|
||||
int addedIndex = obj + form.SelectedSPAU;
|
||||
if (addedIndex != -1 && addedIndex <= countPlaces)
|
||||
|
||||
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = _SPAUs.ShowSPAUs();
|
||||
pictureBoxCollection.Image = obj.ShowSPAUs();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -56,6 +127,8 @@ namespace SelfPropelledArtilleryUnit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Удаление объекта из набора
|
||||
/// </summary>
|
||||
@ -63,24 +136,36 @@ namespace SelfPropelledArtilleryUnit
|
||||
/// <param name="e"></param>
|
||||
private void ButtonRemoveSPAU_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (MessageBox.Show("Удалить объект?", "Удаление",
|
||||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int pos;
|
||||
try {
|
||||
|
||||
pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||||
try
|
||||
{
|
||||
|
||||
pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||||
}
|
||||
catch {
|
||||
catch
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить объект");
|
||||
return;
|
||||
}
|
||||
if (_SPAUs - pos)
|
||||
if (obj - pos)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = _SPAUs.ShowSPAUs();
|
||||
pictureBoxCollection.Image = obj.ShowSPAUs();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -94,7 +179,17 @@ namespace SelfPropelledArtilleryUnit
|
||||
/// <param name="e"></param>
|
||||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image = _SPAUs.ShowSPAUs();
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pictureBoxCollection.Image = obj.ShowSPAUs();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SelfPropelledArtilleryUnit.DrawningObjects;
|
||||
using SelfPropelledArtilleryUnit.MovementStrategy;
|
||||
|
||||
namespace SelfPropelledArtilleryUnit.Generics
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс для хранения коллекции
|
||||
/// </summary>
|
||||
internal class SPAUGenericStorage
|
||||
{
|
||||
/// <summary>
|
||||
/// Словарь (хранилище)
|
||||
/// </summary>
|
||||
readonly Dictionary<string, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>> _SPAUStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий наборов
|
||||
/// </summary>
|
||||
public List<string> Keys => _SPAUStorages.Keys.ToList();
|
||||
/// <summary>
|
||||
/// Ширина окна отрисовки
|
||||
/// </summary>
|
||||
private readonly int _pictureWidth;
|
||||
/// <summary>
|
||||
/// Высота окна отрисовки
|
||||
/// </summary>
|
||||
private readonly int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="pictureWidth"></param>
|
||||
/// <param name="pictureHeight"></param>
|
||||
public SPAUGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_SPAUStorages = new Dictionary<string, SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление набора
|
||||
/// </summary>
|
||||
/// <param name="name">Название набора</param>
|
||||
public void AddSet(string name)
|
||||
{
|
||||
try
|
||||
|
||||
{
|
||||
_SPAUStorages.Add(name, new SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
catch (Exception) { return; }
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление набора
|
||||
/// </summary>
|
||||
/// <param name="name">Название набора</param>
|
||||
public void DelSet(string name)
|
||||
{
|
||||
try { _SPAUStorages.Remove(name); } catch (Exception) { return; }
|
||||
eegov
commented
Простые проверки, никаких try-catch Простые проверки, никаких try-catch
|
||||
}
|
||||
/// <summary>
|
||||
/// Доступ к набору
|
||||
/// </summary>
|
||||
/// <param name="ind"></param>
|
||||
/// <returns></returns>
|
||||
public SPAUGenericCollection<DrawningSPAU, DrawningObjectSPAU>? this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_SPAUStorages.ContainsKey(ind))
|
||||
{
|
||||
return _SPAUStorages[ind];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user
Простые проверки, никаких try-catch