From d3d75cc1e3e87e33e4e35fb44c8d25cd53117673 Mon Sep 17 00:00:00 2001 From: Danil Kargin Date: Wed, 2 Nov 2022 23:12:38 +0400 Subject: [PATCH] Event work --- AirFighter/AirFighter/FormAirFighterConfig.cs | 31 +++++++++++++++++++ .../AirFighter/FormMapWithSetAirFighters.cs | 25 +++++++++------ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/AirFighter/AirFighter/FormAirFighterConfig.cs b/AirFighter/AirFighter/FormAirFighterConfig.cs index 9f0654c..1e8f2a1 100644 --- a/AirFighter/AirFighter/FormAirFighterConfig.cs +++ b/AirFighter/AirFighter/FormAirFighterConfig.cs @@ -17,6 +17,10 @@ namespace AirFighter /// DrawningAirFighter _airFighter = null; /// + /// Событие + /// + private event Action EventAddAirFighter; + /// /// Конструктор /// public FormAirFighterConfig() @@ -30,6 +34,8 @@ namespace AirFighter panelWhite.MouseDown += PanelColor_MouseDown; panelYellow.MouseDown += PanelColor_MouseDown; panelBlue.MouseDown += PanelColor_MouseDown; + // Закрытие формы + buttonCancel.Click += (object sender, EventArgs e) => this.Close(); } /// /// Отрисовать объект @@ -43,6 +49,21 @@ namespace AirFighter pictureBoxObject.Image = bmp; } /// + /// Добавление события + /// + /// + public void AddEvent(Action ev) + { + if (EventAddAirFighter == null) + { + EventAddAirFighter = new Action(ev); + } + else + { + EventAddAirFighter += ev; + } + } + /// /// Передаем информацию при нажатии на Label /// /// @@ -143,5 +164,15 @@ namespace AirFighter } } } + /// + /// Добавление самолета + /// + /// + /// + private void ButtonOk_Click(object sender, EventArgs e) + { + EventAddAirFighter?.Invoke(_airFighter); + Close(); + } } } diff --git a/AirFighter/AirFighter/FormMapWithSetAirFighters.cs b/AirFighter/AirFighter/FormMapWithSetAirFighters.cs index 78e5763..616da9e 100644 --- a/AirFighter/AirFighter/FormMapWithSetAirFighters.cs +++ b/AirFighter/AirFighter/FormMapWithSetAirFighters.cs @@ -59,21 +59,15 @@ namespace AirFighter listBoxMaps.SelectedIndex = index; } } - /// - /// Добавление объекта - /// - /// - /// - private void ButtonAddAirFighter_Click(object sender, EventArgs e) + void AddAirFighter(DrawningAirFighter _airFighter) { if (listBoxMaps.SelectedIndex == -1) { return; } - FormAirFighter form = new(); - if (form.ShowDialog() == DialogResult.OK) + if (_airFighter != null) { - DrawningObjectAirFighter airFighter = new(form.SelectedAirFighter); + DrawningObjectAirFighter airFighter = new(_airFighter); if ((_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airFighter) == 0) { MessageBox.Show("Объект добавлен"); @@ -86,6 +80,19 @@ namespace AirFighter } } /// + /// Добавление объекта + /// + /// + /// + private void ButtonAddAirFighter_Click(object sender, EventArgs e) + { + var formAirFighterConfig = new FormAirFighterConfig(); + Action airFighterDelegate; + airFighterDelegate = new(AddAirFighter); + formAirFighterConfig.AddEvent(airFighterDelegate); + formAirFighterConfig.Show(); + } + /// /// Удаление объекта /// ///