Пятая лабораторая работа. Работа с event

This commit is contained in:
abazov73 2022-11-01 12:58:49 +04:00
parent f88b41849d
commit 8463082f80
4 changed files with 39 additions and 15 deletions

View File

@ -80,6 +80,7 @@
this.buttonOk.TabIndex = 8; this.buttonOk.TabIndex = 8;
this.buttonOk.Text = "Добавить"; this.buttonOk.Text = "Добавить";
this.buttonOk.UseVisualStyleBackColor = true; this.buttonOk.UseVisualStyleBackColor = true;
this.buttonOk.Click += new System.EventHandler(this.buttonOk_Click);
// //
// panelObject // panelObject
// //

View File

@ -14,6 +14,8 @@ namespace AirBomber
{ {
DrawingAirBomber _airBomber = null; DrawingAirBomber _airBomber = null;
private event Action<DrawingAirBomber> EventAddAirBomber;
public FormAirBomberConfig() public FormAirBomberConfig()
{ {
InitializeComponent(); InitializeComponent();
@ -25,6 +27,7 @@ namespace AirBomber
panelWhite.MouseDown += PanelColor_MouseDown; panelWhite.MouseDown += PanelColor_MouseDown;
panelYellow.MouseDown += PanelColor_MouseDown; panelYellow.MouseDown += PanelColor_MouseDown;
panelBlue.MouseDown += PanelColor_MouseDown; panelBlue.MouseDown += PanelColor_MouseDown;
buttonCancel.Click += (object sender, EventArgs e) => Close();
} }
private void DrawAirBomber() private void DrawAirBomber()
@ -53,6 +56,18 @@ namespace AirBomber
} }
} }
public void AddEvent(Action<DrawingAirBomber> ev)
{
if (EventAddAirBomber == null)
{
EventAddAirBomber = ev;
}
else
{
EventAddAirBomber += ev;
}
}
private void panelObject_DragDrop(object sender, DragEventArgs e) private void panelObject_DragDrop(object sender, DragEventArgs e)
{ {
switch (e.Data.GetData(DataFormats.Text).ToString()) switch (e.Data.GetData(DataFormats.Text).ToString())
@ -75,7 +90,7 @@ namespace AirBomber
private void labelColor_DragEnter(object sender, DragEventArgs e) private void labelColor_DragEnter(object sender, DragEventArgs e)
{ {
if (e.Data.GetDataPresent(typeof(Color))) if (e.Data.GetDataPresent(typeof(String)))
{ {
e.Effect = DragDropEffects.Copy; e.Effect = DragDropEffects.Copy;
} }
@ -88,6 +103,7 @@ namespace AirBomber
private void labelBaseColor_DragDrop(object sender, DragEventArgs e) private void labelBaseColor_DragDrop(object sender, DragEventArgs e)
{ {
_airBomber?.AirBomber?.setColor(Color.FromName(e.Data.GetData(DataFormats.Text).ToString())); _airBomber?.AirBomber?.setColor(Color.FromName(e.Data.GetData(DataFormats.Text).ToString()));
DrawAirBomber();
} }
private void labelDopColor_DragDrop(object sender, DragEventArgs e) private void labelDopColor_DragDrop(object sender, DragEventArgs e)
@ -95,7 +111,14 @@ namespace AirBomber
if (_airBomber.AirBomber is EntityHeavyAirBomber heavyAirBomber) if (_airBomber.AirBomber is EntityHeavyAirBomber heavyAirBomber)
{ {
heavyAirBomber.setDopColor(Color.FromName(e.Data.GetData(DataFormats.Text).ToString())); heavyAirBomber.setDopColor(Color.FromName(e.Data.GetData(DataFormats.Text).ToString()));
DrawAirBomber();
} }
} }
private void buttonOk_Click(object sender, EventArgs e)
{
EventAddAirBomber?.Invoke(_airBomber);
Close();
}
} }
} }

View File

@ -27,6 +27,10 @@ namespace AirBomber
/// </summary> /// </summary>
private readonly MapsCollection _mapsCollection; private readonly MapsCollection _mapsCollection;
private Action<DrawingAirBomber> AddAction;
private DrawingObjectAirBomber returnedAirBomber;
public FormMapWithSetAirBombers() public FormMapWithSetAirBombers()
{ {
InitializeComponent(); InitializeComponent();
@ -36,6 +40,8 @@ namespace AirBomber
{ {
comboBoxSelectorMap.Items.Add(elem.Key); comboBoxSelectorMap.Items.Add(elem.Key);
} }
AddAction = airBomber => { _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].add(new DrawingObjectAirBomber(airBomber));
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();};
} }
private void buttonAddAirBomber_Click(object sender, EventArgs e) private void buttonAddAirBomber_Click(object sender, EventArgs e)
@ -44,20 +50,9 @@ namespace AirBomber
{ {
return; return;
} }
FormAirBomber form = new(); var formAirBomberConfig = new FormAirBomberConfig();
if (form.ShowDialog() == DialogResult.OK) formAirBomberConfig.AddEvent(AddAction);
{ formAirBomberConfig.Show();
DrawingObjectAirBomber airBomber = new(form.SelectedAirBomber);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airBomber != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
} }
private void buttonRemoveAirBomber_Click(object sender, EventArgs e) private void buttonRemoveAirBomber_Click(object sender, EventArgs e)

View File

@ -69,6 +69,11 @@ namespace AirBomber
{ {
return map._setAirBombers.Remove(position); return map._setAirBombers.Remove(position);
} }
public void add(T airBomber)
{
_setAirBombers.Insert(airBomber);
}
/// <summary> /// <summary>
/// Вывод всего набора объектов /// Вывод всего набора объектов
/// </summary> /// </summary>