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