|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Data; |
| 5 | +using System.Drawing; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Windows.Forms; |
| 11 | + |
| 12 | +namespace Hometask_3.Forms |
| 13 | +{ |
| 14 | + public partial class FormMain : Form |
| 15 | + { |
| 16 | + //ПОЛЯ КЛАССА: |
| 17 | + //+-------------------------------------------------------------------+ |
| 18 | + |
| 19 | + private string path = null; //путь к файлу |
| 20 | + private FileStream file = null; //байтовый поток |
| 21 | + private StreamReader streamTxtRdr = null; //чтение текста из файла |
| 22 | + private FormProgressBar formProgressBar = null; //окно с прогресс баром |
| 23 | + private OpenFileDialog openFileDialog = null; //окно для откр. файла |
| 24 | + private FormTextEdit formTextEdit = null; //форма для ред. текста |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + //КОНСТРУКТОРЫ КЛАССА: |
| 29 | + //+-------------------------------------------------------------------+ |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Конструктор без параметров |
| 33 | + /// </summary> |
| 34 | + public FormMain() |
| 35 | + { |
| 36 | + this.InitializeComponent(); |
| 37 | + this.EventSubscription(); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + //МЕТОДЫ КЛАССА: |
| 43 | + //+-------------------------------------------------------------------+ |
| 44 | + |
| 45 | + //Создание дочерних форм// |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Создание формы содержащей в себе ProgressBar. |
| 49 | + /// Данная форма отслеживает загрузку данных в приложение. |
| 50 | + /// </summary> |
| 51 | + private void FormProgressBarInitialization() |
| 52 | + { |
| 53 | + this.formProgressBar = new FormProgressBar(this.file, |
| 54 | + this.richTextBoxDocument); |
| 55 | + this.formProgressBar.StartPosition = FormStartPosition.CenterParent; |
| 56 | + this.formProgressBar.Owner = this; |
| 57 | + this.formProgressBar.ShowDialog(this); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Создание диалогового окна для открытия пользователем, произвольного |
| 62 | + /// текстового файла |
| 63 | + /// </summary> |
| 64 | + private void OpenFileDialogInitialization() |
| 65 | + { |
| 66 | + this.openFileDialog = new OpenFileDialog(); |
| 67 | + this.openFileDialog.Multiselect = false; |
| 68 | + this.openFileDialog.Title = "Открыть текстовый файл"; |
| 69 | + this.openFileDialog.AddExtension = true; |
| 70 | + this.openFileDialog.CheckFileExists = true; |
| 71 | + this.openFileDialog.CheckPathExists = true; |
| 72 | + this.openFileDialog.DefaultExt = "txt"; |
| 73 | + this.openFileDialog.DereferenceLinks = true; |
| 74 | + this.openFileDialog.Filter = "txt files (*.txt)|*.txt"; |
| 75 | + this.openFileDialog.ShowDialog(this); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Создание формы для редактирования заданного пользователем текста |
| 80 | + /// </summary> |
| 81 | + private void FormTextEditInitialization() |
| 82 | + { |
| 83 | + this.formTextEdit = new FormTextEdit(this.richTextBoxDocument); |
| 84 | + this.formTextEdit.Owner = this; |
| 85 | + this.formTextEdit.Show(this); |
| 86 | + } |
| 87 | + |
| 88 | + //Подписка на события// |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Подписка контроллеров формы на события |
| 92 | + /// </summary> |
| 93 | + private void EventSubscription() |
| 94 | + { |
| 95 | + this.buttonDownload.Click += new EventHandler(this.ButtonDownloadClick); |
| 96 | + this.buttonEdite.Click += new EventHandler(this.ButtonEditClick); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + //ОБАБОТЧИКИ СОБЫТИЙ: |
| 102 | + //+-------------------------------------------------------------------+ |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Открывает диалоговое окно "открытия файла", после чего, загружет |
| 106 | + /// выбранные файл в текстовый контроллер "RichTextBox" расположенный |
| 107 | + /// в главном окне приложения. |
| 108 | + /// </summary> |
| 109 | + private void ButtonDownloadClick(Object sender, EventArgs e) |
| 110 | + { |
| 111 | + this.OpenFileDialogInitialization(); |
| 112 | + this.path = this.openFileDialog.FileName; |
| 113 | + if (!String.IsNullOrEmpty(this.path) || |
| 114 | + !String.IsNullOrWhiteSpace(this.path)) |
| 115 | + { |
| 116 | + this.file = new FileStream(this.path, FileMode.Open, |
| 117 | + FileAccess.ReadWrite); |
| 118 | + this.streamTxtRdr = new StreamReader(this.file); |
| 119 | + this.FormProgressBarInitialization(); |
| 120 | + if (this.formProgressBar != null) this.formProgressBar.Dispose(); |
| 121 | + this.buttonEdite.Enabled = true; |
| 122 | + if (this.streamTxtRdr != null) |
| 123 | + { |
| 124 | + this.streamTxtRdr.Close(); |
| 125 | + this.streamTxtRdr = null; |
| 126 | + } |
| 127 | + if (this.file != null) |
| 128 | + { |
| 129 | + this.file.Close(); |
| 130 | + this.file = null; |
| 131 | + } |
| 132 | + } |
| 133 | + if (this.openFileDialog != null) |
| 134 | + { |
| 135 | + this.openFileDialog.Dispose(); |
| 136 | + this.openFileDialog = null; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Открывает окно для редактирования загруженного пользователем |
| 142 | + /// из файла текста. |
| 143 | + /// </summary> |
| 144 | + private void ButtonEditClick(Object sender, EventArgs e) |
| 145 | + { |
| 146 | + this.FormTextEditInitialization(); |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + //ДЕСТРУКТОР КЛАССА: |
| 152 | + //+-------------------------------------------------------------------+ |
| 153 | + ~FormMain() |
| 154 | + { |
| 155 | + if (this.formProgressBar != null) this.formProgressBar.Dispose(); |
| 156 | + if (this.streamTxtRdr != null) this.streamTxtRdr.Close(); |
| 157 | + if (this.file != null) this.file.Close(); |
| 158 | + if (this.formTextEdit != null) this.formTextEdit.Dispose(); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments