From 01ea98b1c746cc4b6c63e60b140d308adcd5b635 Mon Sep 17 00:00:00 2001 From: Nikita Potapov <47923521+nikita-potapov@users.noreply.github.com> Date: Mon, 14 Nov 2022 19:14:12 +0400 Subject: [PATCH] Event work --- Boats/Boats/BoatDelegate.cs | 13 ++++++++ Boats/Boats/FormBoatConfig.Designer.cs | 41 +++++++++++++------------ Boats/Boats/FormBoatConfig.cs | 31 +++++++++++++++++++ Boats/Boats/FormMapWithSetBoats.cs | 42 +++++++++++++++----------- Boats/Boats/Program.cs | 2 +- 5 files changed, 90 insertions(+), 39 deletions(-) create mode 100644 Boats/Boats/BoatDelegate.cs diff --git a/Boats/Boats/BoatDelegate.cs b/Boats/Boats/BoatDelegate.cs new file mode 100644 index 0000000..4ecc6f4 --- /dev/null +++ b/Boats/Boats/BoatDelegate.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Boats +{ + /// + /// Делегат для передачи объекта-лодки + /// + public delegate void BoatDelegate(DrawingBoat boat); +} diff --git a/Boats/Boats/FormBoatConfig.Designer.cs b/Boats/Boats/FormBoatConfig.Designer.cs index 8a16866..1b7696c 100644 --- a/Boats/Boats/FormBoatConfig.Designer.cs +++ b/Boats/Boats/FormBoatConfig.Designer.cs @@ -46,12 +46,12 @@ this.numericUpDownSpeed = new System.Windows.Forms.NumericUpDown(); this.labelWeight = new System.Windows.Forms.Label(); this.labelSpeed = new System.Windows.Forms.Label(); - this.button1 = new System.Windows.Forms.Button(); + this.buttonOk = new System.Windows.Forms.Button(); this.panelObject = new System.Windows.Forms.Panel(); this.pictureBoxObject = new System.Windows.Forms.PictureBox(); this.labelDopColor = new System.Windows.Forms.Label(); this.labelBaseColor = new System.Windows.Forms.Label(); - this.button2 = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); this.groupBoxConfig.SuspendLayout(); this.groupBoxColors.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDownWeight)).BeginInit(); @@ -251,14 +251,15 @@ this.labelSpeed.TabIndex = 0; this.labelSpeed.Text = "Скорость:"; // - // button1 + // buttonOk // - this.button1.Location = new System.Drawing.Point(595, 216); - this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(119, 29); - this.button1.TabIndex = 7; - this.button1.Text = "Добавить"; - this.button1.UseVisualStyleBackColor = true; + this.buttonOk.Location = new System.Drawing.Point(595, 216); + this.buttonOk.Name = "buttonOk"; + this.buttonOk.Size = new System.Drawing.Size(119, 29); + this.buttonOk.TabIndex = 7; + this.buttonOk.Text = "Добавить"; + this.buttonOk.UseVisualStyleBackColor = true; + this.buttonOk.Click += new System.EventHandler(this.ButtonOk_Click); // // panelObject // @@ -307,24 +308,24 @@ this.labelBaseColor.DragDrop += new System.Windows.Forms.DragEventHandler(this.LabelBaseColor_DragDrop); this.labelBaseColor.DragEnter += new System.Windows.Forms.DragEventHandler(this.LabelBaseColor_DragEnter); // - // button2 + // buttonCancel // - this.button2.Location = new System.Drawing.Point(736, 217); - this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(119, 29); - this.button2.TabIndex = 8; - this.button2.Text = "Отмена"; - this.button2.UseVisualStyleBackColor = true; + this.buttonCancel.Location = new System.Drawing.Point(736, 217); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(119, 29); + this.buttonCancel.TabIndex = 8; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; // // FormBoatConfig // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(888, 258); - this.Controls.Add(this.button2); + this.Controls.Add(this.buttonCancel); this.Controls.Add(this.panelObject); this.Controls.Add(this.groupBoxConfig); - this.Controls.Add(this.button1); + this.Controls.Add(this.buttonOk); this.Name = "FormBoatConfig"; this.Text = "Создание объекта"; this.groupBoxConfig.ResumeLayout(false); @@ -358,11 +359,11 @@ private NumericUpDown numericUpDownSpeed; private Label labelWeight; private Label labelSpeed; - private Button button1; + private Button buttonOk; private Panel panelObject; private PictureBox pictureBoxObject; private Label labelBaseColor; private Label labelDopColor; - private Button button2; + private Button buttonCancel; } } \ No newline at end of file diff --git a/Boats/Boats/FormBoatConfig.cs b/Boats/Boats/FormBoatConfig.cs index 1f29e8e..62e374b 100644 --- a/Boats/Boats/FormBoatConfig.cs +++ b/Boats/Boats/FormBoatConfig.cs @@ -21,6 +21,10 @@ namespace Boats /// DrawingBoat _boat = null; /// + /// Событие + /// + private event BoatDelegate EventAddBoat; + /// /// Конструктор /// public FormBoatConfig() @@ -34,6 +38,8 @@ namespace Boats panelGray.MouseDown += PanelColor_MouseDown; panelBlack.MouseDown += PanelColor_MouseDown; panelNavy.MouseDown += PanelColor_MouseDown; + + buttonCancel.Click += (sender, e) => Close(); } /// /// Отрисовка лодки @@ -47,6 +53,21 @@ namespace Boats pictureBoxObject.Image = bmp; } /// + /// Добавление события + /// + /// + public void AddEvent(BoatDelegate e) + { + if (EventAddBoat == null) + { + EventAddBoat = new BoatDelegate(e); + } + else + { + EventAddBoat += e; + } + } + /// /// Применяем соответствующий объект /// /// @@ -161,5 +182,15 @@ namespace Boats (_boat.Boat as EntityCatamaran).DopColor = labelDopColor.BackColor; DrawBoat(); } + /// + /// Добавление лодки + /// + /// + /// + private void ButtonOk_Click(object sender, EventArgs e) + { + EventAddBoat?.Invoke(_boat); + Close(); + } } } diff --git a/Boats/Boats/FormMapWithSetBoats.cs b/Boats/Boats/FormMapWithSetBoats.cs index 718f797..2b13665 100644 --- a/Boats/Boats/FormMapWithSetBoats.cs +++ b/Boats/Boats/FormMapWithSetBoats.cs @@ -99,24 +99,9 @@ namespace Boats /// private void ButtonAddBoat_Click(object sender, EventArgs e) { - if (listBoxMaps.SelectedIndex == -1) - { - return; - } - FormBoat form = new(); - if (form.ShowDialog() == DialogResult.OK) - { - DrawingObjectBoat boat = new(form.SelectedBoat); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat != -1) - { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - } - else - { - MessageBox.Show("Не удалось добавить объект"); - } - } + var formBoatConfig = new FormBoatConfig(); + formBoatConfig.AddEvent(new BoatDelegate(AddBoatListener)); + formBoatConfig.Show(); } /// /// Удаление объекта @@ -255,5 +240,26 @@ namespace Boats { pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } + /// + /// Listener для добавления новой лодки из формы + /// + /// + private void AddBoatListener(DrawingBoat drawingBoat) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + DrawingObjectBoat boat = new(drawingBoat); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } } } diff --git a/Boats/Boats/Program.cs b/Boats/Boats/Program.cs index 74db210..91dda50 100644 --- a/Boats/Boats/Program.cs +++ b/Boats/Boats/Program.cs @@ -11,7 +11,7 @@ namespace Boats // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormBoatConfig()); + Application.Run(new FormMapWithSetBoats()); } } } \ No newline at end of file