diff --git a/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/AbstractCompany.cs b/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/AbstractCompany.cs index 06e2e1d..239ab6b 100644 --- a/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/AbstractCompany.cs +++ b/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/AbstractCompany.cs @@ -53,7 +53,7 @@ public abstract class AbstractCompany _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = collection; - _collection.SetMaxCount = GetMaxCount; + _collection.MaxCount = GetMaxCount; } /// diff --git a/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/ICollectionGenericObjects.cs index b8e38ce..a6d24be 100644 --- a/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/ICollectionGenericObjects.cs +++ b/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -20,7 +20,7 @@ public interface ICollectionGenericObjects /// /// Установка максимального количества элементов /// - int SetMaxCount { set; } + int MaxCount { get; set; } /// /// Добавление объекта в коллекцию @@ -50,5 +50,15 @@ public interface ICollectionGenericObjects /// Позиция /// Объект T? Get(int position); + + /// + /// Получение типа коллекции + /// + CollectionType GetCollectionType { get; } + /// + /// Получение объектов коллекции по одному + /// + /// + IEnumerable GetItems(); } diff --git a/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/ListGenericObjects.cs b/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/ListGenericObjects.cs index ee170b9..3b005ce 100644 --- a/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/ListGenericObjects.cs @@ -22,7 +22,10 @@ public class ListGenericObjects : ICollectionGenericObjects /// private int _maxCount; public int Count => _collection.Count; - public int SetMaxCount { set { if (value > 0) { _maxCount = value; } } } + public int MaxCount { set { if (value > 0) { _maxCount = value; } } get { return _collection.Count; } } + + public CollectionType GetCollectionType => CollectionType.List; + /// /// Конструктор /// @@ -65,4 +68,9 @@ public class ListGenericObjects : ICollectionGenericObjects _collection.RemoveAt(position); return true; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Count; i++) yield return _collection[i]; + } } diff --git a/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/MassiveGenericObjects.cs index 0372d8f..4608263 100644 --- a/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/MassiveGenericObjects.cs +++ b/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/MassiveGenericObjects.cs @@ -20,7 +20,7 @@ public class MassiveGenericObjects : ICollectionGenericObjects public int Count => _collection.Length; - public int SetMaxCount + public int MaxCount { set { @@ -36,8 +36,14 @@ public class MassiveGenericObjects : ICollectionGenericObjects } } } + get + { + return _collection.Length; + } } + public CollectionType GetCollectionType => CollectionType.Massive; + /// /// Конструктор /// @@ -101,4 +107,9 @@ public class MassiveGenericObjects : ICollectionGenericObjects _collection[index] = obj; return true; } + + public IEnumerable GetItems() + { + for (int i = 0; i < _collection.Length; i++) yield return _collection[i]; + } } diff --git a/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/StorageCollection.cs b/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/StorageCollection.cs index a624f56..b383161 100644 --- a/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/StorageCollection.cs +++ b/ProjectCruiser/ProjectCruiser/CollectionGenericObjects/StorageCollection.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectCruiser.Drawings; namespace ProjectCruiser.CollectionGenericObjects; @@ -11,7 +12,7 @@ namespace ProjectCruiser.CollectionGenericObjects; /// /// public class StorageCollection - where T : class + where T : DrawningCruiser { /// /// Словарь (хранилище) с коллекциями @@ -21,6 +22,12 @@ public class StorageCollection /// Возвращение списка названий коллекций /// public List Keys => _storages.Keys.ToList(); + + private readonly string _collectionKey = "CollectionStorage"; + private readonly string _separatorForKeyValue = "|"; + private readonly string _separatorItems = ";"; + + /// /// Конструктор /// @@ -77,4 +84,239 @@ public class StorageCollection return _storages[name]; } } + + /// + /// Запись информации в файл + /// + /// + /// + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + + if (_storages.Count == 0) return false; + StringBuilder sb = new(); + sb.Append(_collectionKey); + + foreach (KeyValuePair> value in _storages) + { + sb.Append(Environment.NewLine); + // Не сохраняем пустые коллекции + if (value.Value.Count == 0) continue; + sb.Append(value.Key); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.GetCollectionType); + sb.Append(_separatorForKeyValue); + sb.Append(value.Value.MaxCount); + sb.Append(_separatorForKeyValue); + foreach (T? item in value.Value.GetItems()) + { + string data = item?.GetDataForSave() ?? string.Empty; + if (string.IsNullOrEmpty(data)) continue; + sb.Append(data); + sb.Append(_separatorItems); + } + } + + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); + fs.Write(info, 0, info.Length); + return true; + } + + ///// + ///// Сохранение информации по кораблям в хранилище в файл + ///// + ///// путь и имя файла + ///// + //public bool SaveData(string filename) + //{ + + // if (_storages.Count == 0) + // { + // return false; + // } + + // if (File.Exists(filename)) + // { + // File.Delete(filename); + // } + + + // StringBuilder sb = new(); + + // sb.Append(_collectionKey); + // foreach (KeyValuePair> value in _storages) + // { + // sb.Append(Environment.NewLine); + // if (value.Value.Count == 0) + // { + // continue; + // } + + // sb.Append(value.Key); + // sb.Append(_separatorForKeyValue); + // sb.Append(value.Value.GetCollectionType); + // sb.Append(_separatorForKeyValue); + // sb.Append(value.Value.MaxCount); + // sb.Append(_separatorForKeyValue); + + // foreach (T? item in value.Value.GetItems()) + // { + // string data = item?.GetDataForSave() ?? string.Empty; + // if (string.IsNullOrEmpty(data)) + // { + // continue; + // } + + // sb.Append(data); + // sb.Append(_separatorItems); + // } + // } + + // using FileStream fs = new(filename, FileMode.Create); + // byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); + // fs.Write(info, 0, info.Length); + // return true; + //} + + + + + ///// + ///// Загрузка информации по установкам в хранилище из файла + ///// + ///// + ///// + //public bool LoadData(string filename) + //{ + // if (!File.Exists(filename)) + // { + // return false; + // } + // using (StreamReader reader = File.OpenText(filename)) + // { + // string str = reader.ReadLine(); + // if (str == null || str.Length == 0) + // { + // return false; + // } + // if (!str.StartsWith(_collectionKey)) + // { + // return false; + // } + // _storages.Clear(); + // string strs = ""; + // while ((strs = reader.ReadLine()) != null) + // { + // string[] record = strs.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?.CreateDrawningCruiser() is T cruiser) + // { + // if (!collection.Insert(cruiser)) + // { + // return false; + // } + // } + // } + // _storages.Add(record[0], collection); + // } + // return true; + // } + //} + + /// + /// Загрузка информации по кораблям в хранилище из файла + /// + /// + /// + public bool LoadData(string filename) + { + if (!File.Exists(filename)) + { + return false; + } + string bufferTextFromFile = ""; + using (FileStream fs = new(filename, FileMode.Open)) + { + byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true); + while (fs.Read(b, 0, b.Length) > 0) + { + bufferTextFromFile += temp.GetString(b); + } + } + + string[] strs = bufferTextFromFile.Split(new char[] { '\n', '\r', }, StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + return false; + } + if (!strs[0].Equals(_collectionKey)) + + { + return false; + } + + _storages.Clear(); + foreach (string data in strs) + { + string[] record = data.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?.CreateDrawningCruiser() is T cruiser) + { + if (!collection.Insert(cruiser)) + { + 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/ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs b/ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs index e3cdcbb..f2d5649 100644 --- a/ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs +++ b/ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ProjectCruiser.Entities; +using ProjectCruiser.Entities; namespace ProjectCruiser.Drawings; @@ -92,6 +87,11 @@ public class DrawningCruiser _drawingCruiserHeight = drawingCruiserHeight; } + public DrawningCruiser(EntityCruiser cruiser) + { + EntityCruiser = cruiser; + } + public bool SetPictireSize(int width, int height) { _pictureWidth = width; diff --git a/ProjectCruiser/ProjectCruiser/Drawings/DrawningMilitaryCruiser.cs b/ProjectCruiser/ProjectCruiser/Drawings/DrawningMilitaryCruiser.cs index d8396e6..2e32e6b 100644 --- a/ProjectCruiser/ProjectCruiser/Drawings/DrawningMilitaryCruiser.cs +++ b/ProjectCruiser/ProjectCruiser/Drawings/DrawningMilitaryCruiser.cs @@ -10,6 +10,11 @@ namespace ProjectCruiser.Drawings; public class DrawningMilitaryCruiser: DrawningCruiser { + + public DrawningMilitaryCruiser(EntityCruiser cruiser) : base(cruiser) + { + } + /// /// Конструктор /// diff --git a/ProjectCruiser/ProjectCruiser/Drawings/ExtentionDrawningCruiser.cs b/ProjectCruiser/ProjectCruiser/Drawings/ExtentionDrawningCruiser.cs new file mode 100644 index 0000000..0782f7f --- /dev/null +++ b/ProjectCruiser/ProjectCruiser/Drawings/ExtentionDrawningCruiser.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectCruiser.Entities; + +namespace ProjectCruiser.Drawings; + +public static class ExtentionDrawningCruiser +{ + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly string _separatorForObject = ":"; + + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Объект + public static DrawningCruiser? CreateDrawningCruiser(this string info) + { + string[] strs = info.Split(_separatorForObject); + EntityCruiser? cruiser = EntityMilitaryCruiser.CreateEntityMilitaryCruiser(strs); + if (cruiser != null) + { + return new DrawningMilitaryCruiser(cruiser); + } + cruiser = EntityCruiser.CreateEntityCruiser(strs); + if (cruiser != null) + { + return new DrawningCruiser(cruiser); + } + return null; + } + + ///// + ///// Создание обьекта в зависимости от выбранного типа + ///// + ///// + ///// + //public static DrawningCruiser? CreateDrawningCruiser(this string info) + //{ + // string[] strs = info.Split(_separatorForObject); + // EntityMilitaryCruiser? militaryCruiser = EntityMilitaryCruiser.CreateEntityMilitaryCruiser(strs); + // if (militaryCruiser != null) + // { + // return new DrawningMilitaryCruiser(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]), Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); + // } + + // EntityCruiser? cruiser = EntityCruiser.CreateEntityCruiser(strs); + // if (cruiser != null) + // { + // return new DrawningCruiser(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + // } + + // return null; + + //} + + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Строка с данными по объекту + public static string GetDataForSave(this DrawningCruiser drawningCruiser) + { + string[]? array = drawningCruiser?.EntityCruiser?.GetStringRepresentation(); + if (array == null) + { + return string.Empty; + } + return string.Join(_separatorForObject, array); + } +} diff --git a/ProjectCruiser/ProjectCruiser/Entities/EntityCruiser.cs b/ProjectCruiser/ProjectCruiser/Entities/EntityCruiser.cs index 64d7055..f6857a5 100644 --- a/ProjectCruiser/ProjectCruiser/Entities/EntityCruiser.cs +++ b/ProjectCruiser/ProjectCruiser/Entities/EntityCruiser.cs @@ -52,4 +52,26 @@ public class EntityCruiser Weigth = weigth; BodyColor = bodyColor; } + + /// + /// Получение строк со значениями свойств объекта класса-сущности + /// + /// + public virtual string[] GetStringRepresentation() + { + return new[] { nameof(EntityCruiser), Speed.ToString(), Weigth.ToString(), BodyColor.Name }; + } + /// + /// Создание объекта из массива строк + /// + /// + /// + public static EntityCruiser? CreateEntityCruiser(string[] strs) + { + if (strs.Length != 4 || strs[0] != nameof(EntityCruiser)) + { + return null; + } + return new EntityCruiser(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3])); + } } diff --git a/ProjectCruiser/ProjectCruiser/Entities/EntityMilitaryCruiser.cs b/ProjectCruiser/ProjectCruiser/Entities/EntityMilitaryCruiser.cs index ad3403f..017b986 100644 --- a/ProjectCruiser/ProjectCruiser/Entities/EntityMilitaryCruiser.cs +++ b/ProjectCruiser/ProjectCruiser/Entities/EntityMilitaryCruiser.cs @@ -1,55 +1,67 @@ -namespace ProjectCruiser.Entities +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace ProjectCruiser.Entities; + +/// +/// Класс-сущность "Военный Крейсер" Вариант 18 +/// +public class EntityMilitaryCruiser: EntityCruiser { /// - /// Класс-сущность "Военный Крейсер" Вариант 18 + /// Дополнительный цвет (для опциональных элементов) /// - public class EntityMilitaryCruiser: EntityCruiser + public Color AdditionalColor { get; private set; } + + public void SetAdditionalColor(Color AdditionalColor) { - /// - /// Дополнительный цвет (для опциональных элементов) - /// - public Color AdditionalColor { get; private set; } + this.AdditionalColor = AdditionalColor; + } - public void SetAdditionalColor(Color AdditionalColor) - { - this.AdditionalColor = AdditionalColor; - } + /// + /// Признак (опция) ракетная шахта + /// + public bool RocketMine { get; private set; } - /// - /// Признак (опция) ракетная шахта - /// - public bool RocketMine { get; private set; } + /// + /// Признак (опция) площадка под вертолет + /// + public bool HelicopterPad { get; private set; } - /// - /// Признак (опция) площадка под вертолет - /// - public bool HelicopterPad { get; private set; } + /// + /// Инициализация полей объекта класса крейсера + /// + /// Скорость + /// Вес крейсера + /// Скорость + /// Дополнительный цвет + /// Признак наличия рокетной шахты + /// Признак наличия площадки под вертолет + public EntityMilitaryCruiser( + int speed, + double weigth, + Color bodyColor, + Color additionalColor, + bool rocketMine, + bool helicopterPad + ) + : base(speed, weigth, bodyColor) + { + Speed = speed; + Weigth = weigth; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + RocketMine = rocketMine; + HelicopterPad = helicopterPad; + } - /// - /// Инициализация полей объекта класса крейсера - /// - /// Скорость - /// Вес крейсера - /// Скорость - /// Дополнительный цвет - /// Признак наличия рокетной шахты - /// Признак наличия площадки под вертолет - public EntityMilitaryCruiser( - int speed, - double weigth, - Color bodyColor, - Color additionalColor, - bool rocketMine, - bool helicopterPad - ) - : base(speed, weigth, bodyColor) - { - Speed = speed; - Weigth = weigth; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - RocketMine = rocketMine; - HelicopterPad = helicopterPad; - } + public override string[] GetStringRepresentation() + { + return new[] { nameof(EntityMilitaryCruiser), Speed.ToString(), Weigth.ToString(), BodyColor.Name, AdditionalColor.ToString(), RocketMine.ToString(), HelicopterPad.ToString() }; + } + + public static EntityMilitaryCruiser? CreateEntityMilitaryCruiser(string[] strs) + { + if (strs.Length != 7 || strs[0] != nameof(EntityMilitaryCruiser)) return null; + return new EntityMilitaryCruiser(Convert.ToInt32(strs[1]), Convert.ToDouble(strs[2]), Color.FromName(strs[3]), Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6])); } } diff --git a/ProjectCruiser/ProjectCruiser/FormCruiserCollection.Designer.cs b/ProjectCruiser/ProjectCruiser/FormCruiserCollection.Designer.cs index 7b6351b..e138398 100644 --- a/ProjectCruiser/ProjectCruiser/FormCruiserCollection.Designer.cs +++ b/ProjectCruiser/ProjectCruiser/FormCruiserCollection.Designer.cs @@ -46,10 +46,17 @@ labelCollectionName = new Label(); comboBoxSelectorCompany = new ComboBox(); pictureBox = new PictureBox(); + menuStrip = new MenuStrip(); + файлToolStripMenuItem = 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(890, 0); + groupBoxTools.Location = new Point(890, 28); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(250, 728); + groupBoxTools.Size = new Size(250, 700); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; @@ -77,7 +84,7 @@ panelCompanyTools.Enabled = false; panelCompanyTools.Location = new Point(3, 431); panelCompanyTools.Name = "panelCompanyTools"; - panelCompanyTools.Size = new Size(244, 294); + panelCompanyTools.Size = new Size(244, 266); panelCompanyTools.TabIndex = 9; // // buttonAddCruiser @@ -93,7 +100,7 @@ // // maskedTextBoxPosition // - maskedTextBoxPosition.Location = new Point(9, 109); + maskedTextBoxPosition.Location = new Point(9, 67); maskedTextBoxPosition.Mask = "00"; maskedTextBoxPosition.Name = "maskedTextBoxPosition"; maskedTextBoxPosition.Size = new Size(232, 27); @@ -103,7 +110,7 @@ // buttonRefresh // buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRefresh.Location = new Point(9, 234); + buttonRefresh.Location = new Point(9, 192); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(226, 40); buttonRefresh.TabIndex = 6; @@ -114,7 +121,7 @@ // buttonRemoveCruiser // buttonRemoveCruiser.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonRemoveCruiser.Location = new Point(9, 142); + buttonRemoveCruiser.Location = new Point(9, 100); buttonRemoveCruiser.Name = "buttonRemoveCruiser"; buttonRemoveCruiser.Size = new Size(226, 40); buttonRemoveCruiser.TabIndex = 4; @@ -125,7 +132,7 @@ // buttonGoToCheck // buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - buttonGoToCheck.Location = new Point(9, 188); + buttonGoToCheck.Location = new Point(9, 146); buttonGoToCheck.Name = "buttonGoToCheck"; buttonGoToCheck.Size = new Size(226, 40); buttonGoToCheck.TabIndex = 5; @@ -239,12 +246,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(890, 728); + pictureBox.Size = new Size(890, 700); pictureBox.TabIndex = 1; pictureBox.TabStop = false; // + // menuStrip + // + menuStrip.ImageScalingSize = new Size(20, 20); + menuStrip.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(1140, 28); + menuStrip.TabIndex = 2; + menuStrip.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { saveToolStripMenuItem, loadToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(59, 24); + файлToolStripMenuItem.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"; + // // FormCruiserCollection // AutoScaleDimensions = new SizeF(8F, 20F); @@ -252,6 +300,8 @@ ClientSize = new Size(1140, 728); Controls.Add(pictureBox); Controls.Add(groupBoxTools); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; Name = "FormCruiserCollection"; Text = "Коллекция Крейсеров"; groupBoxTools.ResumeLayout(false); @@ -260,7 +310,10 @@ panelStorage.ResumeLayout(false); panelStorage.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -283,5 +336,11 @@ private Button buttonCollectionAdd; private RadioButton radioButtonList; private Panel panelCompanyTools; + private MenuStrip menuStrip; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem saveToolStripMenuItem; + private ToolStripMenuItem loadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/ProjectCruiser/ProjectCruiser/FormCruiserCollection.cs b/ProjectCruiser/ProjectCruiser/FormCruiserCollection.cs index 7e680ee..3791d03 100644 --- a/ProjectCruiser/ProjectCruiser/FormCruiserCollection.cs +++ b/ProjectCruiser/ProjectCruiser/FormCruiserCollection.cs @@ -157,7 +157,7 @@ public partial class FormCruiserCollection : Form MessageBox.Show("Коллекция не выбрана"); return; } - ICollectionGenericObjects? collection = + ICollectionGenericObjects? collection = _storageCollection[listBoxCollection.SelectedItem.ToString() ?? string.Empty]; if (collection == null) { @@ -186,4 +186,32 @@ public partial class FormCruiserCollection : 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) + { + //TODO продумать логику + 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/ProjectCruiser/ProjectCruiser/FormCruiserCollection.resx b/ProjectCruiser/ProjectCruiser/FormCruiserCollection.resx index af32865..ee1748a 100644 --- a/ProjectCruiser/ProjectCruiser/FormCruiserCollection.resx +++ b/ProjectCruiser/ProjectCruiser/FormCruiserCollection.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + + + 145, 17 + + + 310, 17 + \ No newline at end of file