From a94ad17abcfc7fea2de670214faf3c5e2fa5e00d Mon Sep 17 00:00:00 2001 From: Kirill <117719052+KirillFirsof@users.noreply.github.com> Date: Sun, 17 Nov 2024 14:18:00 +0400 Subject: [PATCH] =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D0=B9=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/testComponent.cs | 42 +++++- .../ValueOutOfRangeException.cs | 2 +- WinFormsApp1/Form1.Designer.cs | 27 ++++ WinFormsApp1/Form1.cs | 19 +++ WinFormsApp1/Form1.resx | 7 +- WinFormsApp1/Form2.Designer.cs | 39 ------ WinFormsApp1/Form2.cs | 20 --- WinFormsApp1/Form2.resx | 120 ------------------ 8 files changed, 92 insertions(+), 184 deletions(-) delete mode 100644 WinFormsApp1/Form2.Designer.cs delete mode 100644 WinFormsApp1/Form2.cs delete mode 100644 WinFormsApp1/Form2.resx diff --git a/CheckedListBoxLibrary/Components/testComponent.cs b/CheckedListBoxLibrary/Components/testComponent.cs index caa8b4e..0905f51 100644 --- a/CheckedListBoxLibrary/Components/testComponent.cs +++ b/CheckedListBoxLibrary/Components/testComponent.cs @@ -10,16 +10,54 @@ namespace MyUserControls.Components { public partial class testComponent : Component { + private string _fileName; + public string FileName + { + set + { + if (string.IsNullOrEmpty(value)){ + return; + } + if (!value.EndsWith(".txt")) + { + throw new ArgumentException("No txt file"); + } + _fileName = value; + } + } public testComponent() { InitializeComponent(); + _fileName = string.Empty; } - public testComponent(IContainer container) { container.Add(this); - InitializeComponent(); + _fileName = string.Empty; } + public bool SaveToFile(string[] texts) + { + CheckFileExsists(); + using var writer = new StreamWriter(_fileName, true); + foreach (var text in texts) + { + writer.WriteLine(text); + } + writer.Flush(); + return true; + } + private void CheckFileExsists() + { + if (string.IsNullOrEmpty(_fileName)) + { + throw new ArgumentNullException(_fileName); + } + if (!File.Exists(_fileName)) + { + throw new FileNotFoundException(_fileName); + } + } + } } diff --git a/CheckedListBoxLibrary/VisualComponents/ValueOutOfRangeException.cs b/CheckedListBoxLibrary/VisualComponents/ValueOutOfRangeException.cs index ad41c4e..c40b9ab 100644 --- a/CheckedListBoxLibrary/VisualComponents/ValueOutOfRangeException.cs +++ b/CheckedListBoxLibrary/VisualComponents/ValueOutOfRangeException.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace MyUserControls.VisualComponents +namespace MyUserControls { public class ValueOutOfRangeException : Exception { diff --git a/WinFormsApp1/Form1.Designer.cs b/WinFormsApp1/Form1.Designer.cs index cb546ba..db87abe 100644 --- a/WinFormsApp1/Form1.Designer.cs +++ b/WinFormsApp1/Form1.Designer.cs @@ -28,9 +28,13 @@ /// private void InitializeComponent() { + components = new System.ComponentModel.Container(); smartCheckedListBox1 = new MyUserControls.SmartCheckedListBox(); smartTextBox1 = new MyUserControls.SmartTextBox(); hierarchicalTreeView = new MyUserControls.HierarchicalTreeView(); + testComponent1 = new MyUserControls.Components.testComponent(components); + buttonSave = new Button(); + richTextBoxText = new RichTextBox(); SuspendLayout(); // // smartCheckedListBox1 @@ -57,11 +61,31 @@ hierarchicalTreeView.Size = new Size(266, 174); hierarchicalTreeView.TabIndex = 3; // + // buttonSave + // + buttonSave.Location = new Point(337, 162); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(168, 32); + buttonSave.TabIndex = 4; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // richTextBoxText + // + richTextBoxText.Location = new Point(337, 26); + richTextBoxText.Name = "richTextBoxText"; + richTextBoxText.Size = new Size(168, 130); + richTextBoxText.TabIndex = 5; + richTextBoxText.Text = ""; + // // Form1 // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1120, 450); + Controls.Add(richTextBoxText); + Controls.Add(buttonSave); Controls.Add(hierarchicalTreeView); Controls.Add(smartTextBox1); Controls.Add(smartCheckedListBox1); @@ -75,5 +99,8 @@ private MyUserControls.SmartCheckedListBox smartCheckedListBox1; private MyUserControls.SmartTextBox smartTextBox1; private MyUserControls.HierarchicalTreeView hierarchicalTreeView; + private MyUserControls.Components.testComponent testComponent1; + private Button buttonSave; + private RichTextBox richTextBoxText; } } diff --git a/WinFormsApp1/Form1.cs b/WinFormsApp1/Form1.cs index d17c169..dcb2610 100644 --- a/WinFormsApp1/Form1.cs +++ b/WinFormsApp1/Form1.cs @@ -1,4 +1,5 @@ using MyUserControls; +using System.ComponentModel; namespace WinFormsApp1 { @@ -10,6 +11,7 @@ namespace WinFormsApp1 InitialChekedListBox(); InitialSmartTextBox(); InitialTree(); + testComponent1.FileName = "1.txt"; } private void InitialChekedListBox() @@ -51,5 +53,22 @@ namespace WinFormsApp1 smartTextBox1.InputText = hierarchicalTreeView.GetSelectedItem().ToString(); } + + + private void buttonSave_Click(object sender, EventArgs e) + { + try + { + testComponent1.SaveToFile(richTextBoxText.Lines); + MessageBox.Show("Ñîõàðíåíî óñïåøíî", "Ðåçóëüòàò", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Îøèáêà", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } diff --git a/WinFormsApp1/Form1.resx b/WinFormsApp1/Form1.resx index af32865..29292b5 100644 --- a/WinFormsApp1/Form1.resx +++ b/WinFormsApp1/Form1.resx @@ -1,7 +1,7 @@  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file