From fa9031723a73752c67444d8003ba259f174e9b5d Mon Sep 17 00:00:00 2001 From: Arklightning Date: Sat, 10 Dec 2022 15:08:00 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=93=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Trolleybus/FormMapWithSetTrolleybus.cs | 48 ++++++++----------- Trolleybus/Trolleybus/SetTrolleybusGeneric.cs | 4 ++ .../Trolleybus/StorageOverflowException.cs | 19 ++++++++ Trolleybus/Trolleybus/Trolleybus.csproj | 2 + .../Trolleybus/TrolleybusNotFoundException.cs | 18 +++++++ 5 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 Trolleybus/Trolleybus/StorageOverflowException.cs create mode 100644 Trolleybus/Trolleybus/TrolleybusNotFoundException.cs diff --git a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs index a5fc4fb..654d6b4 100644 --- a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs +++ b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs @@ -131,24 +131,6 @@ namespace Trolleybus formTrolleybusConfig.AddEvent(AddTrolleybus); formTrolleybusConfig.Show(); - /*if (listBoxMaps.SelectedIndex == -1) - { - return; - } - Form1 form = new(); - if (form.ShowDialog() == DialogResult.OK) - { - DrawningObjectTrolleybus tractor = new(form.SelectedTrolleybus); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + tractor != -1) - { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); - } - else - { - MessageBox.Show("Не удалось добавить объект"); - } - }*/ } /// /// Удаление объекта @@ -162,15 +144,26 @@ namespace Trolleybus { return; } - int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) + try { - MessageBox.Show("Объект удален"); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } } - else + catch (TrolleybusNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show($"Ошибка удаления: {ex.Message}"); + } + catch (Exception ex) + { + MessageBox.Show($"Неизвестная ошибка: {ex.Message}"); } } /// @@ -235,13 +228,14 @@ namespace Trolleybus { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) + try { + _mapsCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } - else + catch(Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs b/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs index ff6dbdf..d4317e9 100644 --- a/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs +++ b/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs @@ -29,6 +29,10 @@ namespace Trolleybus // Добавление объекта в набор public int Insert(T trolleybus) { + if (_places.Count > _maxCount) + { + return -1; + } // вставка в начало набора return Insert(trolleybus, 0); } diff --git a/Trolleybus/Trolleybus/StorageOverflowException.cs b/Trolleybus/Trolleybus/StorageOverflowException.cs new file mode 100644 index 0000000..2661901 --- /dev/null +++ b/Trolleybus/Trolleybus/StorageOverflowException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Trolleybus +{ + internal class StorageOverflowException : ApplicationException + { + public StorageOverflowException() : base() { } + public StorageOverflowException(int count) : base($"В наборе превышино количество: {count} элементов") { } + public StorageOverflowException(string message) : base(message) { } + public StorageOverflowException(string message, Exception exception) : base(message, exception) { } + protected StorageOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/Trolleybus/Trolleybus/Trolleybus.csproj b/Trolleybus/Trolleybus/Trolleybus.csproj index 9ded4bf..71ede53 100644 --- a/Trolleybus/Trolleybus/Trolleybus.csproj +++ b/Trolleybus/Trolleybus/Trolleybus.csproj @@ -89,7 +89,9 @@ + + Form1.cs diff --git a/Trolleybus/Trolleybus/TrolleybusNotFoundException.cs b/Trolleybus/Trolleybus/TrolleybusNotFoundException.cs new file mode 100644 index 0000000..02c8b43 --- /dev/null +++ b/Trolleybus/Trolleybus/TrolleybusNotFoundException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Trolleybus +{ + internal class TrolleybusNotFoundException : ApplicationException + { + public TrolleybusNotFoundException() : base() { } + public TrolleybusNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public TrolleybusNotFoundException(string message) : base(message) { } + public TrolleybusNotFoundException(string message, Exception exception) : base(message, exception) { } + protected TrolleybusNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} -- 2.25.1 From 393c33071c6c35930e904dbcfd48762d541b94cd Mon Sep 17 00:00:00 2001 From: Arklightning Date: Sat, 10 Dec 2022 15:58:25 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=9B=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Trolleybus/Trolleybus/App.config | 26 ++++++++- .../Trolleybus/FormMapWithSetTrolleybus.cs | 8 ++- Trolleybus/Trolleybus/Program.cs | 22 ++++++- Trolleybus/Trolleybus/Trolleybus.csproj | 57 +++++++++++++++++++ Trolleybus/Trolleybus/nlog.config | 13 +++++ Trolleybus/Trolleybus/packages.config | 20 +++++++ 6 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 Trolleybus/Trolleybus/nlog.config create mode 100644 Trolleybus/Trolleybus/packages.config diff --git a/Trolleybus/Trolleybus/App.config b/Trolleybus/Trolleybus/App.config index 56efbc7..3cf0b82 100644 --- a/Trolleybus/Trolleybus/App.config +++ b/Trolleybus/Trolleybus/App.config @@ -1,6 +1,30 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs index 654d6b4..1a8bb5d 100644 --- a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs +++ b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -18,9 +19,11 @@ namespace Trolleybus { "Сложная карта", new AutoStopMap() }, }; private readonly MapsCollection _mapsCollection; - public FormMapWithSetTrolleybus() + private readonly ILogger _logger; + public FormMapWithSetTrolleybus(ILogger logger) { InitializeComponent(); + _logger = logger; _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); comboBoxSelectorMap.Items.Clear(); foreach (var elem in _mapsDict) @@ -66,6 +69,7 @@ namespace Trolleybus } _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); ReloadMaps(); + _logger.LogInformation($"Добвлена карта {textBoxNewMapName.Text}"); } private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { diff --git a/Trolleybus/Trolleybus/Program.cs b/Trolleybus/Trolleybus/Program.cs index 7ada713..c54dbda 100644 --- a/Trolleybus/Trolleybus/Program.cs +++ b/Trolleybus/Trolleybus/Program.cs @@ -1,4 +1,7 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -16,7 +19,22 @@ namespace Trolleybus { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new FormMapWithSetTrolleybus()); + var services = new ServiceCollection(); + ConfigureServices(services); + using (ServiceProvider serviceProvider = services.BuildServiceProvider()) + { + Application.Run(serviceProvider.GetRequiredService()); + } + } + + private static void ConfigureServices(ServiceCollection services) + { + services.AddSingleton() + .AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); + }); } } } diff --git a/Trolleybus/Trolleybus/Trolleybus.csproj b/Trolleybus/Trolleybus/Trolleybus.csproj index 71ede53..4263f50 100644 --- a/Trolleybus/Trolleybus/Trolleybus.csproj +++ b/Trolleybus/Trolleybus/Trolleybus.csproj @@ -34,8 +34,63 @@ 4 + + ..\packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll + + + ..\packages\Microsoft.Extensions.Configuration.Abstractions.2.1.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll + + + ..\packages\Microsoft.Extensions.DependencyInjection.7.0.0\lib\net462\Microsoft.Extensions.DependencyInjection.dll + + + ..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.7.0.0\lib\net462\Microsoft.Extensions.DependencyInjection.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Logging.7.0.0\lib\net462\Microsoft.Extensions.Logging.dll + + + ..\packages\Microsoft.Extensions.Logging.Abstractions.7.0.0\lib\net462\Microsoft.Extensions.Logging.Abstractions.dll + + + ..\packages\Microsoft.Extensions.Options.7.0.0\lib\net462\Microsoft.Extensions.Options.dll + + + ..\packages\Microsoft.Extensions.Primitives.7.0.0\lib\net462\Microsoft.Extensions.Primitives.dll + + + ..\packages\NLog.5.1.0\lib\net46\NLog.dll + + + ..\packages\NLog.Extensions.Logging.5.2.0\lib\net461\NLog.Extensions.Logging.dll + + + ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + + + + ..\packages\System.Diagnostics.DiagnosticSource.7.0.0\lib\net462\System.Diagnostics.DiagnosticSource.dll + + + + ..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll + + + + ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + + + ..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll + + + ..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll + @@ -114,6 +169,8 @@ Resources.resx True + + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/Trolleybus/Trolleybus/nlog.config b/Trolleybus/Trolleybus/nlog.config new file mode 100644 index 0000000..4c50da3 --- /dev/null +++ b/Trolleybus/Trolleybus/nlog.config @@ -0,0 +1,13 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Trolleybus/Trolleybus/packages.config b/Trolleybus/Trolleybus/packages.config new file mode 100644 index 0000000..d57f47a --- /dev/null +++ b/Trolleybus/Trolleybus/packages.config @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- 2.25.1 From 28682f5779d2666628377f6a97dc18db0a1cb972 Mon Sep 17 00:00:00 2001 From: Arklightning Date: Mon, 19 Dec 2022 20:07:10 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=9B=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=87=D0=B5?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs | 5 ----- Trolleybus/Trolleybus/Trolleybus.csproj | 4 +++- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs index 1a8bb5d..8eb25ac 100644 --- a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs +++ b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs @@ -41,11 +41,6 @@ namespace Trolleybus listBoxMaps.Items.Add(_mapsCollection.Keys[i]); } - foreach (string map in _mapsCollection.Keys) - { - listBoxMaps.Items.Add(map); - } - if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count)) { listBoxMaps.SelectedIndex = 0; diff --git a/Trolleybus/Trolleybus/Trolleybus.csproj b/Trolleybus/Trolleybus/Trolleybus.csproj index 4263f50..ac636b2 100644 --- a/Trolleybus/Trolleybus/Trolleybus.csproj +++ b/Trolleybus/Trolleybus/Trolleybus.csproj @@ -169,7 +169,9 @@ Resources.resx True - + + Always + SettingsSingleFileGenerator -- 2.25.1