не работает добавление

This commit is contained in:
Макс Бондаренко 2022-10-30 00:49:41 +04:00
parent f5435fbcce
commit 6e509b6d07
3 changed files with 25 additions and 61 deletions

View File

@ -132,7 +132,7 @@
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(176, 23);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// maskedTextBoxPosition
//

View File

@ -8,15 +8,14 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WarmlyShip;
using static System.Windows.Forms.DataFormats;
namespace WarmlyShip
{
public partial class FormMapWithSetShip : Form
{
private MapWithSetShipGeneric<DrawningObjectShip, AbstractMap> _mapShipCollectionGeneric;
public bool ShipOnMap = false;
private readonly Dictionary<string, AbstractMap> _mapsDict = new() {
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{ "Простая карта", new SimpleMap() },
{ "Вторая карта", new SecondMap() },
{ "Последняя карта", new LastMap() }
@ -33,7 +32,6 @@ namespace WarmlyShip
{
comboBoxSelectorMap.Items.Add(elem.Key);
}
}
private void ReloadMaps()
@ -54,25 +52,6 @@ namespace WarmlyShip
}
}
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonDeleteMap_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
ReloadMaps();
}
}
private void ButtonAddMap_Click(object sender, EventArgs e)
{
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
@ -89,31 +68,25 @@ namespace WarmlyShip
ReloadMaps();
}
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
private void ButtonDeleteMap_Click(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
if (listBoxMaps.SelectedIndex == -1)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new SecondMap();
break;
case "Последняя карта":
map = new LastMap();
break;
return;
}
if (map != null)
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapShipCollectionGeneric = new MapWithSetShipGeneric<DrawningObjectShip, AbstractMap>(pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapShipCollectionGeneric = null;
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ??
string.Empty);
ReloadMaps();
}
}
private void ButtonAddShip_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
@ -124,10 +97,10 @@ namespace WarmlyShip
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObjectShip ship = new(form.SelectedShip);
if (_mapShipCollectionGeneric + ship == 1)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + ship == 1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapShipCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@ -154,13 +127,13 @@ namespace WarmlyShip
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos == 1)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
pictureBox.Image =
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonShowStorage_Click(object sender, EventArgs e)

View File

@ -24,21 +24,15 @@ namespace WarmlyShip
_pictureHeight = pictureHeight;
}
private bool inDir(string name)
{
foreach(var key in Keys)
if (key == name) return true;
return false;
}
public void AddMap(string name, AbstractMap map)
{
if (inDir(name)) return;
if (_mapStorages.ContainsKey(name)) return;
_mapStorages.Add(name, new MapWithSetShipGeneric<DrawningObjectShip, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
public void DelMap(string name)
{
if (!inDir(name)) return;
if (_mapStorages.ContainsKey(name)) return;
_mapStorages.Remove(name);
}
@ -46,10 +40,7 @@ namespace WarmlyShip
{
get
{
foreach (var map in _mapStorages)
{
if(map.Key == ind) return map.Value;
}
if(_mapStorages.ContainsKey(ind)) return _mapStorages[ind];
return null;
}
}