From 683a6c138240d38dbb7b3c909071f2e993611cd5 Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 14 Nov 2023 00:48:15 +0400 Subject: [PATCH 1/8] create branch 6 for lab 6 --- .../ProjectElectricLocomotive/FormLocomotiveCollections.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs index 7d4cae9..c04158a 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs @@ -15,6 +15,7 @@ using System.Windows.Forms; namespace ProjectElectricLocomotive { + //create 6 branch public partial class FormLocomotiveCollections : Form { private readonly LocomotiveGenericStorage _storage; -- 2.25.1 From 250ef4516fa85c219059af039a721370621868b4 Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 14 Nov 2023 00:59:34 +0400 Subject: [PATCH 2/8] create extension (ExtentionDrawingLocomotive) --- .../ExtentionDrawningCar.cs | 79 +++++++++++++++++++ .../FormLocomotiveCollections.cs | 1 - 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs new file mode 100644 index 0000000..e9858b2 --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs @@ -0,0 +1,79 @@ +using ProjectElectricLocomotive.DrawingObjects; +using ProjectElectricLocomotive.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive +{ + /// + /// Расширение для класса EntityLocomotive + /// + public static class ExtentionDrawingLocomotive + { + /// + /// Создание объекта из строки + /// + /// Строка с данными для создания объекта + /// Разделитель даннных + /// Ширина + /// Высота + /// Объект + public static DrawingLocomotive? CreateDrawingLocomotive(this string info, char + separatorForObject, int width, int height) + { + string[] strs = info.Split(separatorForObject); + if (strs.Length == 3) + { + return new DrawingLocomotive( + Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + width, + height + ); + } + if (strs.Length == 7) + { + return new DrawingElectricLocomotive( + Convert.ToInt32(strs[0]), + Convert.ToInt32(strs[1]), + Color.FromName(strs[2]), + Color.FromName(strs[3]), + Convert.ToBoolean(strs[4]), + Convert.ToBoolean(strs[5]), + width, + height + ); + } + return null; + } + /// + /// Получение данных для сохранения в файл + /// + /// Сохраняемый объект + /// Разделитель даннных + /// Строка с данными по объекту + public static string GetDataForSave(this DrawingLocomotive drawningLoco, char separatorForObject) + { + var loco = drawningLoco.EntityLocomotive; + if (loco == null) + { + return string.Empty; + } + var str = + $"{loco.Speed}{separatorForObject}{loco.Weight}{separatorForObject}{loco.BodyColor.Name}"; + if (loco is not EntityElectricLocomotive electroLoco) + { + return str; + } + return + $"{str}{separatorForObject}{electroLoco.AdditionalColor.Name}{separatorForObject}" + + $"{electroLoco.Horns}{separatorForObject}{electroLoco.SeifBatteries}" /*+ + $"{separatorForObject}{electroLoco.SportLine}"*/; + } + + } +} diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs index c04158a..7d4cae9 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs @@ -15,7 +15,6 @@ using System.Windows.Forms; namespace ProjectElectricLocomotive { - //create 6 branch public partial class FormLocomotiveCollections : Form { private readonly LocomotiveGenericStorage _storage; -- 2.25.1 From d397203f80c530c27e2b9b76c8070c3288e19500 Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 14 Nov 2023 01:15:07 +0400 Subject: [PATCH 3/8] some additions in storage class --- .../LocomotiveGenericCollection.cs | 6 + .../LocomotiveGenericStorage.cs | 111 +++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs index ecb9eaf..fcbb498 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs @@ -10,6 +10,12 @@ namespace ProjectElectricLocomotive.Generics { internal class LocomotiveGenericCollection where T : DrawingLocomotive where U : IMoveableObject { + /// + /// Получение объектов коллекции + /// + public IEnumerable GetLocomotives => _collection.GetLocomotives(); + + //ширина/высота окна private readonly int _pictureWidth; private readonly int _pictureHeight; diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs index 63704a9..8c87e67 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs @@ -20,9 +20,9 @@ namespace ProjectElectricLocomotive.Generics /// public List Keys => _locomotivesStorage.Keys.ToList(); - + private readonly int _pictureWidth; - + private readonly int _pictureHeight; /// @@ -78,5 +78,112 @@ namespace ProjectElectricLocomotive.Generics return null; } } + + /// + /// Разделитель для записи ключа и значения элемента словаря + /// + private static readonly char _separatorForKeyValue = '|'; + /// + /// Разделитель для записей коллекции данных в файл + /// + private readonly char _separatorRecords = ';'; + /// + /// Разделитель для записи информации по объекту в файл + /// + private static readonly char _separatorForObject = ':'; + + /// + /// Сохранение информации по автомобилям в хранилище в файл + /// + /// Путь и имя файла + /// true - сохранение прошло успешно, false - ошибка при сохранении данных + public bool SaveData(string filename) + { + if (File.Exists(filename)) + { + File.Delete(filename); + } + StringBuilder data = new(); + foreach (KeyValuePair> record in _locomotivesStorage) + { + StringBuilder records = new(); + foreach (DrawingLocomotive? elem in record.Value.GetLocomotives) + { + records.Append($"{elem?.GetDataForSave(_separatorForObject)}{_separatorRecords}"); + } + data.AppendLine($"{record.Key}{_separatorForKeyValue}{records}"); + } + if (data.Length == 0) + { + return false; + } + using FileStream fs = new(filename, FileMode.Create); + byte[] info = new + UTF8Encoding(true).GetBytes($"LocomotiveStorage{Environment.NewLine}{data}"); + fs.Write(info, 0, info.Length); + return true; + } + /// + /// Загрузка информации по автомобилям в хранилище из файла + /// + /// Путь и имя файла + /// true - загрузка прошла успешно, false - ошибка при загрузке данных + 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); + } + } + var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + StringSplitOptions.RemoveEmptyEntries); + if (strs == null || strs.Length == 0) + { + return false; + } + if (!strs[0].StartsWith("LocomotiveStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + _locomotivesStorage.Clear(); + foreach (string data in strs) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + LocomotiveGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingLocomotive? loco = + elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); + if (loco != null) + { + if ((collection + loco) != -1) // or vice versa + { + return false; + } + } + } + _locomotivesStorage.Add(record[0], collection); + } + return true; + } } } -- 2.25.1 From aee4ec8c0e96b078a0967763acf627211d7354c5 Mon Sep 17 00:00:00 2001 From: ekallin Date: Wed, 15 Nov 2023 20:35:55 +0400 Subject: [PATCH 4/8] Some modifies with formLocoCollection --- .../FormLocomotiveCollections.Designer.cs | 82 ++++++++++++++++--- .../FormLocomotiveCollections.cs | 30 +++++++ .../FormLocomotiveCollections.resx | 11 ++- 3 files changed, 110 insertions(+), 13 deletions(-) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs index fe4e41c..3ff35c4 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs @@ -42,11 +42,18 @@ this.ButtonAddObject = new System.Windows.Forms.Button(); this.ButtonRemoveObject = new System.Windows.Forms.Button(); this.Instruments = new System.Windows.Forms.GroupBox(); + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); + this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollections)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.bindingSource2)).BeginInit(); this.groupBox1.SuspendLayout(); this.Instruments.SuspendLayout(); + this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // maskedTextBoxNumber @@ -56,14 +63,14 @@ this.maskedTextBoxNumber.Location = new System.Drawing.Point(38, 532); this.maskedTextBoxNumber.Mask = "0"; this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(149, 27); + this.maskedTextBoxNumber.Size = new System.Drawing.Size(156, 27); this.maskedTextBoxNumber.TabIndex = 4; // // ButtonRefreshCollection // this.ButtonRefreshCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.ButtonRefreshCollection.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonRefreshCollection.Location = new System.Drawing.Point(38, 665); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(45, 663); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; this.ButtonRefreshCollection.Size = new System.Drawing.Size(150, 41); this.ButtonRefreshCollection.TabIndex = 2; @@ -74,7 +81,7 @@ // this.ButtonRemoveLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.ButtonRemoveLocomotive.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonRemoveLocomotive.Location = new System.Drawing.Point(38, 592); + this.ButtonRemoveLocomotive.Location = new System.Drawing.Point(45, 590); this.ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive"; this.ButtonRemoveLocomotive.Size = new System.Drawing.Size(150, 44); this.ButtonRemoveLocomotive.TabIndex = 1; @@ -86,7 +93,7 @@ // this.ButtonAddLocomotive.Anchor = System.Windows.Forms.AnchorStyles.Top; this.ButtonAddLocomotive.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonAddLocomotive.Location = new System.Drawing.Point(38, 461); + this.ButtonAddLocomotive.Location = new System.Drawing.Point(42, 461); this.ButtonAddLocomotive.Name = "ButtonAddLocomotive"; this.ButtonAddLocomotive.Size = new System.Drawing.Size(150, 39); this.ButtonAddLocomotive.TabIndex = 0; @@ -96,12 +103,10 @@ // // pictureBoxCollections // - this.pictureBoxCollections.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.pictureBoxCollections.Location = new System.Drawing.Point(-1, 5); + this.pictureBoxCollections.Anchor = System.Windows.Forms.AnchorStyles.Left; + this.pictureBoxCollections.Location = new System.Drawing.Point(-1, 27); this.pictureBoxCollections.Name = "pictureBoxCollections"; - this.pictureBoxCollections.Size = new System.Drawing.Size(798, 721); + this.pictureBoxCollections.Size = new System.Drawing.Size(1104, 692); this.pictureBoxCollections.TabIndex = 1; this.pictureBoxCollections.TabStop = false; // @@ -165,27 +170,71 @@ // // Instruments // + this.Instruments.Anchor = System.Windows.Forms.AnchorStyles.Right; this.Instruments.Controls.Add(this.ButtonRefreshCollection); this.Instruments.Controls.Add(this.groupBox1); this.Instruments.Controls.Add(this.maskedTextBoxNumber); this.Instruments.Controls.Add(this.ButtonAddLocomotive); this.Instruments.Controls.Add(this.ButtonRemoveLocomotive); - this.Instruments.Location = new System.Drawing.Point(797, 5); + this.Instruments.Location = new System.Drawing.Point(1102, 0); this.Instruments.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Instruments.Name = "Instruments"; this.Instruments.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.Instruments.Size = new System.Drawing.Size(229, 721); + this.Instruments.Size = new System.Drawing.Size(236, 719); this.Instruments.TabIndex = 6; this.Instruments.TabStop = false; this.Instruments.Text = "Инструменты"; // + // menuStrip1 + // + this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.файлToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(1338, 28); + this.menuStrip1.TabIndex = 7; + this.menuStrip1.Text = "menuStrip1"; + // + // файлToolStripMenuItem + // + this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.SaveToolStripMenuItem, + this.LoadToolStripMenuItem}); + this.файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + this.файлToolStripMenuItem.Size = new System.Drawing.Size(59, 24); + this.файлToolStripMenuItem.Text = "Файл"; + // + // SaveToolStripMenuItem + // + this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.SaveToolStripMenuItem.Text = "Сохранить"; + // + // LoadToolStripMenuItem + // + this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.LoadToolStripMenuItem.Text = "Загрузить"; + // + // saveFileDialog + // + this.saveFileDialog.Filter = "txt file | *.txt"; + // + // openFileDialog + // + this.openFileDialog.FileName = "openFileDialog1"; + this.openFileDialog.Filter = "txt file | *.txt"; + // // FormLocomotiveCollections // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1033, 724); + this.ClientSize = new System.Drawing.Size(1338, 724); this.Controls.Add(this.Instruments); this.Controls.Add(this.pictureBoxCollections); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; this.Name = "FormLocomotiveCollections"; this.Text = "Набор локомотивов"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollections)).EndInit(); @@ -195,7 +244,10 @@ this.groupBox1.PerformLayout(); this.Instruments.ResumeLayout(false); this.Instruments.PerformLayout(); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); this.ResumeLayout(false); + this.PerformLayout(); } @@ -213,5 +265,11 @@ private Button ButtonAddObject; private Button ButtonRemoveObject; private GroupBox Instruments; + private MenuStrip menuStrip1; + private ToolStripMenuItem файлToolStripMenuItem; + private ToolStripMenuItem SaveToolStripMenuItem; + private ToolStripMenuItem LoadToolStripMenuItem; + private SaveFileDialog saveFileDialog; + private OpenFileDialog openFileDialog; } } \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs index 7d4cae9..00ce712 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs @@ -143,5 +143,35 @@ namespace ProjectElectricLocomotive } pictureBoxCollections.Image = obj.ShowLocomotives(); } + /// + /// Обработка нажатия "Сохранение" + /// + /// + /// + private void SaveToolStripMenuItem_Click(object sender, EventArgs e) + { + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.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 продумать логику + } } } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx index 4d89b60..170c13e 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx @@ -63,7 +63,16 @@ 154, 17 + + 318, 17 + + + 453, 17 + + + 615, 17 + - 25 + 144 \ No newline at end of file -- 2.25.1 From e3b1101da18449e91070bd2bd5906429765112b1 Mon Sep 17 00:00:00 2001 From: ekallin Date: Thu, 16 Nov 2023 21:33:46 +0400 Subject: [PATCH 5/8] save data with stream writer --- .../ExtentionDrawningCar.cs | 2 +- .../FormLocomotiveCollections.Designer.cs | 2 + .../FormLocomotiveCollections.cs | 13 +++ .../FormLocomotiveConfig.Designer.cs | 18 ++-- .../FormLocomotiveConfig.cs | 2 - .../LocomotiveGenericStorage.cs | 82 ++++++++++++++++--- 6 files changed, 94 insertions(+), 25 deletions(-) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs index e9858b2..788893c 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs @@ -35,7 +35,7 @@ namespace ProjectElectricLocomotive height ); } - if (strs.Length == 7) + if (strs.Length == 6) { return new DrawingElectricLocomotive( Convert.ToInt32(strs[0]), diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs index 3ff35c4..29e2812 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs @@ -210,12 +210,14 @@ this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); this.SaveToolStripMenuItem.Text = "Сохранить"; + this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); // // LoadToolStripMenuItem // this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); this.LoadToolStripMenuItem.Text = "Загрузить"; + this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); // // saveFileDialog // diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs index 00ce712..d2a4f71 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs @@ -172,6 +172,19 @@ namespace ProjectElectricLocomotive private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { // TODO продумать логику + if(openFileDialog.ShowDialog() == DialogResult.OK) + { + if (_storage.LoadData(openFileDialog.FileName)) + { + MessageBox.Show("Load is done!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); + ReloadObjects(); + } + else + { + MessageBox.Show("Load is fail!", "Result", MessageBoxButtons.OK, MessageBoxIcon.Error); + + } + } } } } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs index b305860..9f8b1fa 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.Designer.cs @@ -205,7 +205,7 @@ // panelPastelViolet // this.panelPastelViolet.AllowDrop = true; - this.panelPastelViolet.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(255))))); + this.panelPastelViolet.BackColor = System.Drawing.Color.Plum; this.panelPastelViolet.Location = new System.Drawing.Point(281, 110); this.panelPastelViolet.Name = "panelPastelViolet"; this.panelPastelViolet.Size = new System.Drawing.Size(40, 40); @@ -214,7 +214,7 @@ // panelPastelLilac // this.panelPastelLilac.AllowDrop = true; - this.panelPastelLilac.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255))))); + this.panelPastelLilac.BackColor = System.Drawing.Color.RoyalBlue; this.panelPastelLilac.Location = new System.Drawing.Point(207, 110); this.panelPastelLilac.Name = "panelPastelLilac"; this.panelPastelLilac.Size = new System.Drawing.Size(40, 40); @@ -223,7 +223,7 @@ // panelSkyBlue // this.panelSkyBlue.AllowDrop = true; - this.panelSkyBlue.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))); + this.panelSkyBlue.BackColor = System.Drawing.Color.Turquoise; this.panelSkyBlue.Location = new System.Drawing.Point(132, 110); this.panelSkyBlue.Name = "panelSkyBlue"; this.panelSkyBlue.Size = new System.Drawing.Size(40, 40); @@ -232,7 +232,7 @@ // panelPastelGreen // this.panelPastelGreen.AllowDrop = true; - this.panelPastelGreen.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); + this.panelPastelGreen.BackColor = System.Drawing.Color.PaleGreen; this.panelPastelGreen.Location = new System.Drawing.Point(55, 110); this.panelPastelGreen.Name = "panelPastelGreen"; this.panelPastelGreen.Size = new System.Drawing.Size(40, 40); @@ -241,7 +241,7 @@ // panelPastelYellow // this.panelPastelYellow.AllowDrop = true; - this.panelPastelYellow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); + this.panelPastelYellow.BackColor = System.Drawing.Color.LightYellow; this.panelPastelYellow.Location = new System.Drawing.Point(281, 35); this.panelPastelYellow.Name = "panelPastelYellow"; this.panelPastelYellow.Size = new System.Drawing.Size(40, 40); @@ -250,7 +250,7 @@ // panelPastelOrange // this.panelPastelOrange.AllowDrop = true; - this.panelPastelOrange.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192))))); + this.panelPastelOrange.BackColor = System.Drawing.Color.PeachPuff; this.panelPastelOrange.Location = new System.Drawing.Point(207, 35); this.panelPastelOrange.Name = "panelPastelOrange"; this.panelPastelOrange.Size = new System.Drawing.Size(40, 40); @@ -259,7 +259,7 @@ // panelPastelPink // this.panelPastelPink.AllowDrop = true; - this.panelPastelPink.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192))))); + this.panelPastelPink.BackColor = System.Drawing.Color.LightSalmon; this.panelPastelPink.Location = new System.Drawing.Point(132, 35); this.panelPastelPink.Name = "panelPastelPink"; this.panelPastelPink.Size = new System.Drawing.Size(40, 40); @@ -268,7 +268,7 @@ // panelPastelRed // this.panelPastelRed.AllowDrop = true; - this.panelPastelRed.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128))))); + this.panelPastelRed.BackColor = System.Drawing.Color.Red; this.panelPastelRed.Location = new System.Drawing.Point(55, 35); this.panelPastelRed.Name = "panelPastelRed"; this.panelPastelRed.Size = new System.Drawing.Size(40, 40); @@ -330,7 +330,7 @@ this.ButtonOk.TabIndex = 2; this.ButtonOk.Text = "Добавить"; this.ButtonOk.UseVisualStyleBackColor = true; - // this.ButtonOk.Click += new System.EventHandler(this.ButtonOk_Click); + this.ButtonOk.Click += new System.EventHandler(this.ButtonOk_Click); // // buttonCancel // diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs index ac78182..d1bd132 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveConfig.cs @@ -170,8 +170,6 @@ namespace ProjectElectricLocomotive } } - // TODO Реализовать логику смены цветов: основного и дополнительного (для продвинутого объекта) - /// /// Добавление loco /// diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs index 8c87e67..b0bf07c 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs @@ -118,10 +118,12 @@ namespace ProjectElectricLocomotive.Generics { return false; } - using FileStream fs = new(filename, FileMode.Create); - byte[] info = new - UTF8Encoding(true).GetBytes($"LocomotiveStorage{Environment.NewLine}{data}"); - fs.Write(info, 0, info.Length); + using StreamWriter fs = new StreamWriter(filename); + + foreach(var str in _locomotivesStorage) + { + fs.WriteLine($"LocomotiveStorage{Environment.NewLine}{data}"); + } return true; } /// @@ -135,19 +137,73 @@ namespace ProjectElectricLocomotive.Generics { return false; } - string bufferTextFromFile = ""; - using (FileStream fs = new(filename, FileMode.Open)) + //string bufferTextFromFile = ""; + //using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader fs = File.OpenText(filename)) + { - byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true); - while (fs.Read(b, 0, b.Length) > 0) + /* byte[] b = new byte[fs.Length]; + UTF8Encoding temp = new(true);*/ + /*while (fs.Read(b, 0, b.Length) > 0) { bufferTextFromFile += temp.GetString(b); + }*//* + + List strsh = new List(); + + string str; + while ((str = fs.ReadLine()) != null) + { + Console.WriteLine(str); + strsh.Add(str); + }*/ + string? str = fs.ReadLine(); + + if (str == null || str.Length == 0) + { + return false; } + + if (!str.Contains("LocomotiveStorage")) + { + //если нет такой записи, то это не те данные + return false; + } + + _locomotivesStorage.Clear(); + + foreach(string data in str) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + LocomotiveGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingLocomotive? loco = + elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); + if (loco != null) + { + if ((collection + loco) != -1) // or vice versa + { + return false; + } + } + } + _locomotivesStorage.Add(record[0], collection); + } + return true; + } - var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, + /*var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); - if (strs == null || strs.Length == 0) + if (str == null || str.Length == 0) { return false; } @@ -157,7 +213,7 @@ namespace ProjectElectricLocomotive.Generics return false; } _locomotivesStorage.Clear(); - foreach (string data in strs) + foreach (string data in str) { string[] record = data.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); @@ -183,7 +239,7 @@ namespace ProjectElectricLocomotive.Generics } _locomotivesStorage.Add(record[0], collection); } - return true; + return true;*/ } } } -- 2.25.1 From a2cc6dcd8db38088a93d044497436e1c0156b6eb Mon Sep 17 00:00:00 2001 From: ekallin Date: Fri, 17 Nov 2023 00:11:00 +0400 Subject: [PATCH 6/8] Almost ready lab 6, but weight of Electro Loco always 354... why? --- .../FormLocomotiveCollections.Designer.cs | 333 +++++++++--------- .../FormLocomotiveCollections.resx | 64 +++- .../LocomotiveGenericStorage.cs | 86 +++-- 3 files changed, 280 insertions(+), 203 deletions(-) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs index 29e2812..ae11658 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs @@ -28,229 +28,224 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); - this.ButtonRefreshCollection = new System.Windows.Forms.Button(); - this.ButtonRemoveLocomotive = new System.Windows.Forms.Button(); - this.ButtonAddLocomotive = new System.Windows.Forms.Button(); - this.pictureBoxCollections = new System.Windows.Forms.PictureBox(); - this.textBoxStorageName = new System.Windows.Forms.TextBox(); - this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components); - this.bindingSource2 = new System.Windows.Forms.BindingSource(this.components); - this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.listBoxStorage = new System.Windows.Forms.ListBox(); - this.ButtonAddObject = new System.Windows.Forms.Button(); - this.ButtonRemoveObject = new System.Windows.Forms.Button(); - this.Instruments = new System.Windows.Forms.GroupBox(); - this.menuStrip1 = new System.Windows.Forms.MenuStrip(); - this.файлToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.SaveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.LoadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); - this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollections)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.bindingSource2)).BeginInit(); - this.groupBox1.SuspendLayout(); - this.Instruments.SuspendLayout(); - this.menuStrip1.SuspendLayout(); - this.SuspendLayout(); + components = new System.ComponentModel.Container(); + maskedTextBoxNumber = new MaskedTextBox(); + ButtonRefreshCollection = new Button(); + ButtonRemoveLocomotive = new Button(); + ButtonAddLocomotive = new Button(); + pictureBoxCollections = new PictureBox(); + textBoxStorageName = new TextBox(); + bindingSource1 = new BindingSource(components); + bindingSource2 = new BindingSource(components); + groupBox1 = new GroupBox(); + listBoxStorage = new ListBox(); + ButtonAddObject = new Button(); + ButtonRemoveObject = new Button(); + Instruments = new GroupBox(); + menuStrip1 = new MenuStrip(); + файлToolStripMenuItem = new ToolStripMenuItem(); + SaveToolStripMenuItem = new ToolStripMenuItem(); + LoadToolStripMenuItem = new ToolStripMenuItem(); + saveFileDialog = new SaveFileDialog(); + openFileDialog = new OpenFileDialog(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollections).BeginInit(); + ((System.ComponentModel.ISupportInitialize)bindingSource1).BeginInit(); + ((System.ComponentModel.ISupportInitialize)bindingSource2).BeginInit(); + groupBox1.SuspendLayout(); + Instruments.SuspendLayout(); + menuStrip1.SuspendLayout(); + SuspendLayout(); // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.maskedTextBoxNumber.Location = new System.Drawing.Point(38, 532); - this.maskedTextBoxNumber.Mask = "0"; - this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(156, 27); - this.maskedTextBoxNumber.TabIndex = 4; + maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + maskedTextBoxNumber.Location = new Point(38, 532); + maskedTextBoxNumber.Mask = "0"; + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(156, 27); + maskedTextBoxNumber.TabIndex = 4; // // ButtonRefreshCollection // - this.ButtonRefreshCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonRefreshCollection.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonRefreshCollection.Location = new System.Drawing.Point(45, 663); - this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - this.ButtonRefreshCollection.Size = new System.Drawing.Size(150, 41); - this.ButtonRefreshCollection.TabIndex = 2; - this.ButtonRefreshCollection.Text = "Обновить все"; - this.ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + ButtonRefreshCollection.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); + ButtonRefreshCollection.Location = new Point(45, 663); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new Size(150, 41); + ButtonRefreshCollection.TabIndex = 2; + ButtonRefreshCollection.Text = "Обновить все"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; // // ButtonRemoveLocomotive // - this.ButtonRemoveLocomotive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonRemoveLocomotive.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonRemoveLocomotive.Location = new System.Drawing.Point(45, 590); - this.ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive"; - this.ButtonRemoveLocomotive.Size = new System.Drawing.Size(150, 44); - this.ButtonRemoveLocomotive.TabIndex = 1; - this.ButtonRemoveLocomotive.Text = "Удалить локо"; - this.ButtonRemoveLocomotive.UseVisualStyleBackColor = true; - this.ButtonRemoveLocomotive.Click += new System.EventHandler(this.ButtonRemoveLocomotive_Click); + ButtonRemoveLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + ButtonRemoveLocomotive.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); + ButtonRemoveLocomotive.Location = new Point(45, 590); + ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive"; + ButtonRemoveLocomotive.Size = new Size(150, 44); + ButtonRemoveLocomotive.TabIndex = 1; + ButtonRemoveLocomotive.Text = "Удалить локо"; + ButtonRemoveLocomotive.UseVisualStyleBackColor = true; + ButtonRemoveLocomotive.Click += ButtonRemoveLocomotive_Click; // // ButtonAddLocomotive // - this.ButtonAddLocomotive.Anchor = System.Windows.Forms.AnchorStyles.Top; - this.ButtonAddLocomotive.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonAddLocomotive.Location = new System.Drawing.Point(42, 461); - this.ButtonAddLocomotive.Name = "ButtonAddLocomotive"; - this.ButtonAddLocomotive.Size = new System.Drawing.Size(150, 39); - this.ButtonAddLocomotive.TabIndex = 0; - this.ButtonAddLocomotive.Text = "Добавить локо"; - this.ButtonAddLocomotive.UseVisualStyleBackColor = true; - this.ButtonAddLocomotive.Click += new System.EventHandler(this.ButtonAddLocomotive_Click); + ButtonAddLocomotive.Anchor = AnchorStyles.Top; + ButtonAddLocomotive.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); + ButtonAddLocomotive.Location = new Point(42, 461); + ButtonAddLocomotive.Name = "ButtonAddLocomotive"; + ButtonAddLocomotive.Size = new Size(150, 39); + ButtonAddLocomotive.TabIndex = 0; + ButtonAddLocomotive.Text = "Добавить локо"; + ButtonAddLocomotive.UseVisualStyleBackColor = true; + ButtonAddLocomotive.Click += ButtonAddLocomotive_Click; // // pictureBoxCollections // - this.pictureBoxCollections.Anchor = System.Windows.Forms.AnchorStyles.Left; - this.pictureBoxCollections.Location = new System.Drawing.Point(-1, 27); - this.pictureBoxCollections.Name = "pictureBoxCollections"; - this.pictureBoxCollections.Size = new System.Drawing.Size(1104, 692); - this.pictureBoxCollections.TabIndex = 1; - this.pictureBoxCollections.TabStop = false; + pictureBoxCollections.Anchor = AnchorStyles.Left; + pictureBoxCollections.Location = new Point(-1, 27); + pictureBoxCollections.Name = "pictureBoxCollections"; + pictureBoxCollections.Size = new Size(1104, 692); + pictureBoxCollections.TabIndex = 1; + pictureBoxCollections.TabStop = false; // // textBoxStorageName // - this.textBoxStorageName.Location = new System.Drawing.Point(31, 43); - this.textBoxStorageName.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.textBoxStorageName.Name = "textBoxStorageName"; - this.textBoxStorageName.Size = new System.Drawing.Size(149, 27); - this.textBoxStorageName.TabIndex = 5; + textBoxStorageName.Location = new Point(31, 43); + textBoxStorageName.Margin = new Padding(3, 4, 3, 4); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(149, 27); + textBoxStorageName.TabIndex = 5; // // groupBox1 // - this.groupBox1.Controls.Add(this.listBoxStorage); - this.groupBox1.Controls.Add(this.ButtonAddObject); - this.groupBox1.Controls.Add(this.ButtonRemoveObject); - this.groupBox1.Controls.Add(this.textBoxStorageName); - this.groupBox1.Location = new System.Drawing.Point(7, 29); - this.groupBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.groupBox1.Name = "groupBox1"; - this.groupBox1.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.groupBox1.Size = new System.Drawing.Size(216, 395); - this.groupBox1.TabIndex = 5; - this.groupBox1.TabStop = false; - this.groupBox1.Text = "Наборы"; + groupBox1.Controls.Add(listBoxStorage); + groupBox1.Controls.Add(ButtonAddObject); + groupBox1.Controls.Add(ButtonRemoveObject); + groupBox1.Controls.Add(textBoxStorageName); + groupBox1.Location = new Point(7, 29); + groupBox1.Margin = new Padding(3, 4, 3, 4); + groupBox1.Name = "groupBox1"; + groupBox1.Padding = new Padding(3, 4, 3, 4); + groupBox1.Size = new Size(216, 395); + groupBox1.TabIndex = 5; + groupBox1.TabStop = false; + groupBox1.Text = "Наборы"; // // listBoxStorage // - this.listBoxStorage.FormattingEnabled = true; - this.listBoxStorage.ItemHeight = 20; - this.listBoxStorage.Location = new System.Drawing.Point(31, 163); - this.listBoxStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.listBoxStorage.Name = "listBoxStorage"; - this.listBoxStorage.Size = new System.Drawing.Size(149, 124); - this.listBoxStorage.TabIndex = 9; - this.listBoxStorage.SelectedIndexChanged += new System.EventHandler(this.listBoxStorage_SelectedIndexChanged); + listBoxStorage.FormattingEnabled = true; + listBoxStorage.ItemHeight = 20; + listBoxStorage.Location = new Point(31, 163); + listBoxStorage.Margin = new Padding(3, 4, 3, 4); + listBoxStorage.Name = "listBoxStorage"; + listBoxStorage.Size = new Size(149, 124); + listBoxStorage.TabIndex = 9; + listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged; // // ButtonAddObject // - this.ButtonAddObject.Anchor = System.Windows.Forms.AnchorStyles.Top; - this.ButtonAddObject.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonAddObject.Location = new System.Drawing.Point(31, 96); - this.ButtonAddObject.Name = "ButtonAddObject"; - this.ButtonAddObject.Size = new System.Drawing.Size(150, 39); - this.ButtonAddObject.TabIndex = 7; - this.ButtonAddObject.Text = "Добавить набор"; - this.ButtonAddObject.UseVisualStyleBackColor = true; - this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + ButtonAddObject.Anchor = AnchorStyles.Top; + ButtonAddObject.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); + ButtonAddObject.Location = new Point(31, 96); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(150, 39); + ButtonAddObject.TabIndex = 7; + ButtonAddObject.Text = "Добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += ButtonAddObject_Click; // // ButtonRemoveObject // - this.ButtonRemoveObject.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.ButtonRemoveObject.Font = new System.Drawing.Font("Candara Light", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.ButtonRemoveObject.Location = new System.Drawing.Point(31, 325); - this.ButtonRemoveObject.Name = "ButtonRemoveObject"; - this.ButtonRemoveObject.Size = new System.Drawing.Size(150, 44); - this.ButtonRemoveObject.TabIndex = 8; - this.ButtonRemoveObject.Text = "Удалить набор"; - this.ButtonRemoveObject.UseVisualStyleBackColor = true; - this.ButtonRemoveObject.Click += new System.EventHandler(this.ButtonRemoveObject_Click); + ButtonRemoveObject.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + ButtonRemoveObject.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); + ButtonRemoveObject.Location = new Point(31, 325); + ButtonRemoveObject.Name = "ButtonRemoveObject"; + ButtonRemoveObject.Size = new Size(150, 44); + ButtonRemoveObject.TabIndex = 8; + ButtonRemoveObject.Text = "Удалить набор"; + ButtonRemoveObject.UseVisualStyleBackColor = true; + ButtonRemoveObject.Click += ButtonRemoveObject_Click; // // Instruments // - this.Instruments.Anchor = System.Windows.Forms.AnchorStyles.Right; - this.Instruments.Controls.Add(this.ButtonRefreshCollection); - this.Instruments.Controls.Add(this.groupBox1); - this.Instruments.Controls.Add(this.maskedTextBoxNumber); - this.Instruments.Controls.Add(this.ButtonAddLocomotive); - this.Instruments.Controls.Add(this.ButtonRemoveLocomotive); - this.Instruments.Location = new System.Drawing.Point(1102, 0); - this.Instruments.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.Instruments.Name = "Instruments"; - this.Instruments.Padding = new System.Windows.Forms.Padding(3, 4, 3, 4); - this.Instruments.Size = new System.Drawing.Size(236, 719); - this.Instruments.TabIndex = 6; - this.Instruments.TabStop = false; - this.Instruments.Text = "Инструменты"; + Instruments.Anchor = AnchorStyles.Right; + Instruments.Controls.Add(ButtonRefreshCollection); + Instruments.Controls.Add(groupBox1); + Instruments.Controls.Add(maskedTextBoxNumber); + Instruments.Controls.Add(ButtonAddLocomotive); + Instruments.Controls.Add(ButtonRemoveLocomotive); + Instruments.Location = new Point(1102, 0); + Instruments.Margin = new Padding(3, 4, 3, 4); + Instruments.Name = "Instruments"; + Instruments.Padding = new Padding(3, 4, 3, 4); + Instruments.Size = new Size(236, 719); + Instruments.TabIndex = 6; + Instruments.TabStop = false; + Instruments.Text = "Инструменты"; // // menuStrip1 // - this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); - this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.файлToolStripMenuItem}); - this.menuStrip1.Location = new System.Drawing.Point(0, 0); - this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Size = new System.Drawing.Size(1338, 28); - this.menuStrip1.TabIndex = 7; - this.menuStrip1.Text = "menuStrip1"; + menuStrip1.ImageScalingSize = new Size(20, 20); + menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(1338, 28); + menuStrip1.TabIndex = 7; + menuStrip1.Text = "menuStrip1"; // // файлToolStripMenuItem // - this.файлToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.SaveToolStripMenuItem, - this.LoadToolStripMenuItem}); - this.файлToolStripMenuItem.Name = "файлToolStripMenuItem"; - this.файлToolStripMenuItem.Size = new System.Drawing.Size(59, 24); - this.файлToolStripMenuItem.Text = "Файл"; + файлToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SaveToolStripMenuItem, LoadToolStripMenuItem }); + файлToolStripMenuItem.Name = "файлToolStripMenuItem"; + файлToolStripMenuItem.Size = new Size(59, 24); + файлToolStripMenuItem.Text = "Файл"; // // SaveToolStripMenuItem // - this.SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; - this.SaveToolStripMenuItem.Size = new System.Drawing.Size(224, 26); - this.SaveToolStripMenuItem.Text = "Сохранить"; - this.SaveToolStripMenuItem.Click += new System.EventHandler(this.SaveToolStripMenuItem_Click); + SaveToolStripMenuItem.Name = "SaveToolStripMenuItem"; + SaveToolStripMenuItem.Size = new Size(166, 26); + SaveToolStripMenuItem.Text = "Сохранить"; + SaveToolStripMenuItem.Click += SaveToolStripMenuItem_Click; // // LoadToolStripMenuItem // - this.LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; - this.LoadToolStripMenuItem.Size = new System.Drawing.Size(224, 26); - this.LoadToolStripMenuItem.Text = "Загрузить"; - this.LoadToolStripMenuItem.Click += new System.EventHandler(this.LoadToolStripMenuItem_Click); + LoadToolStripMenuItem.Name = "LoadToolStripMenuItem"; + LoadToolStripMenuItem.Size = new Size(166, 26); + LoadToolStripMenuItem.Text = "Загрузить"; + LoadToolStripMenuItem.Click += LoadToolStripMenuItem_Click; // // saveFileDialog // - this.saveFileDialog.Filter = "txt file | *.txt"; + saveFileDialog.Filter = "txt file | *.txt"; // // openFileDialog // - this.openFileDialog.FileName = "openFileDialog1"; - this.openFileDialog.Filter = "txt file | *.txt"; + openFileDialog.FileName = "openFileDialog1"; + openFileDialog.Filter = "txt file | *.txt"; // // FormLocomotiveCollections // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1338, 724); - this.Controls.Add(this.Instruments); - this.Controls.Add(this.pictureBoxCollections); - this.Controls.Add(this.menuStrip1); - this.MainMenuStrip = this.menuStrip1; - this.Name = "FormLocomotiveCollections"; - this.Text = "Набор локомотивов"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollections)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.bindingSource2)).EndInit(); - this.groupBox1.ResumeLayout(false); - this.groupBox1.PerformLayout(); - this.Instruments.ResumeLayout(false); - this.Instruments.PerformLayout(); - this.menuStrip1.ResumeLayout(false); - this.menuStrip1.PerformLayout(); - this.ResumeLayout(false); - this.PerformLayout(); - + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1338, 724); + Controls.Add(Instruments); + Controls.Add(pictureBoxCollections); + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; + Name = "FormLocomotiveCollections"; + Text = "Набор локомотивов"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollections).EndInit(); + ((System.ComponentModel.ISupportInitialize)bindingSource1).EndInit(); + ((System.ComponentModel.ISupportInitialize)bindingSource2).EndInit(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + Instruments.ResumeLayout(false); + Instruments.PerformLayout(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); + ResumeLayout(false); + PerformLayout(); } #endregion diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx index 170c13e..f2c8442 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx @@ -1,4 +1,64 @@ - + + + @@ -73,6 +133,6 @@ 615, 17 - 144 + 84 \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs index b0bf07c..70be0c9 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs @@ -119,10 +119,9 @@ namespace ProjectElectricLocomotive.Generics return false; } using StreamWriter fs = new StreamWriter(filename); - - foreach(var str in _locomotivesStorage) { - fs.WriteLine($"LocomotiveStorage{Environment.NewLine}{data}"); + fs.WriteLine($"LocomotiveStorage{Environment.NewLine}"); + fs.WriteLine(data); } return true; } @@ -137,60 +136,53 @@ namespace ProjectElectricLocomotive.Generics { return false; } - //string bufferTextFromFile = ""; - //using (FileStream fs = new(filename, FileMode.Open)) + using (StreamReader fs = File.OpenText(filename)) - { - /* byte[] b = new byte[fs.Length]; - UTF8Encoding temp = new(true);*/ - /*while (fs.Read(b, 0, b.Length) > 0) - { - bufferTextFromFile += temp.GetString(b); - }*//* - List strsh = new List(); - - string str; - while ((str = fs.ReadLine()) != null) - { - Console.WriteLine(str); - strsh.Add(str); - }*/ - string? str = fs.ReadLine(); + string str = fs.ReadLine(); if (str == null || str.Length == 0) { return false; } - if (!str.Contains("LocomotiveStorage")) + if (!str.StartsWith("LocomotiveStorage")) { //если нет такой записи, то это не те данные return false; } _locomotivesStorage.Clear(); + string strs = ""; - foreach(string data in str) + bool firstinit = true; + + while ((strs = fs.ReadLine()) != null) { - string[] record = data.Split(_separatorForKeyValue, - StringSplitOptions.RemoveEmptyEntries); + if (strs == null && firstinit) + { + return false; + } + else if (strs == null) + { + break; + } + firstinit = false; + + string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); if (record.Length != 2) { continue; } - LocomotiveGenericCollection - collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, - StringSplitOptions.RemoveEmptyEntries); + LocomotiveGenericCollection collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, StringSplitOptions.RemoveEmptyEntries); foreach (string elem in set) { - DrawingLocomotive? loco = - elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); + DrawingLocomotive? loco = elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); if (loco != null) { - if ((collection + loco) != -1) // or vice versa + if ((collection + loco) == -1) // or vice versa { return false; } @@ -201,6 +193,36 @@ namespace ProjectElectricLocomotive.Generics return true; } + + /*foreach(string data in str) + { + string[] record = data.Split(_separatorForKeyValue, + StringSplitOptions.RemoveEmptyEntries); + if (record.Length != 2) + { + continue; + } + LocomotiveGenericCollection + collection = new(_pictureWidth, _pictureHeight); + string[] set = record[1].Split(_separatorRecords, + StringSplitOptions.RemoveEmptyEntries); + foreach (string elem in set) + { + DrawingLocomotive? loco = + elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); + if (loco != null) + { + if ((collection + loco) != -1) // or vice versa + { + return false; + } + } + } + _locomotivesStorage.Add(record[0], collection); + } + return true; + + }*/ /*var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); if (str == null || str.Length == 0) -- 2.25.1 From 17a21a8284a5fceceb26e0ab429a5f193044ba8a Mon Sep 17 00:00:00 2001 From: ekallin Date: Fri, 17 Nov 2023 00:47:35 +0400 Subject: [PATCH 7/8] uraaaaa mistake found --- .../DrawingElectricLocomotive.cs | 2 +- .../ProjectElectricLocomotive/ExtentionDrawningCar.cs | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs index f944efc..f882f92 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/DrawingElectricLocomotive.cs @@ -14,7 +14,7 @@ namespace ProjectElectricLocomotive.DrawingObjects { if (EntityLocomotive != null) { - EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); + EntityLocomotive = new EntityElectricLocomotive(speed, weight, bodyColor, additionalColor, horns, seifBatteries); } } public override void DrawTransport(Graphics g) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs index 788893c..f577133 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs @@ -69,10 +69,12 @@ namespace ProjectElectricLocomotive { return str; } - return - $"{str}{separatorForObject}{electroLoco.AdditionalColor.Name}{separatorForObject}" + - $"{electroLoco.Horns}{separatorForObject}{electroLoco.SeifBatteries}" /*+ - $"{separatorForObject}{electroLoco.SportLine}"*/; + else + { + return + $"{str}{separatorForObject}{electroLoco.AdditionalColor.Name}{separatorForObject}" + + $"{electroLoco.Horns}{separatorForObject}{electroLoco.SeifBatteries}"; + } } } -- 2.25.1 From 6ec573e2c20ab975cb0a088d28e72597ae12257c Mon Sep 17 00:00:00 2001 From: ekallin Date: Sun, 19 Nov 2023 12:11:24 +0400 Subject: [PATCH 8/8] correcting some mistakes in lab 6 --- ...ngCar.cs => ExtentionDrawingLocomotive.cs} | 2 +- .../LocomotiveGenericStorage.cs | 84 +------------------ 2 files changed, 5 insertions(+), 81 deletions(-) rename ProjectElectricLocomotive/ProjectElectricLocomotive/{ExtentionDrawningCar.cs => ExtentionDrawingLocomotive.cs} (97%) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawingLocomotive.cs similarity index 97% rename from ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs rename to ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawingLocomotive.cs index f577133..a2f4878 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawningCar.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/ExtentionDrawingLocomotive.cs @@ -53,7 +53,7 @@ namespace ProjectElectricLocomotive /// /// Получение данных для сохранения в файл /// - /// Сохраняемый объект + /// Сохраняемый объект /// Разделитель даннных /// Строка с данными по объекту public static string GetDataForSave(this DrawingLocomotive drawningLoco, char separatorForObject) diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs index 70be0c9..100df98 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs @@ -104,8 +104,7 @@ namespace ProjectElectricLocomotive.Generics File.Delete(filename); } StringBuilder data = new(); - foreach (KeyValuePair> record in _locomotivesStorage) + foreach (KeyValuePair> record in _locomotivesStorage) { StringBuilder records = new(); foreach (DrawingLocomotive? elem in record.Value.GetLocomotives) @@ -156,19 +155,14 @@ namespace ProjectElectricLocomotive.Generics _locomotivesStorage.Clear(); string strs = ""; - bool firstinit = true; while ((strs = fs.ReadLine()) != null) { - if (strs == null && firstinit) + + if (strs == null) { return false; } - else if (strs == null) - { - break; - } - firstinit = false; string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); if (record.Length != 2) @@ -182,7 +176,7 @@ namespace ProjectElectricLocomotive.Generics DrawingLocomotive? loco = elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); if (loco != null) { - if ((collection + loco) == -1) // or vice versa + if ((collection + loco) == -1) // for my realization it's -1, for eegov's realization it's boolean { return false; } @@ -191,77 +185,7 @@ namespace ProjectElectricLocomotive.Generics _locomotivesStorage.Add(record[0], collection); } return true; - } - - /*foreach(string data in str) - { - string[] record = data.Split(_separatorForKeyValue, - StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) - { - continue; - } - LocomotiveGenericCollection - collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, - StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) - { - DrawingLocomotive? loco = - elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); - if (loco != null) - { - if ((collection + loco) != -1) // or vice versa - { - return false; - } - } - } - _locomotivesStorage.Add(record[0], collection); - } - return true; - - }*/ - /*var strs = bufferTextFromFile.Split(new char[] { '\n', '\r' }, - StringSplitOptions.RemoveEmptyEntries); - if (str == null || str.Length == 0) - { - return false; - } - if (!strs[0].StartsWith("LocomotiveStorage")) - { - //если нет такой записи, то это не те данные - return false; - } - _locomotivesStorage.Clear(); - foreach (string data in str) - { - string[] record = data.Split(_separatorForKeyValue, - StringSplitOptions.RemoveEmptyEntries); - if (record.Length != 2) - { - continue; - } - LocomotiveGenericCollection - collection = new(_pictureWidth, _pictureHeight); - string[] set = record[1].Split(_separatorRecords, - StringSplitOptions.RemoveEmptyEntries); - foreach (string elem in set) - { - DrawingLocomotive? loco = - elem?.CreateDrawingLocomotive(_separatorForObject, _pictureWidth, _pictureHeight); - if (loco != null) - { - if ((collection + loco) != -1) // or vice versa - { - return false; - } - } - } - _locomotivesStorage.Add(record[0], collection); - } - return true;*/ } } } -- 2.25.1