From b04dda62a6317bf1a8443c19535586ddfdaab7b9 Mon Sep 17 00:00:00 2001 From: vkobi Date: Tue, 23 Apr 2024 09:06:31 +0400 Subject: [PATCH 1/2] =?UTF-8?q?6=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 2 +- .../ICollectionGenericObjects.cs | 13 +- .../ListGenericObjects.cs | 24 ++- .../MassiveGenericObjects.cs | 16 +- .../StorageCollection.cs | 151 +++++++++++++++++- .../Drawnings/DrawningLocomotive.cs | 11 ++ .../Drawnings/DrawningWarmlyLocomotive.cs | 9 ++ .../Drawnings/ExtentionDrawningLocomotive.cs | 50 ++++++ .../Entities/EntityLocomotive.cs | 22 +++ .../Entities/EntityWarmlyLocomotive.cs | 22 +++ .../FormLocomotiveCollection.Designer.cs | 81 ++++++++-- .../FormLocomotiveCollection.cs | 42 +++++ .../FormLocomotiveCollection.resx | 12 ++ 13 files changed, 438 insertions(+), 17 deletions(-) create mode 100644 WarmlyLocomotive/WarmlyLocomotive/Drawnings/ExtentionDrawningLocomotive.cs diff --git a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/AbstractCompany.cs b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/AbstractCompany.cs index 236a4bb..ef1388f 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/AbstractCompany.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/AbstractCompany.cs @@ -48,7 +48,7 @@ public abstract class AbstractCompany _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount = GetMaxCount; + _collection.MaxCount = GetMaxCount; } /// diff --git a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ICollectionGenericObjects.cs b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ICollectionGenericObjects.cs index ab634d3..562358f 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -15,7 +15,7 @@ public interface ICollectionGenericObjects /// /// Установка максимального количества элементов /// - int SetMaxCount { set; } + int MaxCount { get; set; } /// /// Добавление объекта в коллекцию @@ -45,4 +45,15 @@ public interface ICollectionGenericObjects /// Позиция /// Объект T? Get(int position); + + /// + /// Получение типа коллекции + /// + CollectionType GetCollectionType { get; } + + /// + /// Получение объектов коллекции по одному + /// + /// Поэлементый вывод элементов коллекции + IEnumerable GetItems(); } \ No newline at end of file diff --git a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ListGenericObjects.cs b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ListGenericObjects.cs index dc38f21..0d6673d 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ListGenericObjects.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/ListGenericObjects.cs @@ -19,7 +19,21 @@ public class ListGenericObjects : ICollectionGenericObjects public int Count => _collection.Count; - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public int MaxCount { + get + { + return _collection.Count; + } + set + { + if (value > 0) + { + _maxCount = value; + } + } + } + + public CollectionType GetCollectionType => CollectionType.List; /// /// Конструктор @@ -79,4 +93,12 @@ public class ListGenericObjects : ICollectionGenericObjects _collection.RemoveAt(position); return obj; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Count; ++i) + { + yield return _collection[i]; + } + } } diff --git a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs index 7d2bf50..3bca1fe 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/MassiveGenericObjects.cs @@ -14,8 +14,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Count => _collection.Length; - public int SetMaxCount + public int MaxCount { + get + { + return _collection.Length; + } set { if (value > 0) @@ -32,6 +36,8 @@ public class MassiveGenericObjects : ICollectionGenericObjects } } + public CollectionType GetCollectionType => CollectionType.Massive; + /// /// Конструктор /// @@ -115,4 +121,12 @@ public class MassiveGenericObjects : ICollectionGenericObjects // TODO удаление объекта из массива, присвоив элементу массива значение null return tmp; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; ++i) + { + yield return _collection[i]; + } + } } diff --git a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/StorageCollection.cs b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/StorageCollection.cs index 97811ca..90440ed 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/StorageCollection.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/CollectionGenericObjects/StorageCollection.cs @@ -1,12 +1,30 @@ -namespace WarmlyLocomotive.CollectionGenericObjects; +using System.Text; +using WarmlyLocomotive.Drawnings; + +namespace WarmlyLocomotive.CollectionGenericObjects; /// /// Класс-хранилище коллекций /// /// public class StorageCollection - where T : class + where T : DrawningLocomotive { + /// + /// Ключевое слово, с которого должен начинаться файл + /// + private readonly string _collectionKey = "CollectionsStorage"; + + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private readonly string _separatorForKeyValue = "|"; + + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly string _separatorItems = ";"; + /// /// Словарь (хранилище) с коллекциями /// @@ -80,4 +98,133 @@ public class StorageCollection } } + /// + /// Сохранение информации по локомотивам в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (_storages.Count == 0) + { + return false; + } + + if (File.Exists(filename)) + { + File.Delete(filename); + } + + //StringBuilder sb = new(); + using FileStream fs = new(filename, FileMode.Create); + using StreamWriter sw = new StreamWriter(fs); + sw.WriteLine(_collectionKey); + //sb.Append(_collectionKey); + foreach (KeyValuePair> value in _storages) + { + sw.Write(Environment.NewLine); + // не сохраняем пустые коллекции + if (value.Value.Count == 0) + { + continue; + } + sw.Write(value.Key); + sw.Write(_separatorForKeyValue); + sw.Write(value.Value.GetCollectionType); + sw.Write(_separatorForKeyValue); + sw.Write(value.Value.MaxCount); + sw.Write(_separatorForKeyValue); + + + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (string.IsNullOrEmpty(data)) + { + continue; + } + + sw.Write(data); + sw.Write(_separatorItems); + } + } + return true; + } + + /// + /// Загрузка информации по локомотивам в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + + + + using (StreamReader reader = new(filename)) + { + string line = reader.ReadLine(); + if (line == null || line.Length == 0) + { + return false; + } + if (!line.Equals(_collectionKey)) + { + return false; + } + _storages.Clear(); + while ((line = reader.ReadLine()) != null) + { + string[] record = line.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 4) + { + continue; + } + + CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]); + ICollectionGenericObjects? collection = StorageCollection.CreateCollection(collectionType); + if (collection == null) + { + return false; + } + + collection.MaxCount = Convert.ToInt32(record[2]); + + string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + if (elem?.CreateDrawningLocomotive() is T locomotive) + { + if (collection.Insert(locomotive) == -1) + { + return false; + } + } + + } + _storages.Add(record[0], collection); + } + } + return true; + } + + /// + /// Создание коллекции по типу + /// + /// + /// + private static ICollectionGenericObjects? CreateCollection(CollectionType collectionType) + { + return collectionType switch + { + CollectionType.Massive => new MassiveGenericObjects(), + CollectionType.List => new ListGenericObjects(), + _ => null, + }; + } + } diff --git a/WarmlyLocomotive/WarmlyLocomotive/Drawnings/DrawningLocomotive.cs b/WarmlyLocomotive/WarmlyLocomotive/Drawnings/DrawningLocomotive.cs index cda06db..daac0c6 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/Drawnings/DrawningLocomotive.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/Drawnings/DrawningLocomotive.cs @@ -76,6 +76,17 @@ public class DrawningLocomotive EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); } + public DrawningLocomotive(EntityLocomotive? entityLocomotive) : this() + { + if(entityLocomotive == null) + { + return; + } + EntityLocomotive = new EntityLocomotive(entityLocomotive.Speed, entityLocomotive.Weight, entityLocomotive.BodyColor); + } + + + /// /// Конструктор для наследников /// diff --git a/WarmlyLocomotive/WarmlyLocomotive/Drawnings/DrawningWarmlyLocomotive.cs b/WarmlyLocomotive/WarmlyLocomotive/Drawnings/DrawningWarmlyLocomotive.cs index c73d3c8..6614826 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/Drawnings/DrawningWarmlyLocomotive.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/Drawnings/DrawningWarmlyLocomotive.cs @@ -21,6 +21,15 @@ public class DrawningWarmlyLocomotive : DrawningLocomotive EntityLocomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, additionalColor, chimney, compartment); } + public DrawningWarmlyLocomotive(EntityWarmlyLocomotive entityWarmlyLocomotive) : base(130, 50) + { + if(entityWarmlyLocomotive == null) + { + return; + } + EntityLocomotive = new EntityWarmlyLocomotive(entityWarmlyLocomotive.Speed, entityWarmlyLocomotive.Weight, entityWarmlyLocomotive.BodyColor, entityWarmlyLocomotive.AdditionalColor, entityWarmlyLocomotive.Chimney, entityWarmlyLocomotive.Compartment); + } + public override void DrawTransport(Graphics g) { if (EntityLocomotive == null || EntityLocomotive is not EntityWarmlyLocomotive warmlyLocmotive || !_startPosX.HasValue || !_startPosY.HasValue) diff --git a/WarmlyLocomotive/WarmlyLocomotive/Drawnings/ExtentionDrawningLocomotive.cs b/WarmlyLocomotive/WarmlyLocomotive/Drawnings/ExtentionDrawningLocomotive.cs new file mode 100644 index 0000000..204f842 --- /dev/null +++ b/WarmlyLocomotive/WarmlyLocomotive/Drawnings/ExtentionDrawningLocomotive.cs @@ -0,0 +1,50 @@ +using WarmlyLocomotive.Entities; + +namespace WarmlyLocomotive.Drawnings; + +/// +/// Расширение для класса EntityLocomotive +/// +public static class ExtentionDrawningLocomotive +{ + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separatorForObject = ":"; + + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Объект + public static DrawningLocomotive? CreateDrawningLocomotive(this string info) + { + string[] strs = info.Split(_separatorForObject); + EntityLocomotive? locomotive = EntityWarmlyLocomotive.CreateEntityWarmlyLocomotive(strs); + if (locomotive != null) + { + return new DrawningWarmlyLocomotive((EntityWarmlyLocomotive)locomotive); + } + locomotive = EntityLocomotive.CreateEntityLocomotive(strs); + if (locomotive != null) + { + return new DrawningLocomotive(locomotive); + } + return null; + } + + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Строка с данными по объекту + public static string GetDataForSave(this DrawningLocomotive drawningLocomotive) + { + string[]? array = drawningLocomotive?.EntityLocomotive?.GetStringRepresentation(); + if (array == null) + { + return string.Empty; + } + return string.Join(_separatorForObject, array); + } +} diff --git a/WarmlyLocomotive/WarmlyLocomotive/Entities/EntityLocomotive.cs b/WarmlyLocomotive/WarmlyLocomotive/Entities/EntityLocomotive.cs index 1e803c2..90f2bbb 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/Entities/EntityLocomotive.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/Entities/EntityLocomotive.cs @@ -42,4 +42,26 @@ public class EntityLocomotive Weight = weight; BodyColor = bodyColor; } + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityLocomotive), Speed.ToString(), Weight.ToString(), BodyColor.Name }; + } + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityLocomotive? CreateEntityLocomotive(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityLocomotive)) + { + return null; + } + return new EntityLocomotive(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } } \ No newline at end of file diff --git a/WarmlyLocomotive/WarmlyLocomotive/Entities/EntityWarmlyLocomotive.cs b/WarmlyLocomotive/WarmlyLocomotive/Entities/EntityWarmlyLocomotive.cs index d02e720..9651fe1 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/Entities/EntityWarmlyLocomotive.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/Entities/EntityWarmlyLocomotive.cs @@ -40,4 +40,26 @@ public class EntityWarmlyLocomotive : EntityLocomotive Chimney = chimney; Compartment = compartment; } + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public override string[] GetStringRepresentation() + { + return new[] { nameof(EntityWarmlyLocomotive), Speed.ToString(), Weight.ToString(), BodyColor.Name, AdditionalColor.Name, Chimney.ToString(), Compartment.ToString()}; + } + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityWarmlyLocomotive? CreateEntityWarmlyLocomotive(string[] strs) + { + if (strs.Length != 7 || strs[0] != nameof(EntityWarmlyLocomotive)) + { + return null; + } + return new EntityWarmlyLocomotive(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]), Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + } } \ No newline at end of file diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.Designer.cs b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.Designer.cs index c12d097..e8362b0 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.Designer.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.Designer.cs @@ -46,10 +46,17 @@ labelCollectionName = new Label(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + menuStrip = new MenuStrip(); + fileToolStripMenuItem = new ToolStripMenuItem(); + saveToolStripMenuItem = new ToolStripMenuItem(); + loadToolStripMenuItem = new ToolStripMenuItem(); + saveFileDialog = new SaveFileDialog(); + openFileDialog = new OpenFileDialog(); groupBoxTools.SuspendLayout(); panelCompanyTools.SuspendLayout(); panelStorage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + menuStrip.SuspendLayout(); SuspendLayout(); // // groupBoxTools @@ -59,9 +66,9 @@ groupBoxTools.Controls.Add(panelStorage); groupBoxTools.Controls.Add(comboBoxSelectorCompany); groupBoxTools.Dock = DockStyle.Right; - groupBoxTools.Location = new Point(821, 0); + groupBoxTools.Location = new Point(821, 28); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(193, 618); + groupBoxTools.Size = new Size(193, 590); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -75,15 +82,15 @@ panelCompanyTools.Controls.Add(buttonGoToCheck); panelCompanyTools.Dock = DockStyle.Bottom; panelCompanyTools.Enabled = false; - panelCompanyTools.Location = new Point(3, 347); + panelCompanyTools.Location = new Point(3, 341); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(187, 268); + panelCompanyTools.Size = new Size(187, 246); panelCompanyTools.TabIndex = 9; // // buttonAddLocomotive // buttonAddLocomotive.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonAddLocomotive.Location = new Point(6, 3); + buttonAddLocomotive.Location = new Point(3, 3); buttonAddLocomotive.Name = "buttonAddLocomotive"; buttonAddLocomotive.Size = new Size(172, 48); buttonAddLocomotive.TabIndex = 1; @@ -93,7 +100,7 @@ // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(6, 111); + maskedTextBoxPosition.Location = new Point(0, 57); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Size = new Size(175, 27); @@ -103,7 +110,7 @@ // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(6, 231); + buttonRefresh.Location = new Point(0, 200); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(172, 34); buttonRefresh.TabIndex = 6; @@ -114,7 +121,7 @@ // buttonRemoveLocomotive // buttonRemoveLocomotive.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveLocomotive.Location = new Point(6, 144); + buttonRemoveLocomotive.Location = new Point(-3, 90); buttonRemoveLocomotive.Name = "buttonRemoveLocomotive"; buttonRemoveLocomotive.Size = new Size(172, 48); buttonRemoveLocomotive.TabIndex = 4; @@ -125,7 +132,7 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(6, 195); + buttonGoToCheck.Location = new Point(0, 144); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(172, 33); buttonGoToCheck.TabIndex = 5; @@ -240,12 +247,53 @@ // pictureBox // pictureBox.Dock = DockStyle.Fill; - pictureBox.Location = new Point(0, 0); + pictureBox.Location = new Point(0, 28); pictureBox.Name = "pictureBox"; - pictureBox.Size = new Size(821, 618); + pictureBox.Size = new Size(821, 590); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // menuStrip + // + menuStrip.ImageScalingSize = new Size(20, 20); + menuStrip.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(1014, 28); + menuStrip.TabIndex = 2; + menuStrip.Text = "menuStrip"; + // + // fileToolStripMenuItem + // + fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + fileToolStripMenuItem.Name = "fileToolStripMenuItem"; + fileToolStripMenuItem.Size = new Size(59, 24); + fileToolStripMenuItem.Text = "Файл"; + // + // saveToolStripMenuItem + // + saveToolStripMenuItem.Name = "saveToolStripMenuItem"; + saveToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.S; + saveToolStripMenuItem.Size = new Size(227, 26); + saveToolStripMenuItem.Text = "Сохранение"; + saveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; + // + // loadToolStripMenuItem + // + loadToolStripMenuItem.Name = "loadToolStripMenuItem"; + loadToolStripMenuItem.ShortcutKeys = Keys.Control | Keys.L; + loadToolStripMenuItem.Size = new Size(227, 26); + loadToolStripMenuItem.Text = "Загрузка"; + loadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; + // + // saveFileDialog + // + saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + openFileDialog.Filter = "txt file | *.txt"; + // // FormLocomotiveCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -253,6 +301,8 @@ ClientSize = new Size(1014, 618); Controls.Add(pictureBox); Controls.Add(groupBoxTools); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormLocomotiveCollection"; Text = "Коллекция локомотивов"; groupBoxTools.ResumeLayout(false); @@ -261,7 +311,10 @@ panelStorage.ResumeLayout(false); panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -284,5 +337,11 @@ private ListBox listBoxCollection; private Button buttonCreateCompany; private Panel panelCompanyTools; + private MenuStrip menuStrip; + private ToolStripMenuItem fileToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs index 820d903..d3790d6 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs @@ -237,4 +237,46 @@ public partial class FormLocomotiveCollection : Form } } } + + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storageCollection.SaveData(saveFileDialog.FileName)) + { + MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else + { + MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + /// + /// Обработка нажатия "Загрузка" + /// + /// + /// + private void LoadToolStripMenuItem_Click(object sender, EventArgs e) + { + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storageCollection.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + RerfreshListBoxItems(); + } + else + { + MessageBox.Show("не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + + } + } + } } diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.resx b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.resx index af32865..f31ceb7 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.resx +++ b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.resx @@ -117,4 +117,16 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 145, 17 + + + 310, 17 + + + 81 + \ No newline at end of file -- 2.25.1 From c241b589f3550bae6879e63e0ad6b49a33b6d412 Mon Sep 17 00:00:00 2001 From: vkobi Date: Tue, 23 Apr 2024 11:00:15 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=D1=81=D1=82=D1=80=D0=BE=D0=B5=D0=BD=D0=BD?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=20=D0=B4=D0=B5=D0=BB=D0=B5=D0=B3=D0=B0=D1=82?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WarmlyLocomotive/FormLocomotiveCollection.cs | 4 ++++ WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveConfig.cs | 6 ++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs index d3790d6..4574a58 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveCollection.cs @@ -44,6 +44,10 @@ public partial class FormLocomotiveCollection : Form /// private void ButtonAddLocomotive_Click(object sender, EventArgs e) { + if(_company == null) + { + return; + } FormLocomotiveConfig form = new(); // TODO передать метод form.AddEvent(SetLocomotive); diff --git a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveConfig.cs b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveConfig.cs index 03a0dec..4398f0b 100644 --- a/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveConfig.cs +++ b/WarmlyLocomotive/WarmlyLocomotive/FormLocomotiveConfig.cs @@ -16,7 +16,7 @@ public partial class FormLocomotiveConfig : Form /// /// Событие для передачи объекта /// - private event LocomotiveDelegate? LocomotiveDelegate; + private event Action? LocomotiveDelegate; /// /// Конструктор @@ -34,15 +34,13 @@ public partial class FormLocomotiveConfig : Form panelBlue.MouseDown += Panel_MouseDown; buttonCancel.Click += (events, e) => Close(); - - } /// /// Привязка внешнего метода к событию /// /// - public void AddEvent(LocomotiveDelegate locomotiveDelegate) + public void AddEvent(Action locomotiveDelegate) { LocomotiveDelegate += locomotiveDelegate; } -- 2.25.1