Скачать проект.

{codecitation style="brush: xml;"} using System.IO; using System.Text; namespace TextEditor.BL { public interface IFileManager { bool IsExist(string filePath); int GetSymbolCount(string content); string GetContent(string filePath); string GetContent(string filePath, Encoding encoding); void SaveContent(string content, string filePath); void SaveContent(string content, string filePath, Encoding encoding); } public class FileManager:IFileManager { private readonly Encoding _encoding = Encoding.GetEncoding(1251); public bool IsExist(string filePath) { return File.Exists(filePath); } public int GetSymbolCount(string content) { return content.Length; } public string GetContent(string filePath) { return GetContent(filePath, _encoding); } public string GetContent(string filePath, Encoding encoding) { return File.ReadAllText(filePath, encoding); } public void SaveContent(string content, string filePath) { SaveContent(content, filePath, _encoding); } public void SaveContent(string content, string filePath, Encoding encoding) { File.WriteAllText(filePath, content, encoding); } } } {/codecitation} {codecitation style="brush: xml;"} using System.Windows.Forms; namespace TextEditor { public interface IMessageService { void ShowMessage(string message); void ShowExclamation(string exclamation); void ShowError(string error); } public class MessageService:IMessageService { public void ShowMessage(string message) { MessageBox.Show(message, "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); } public void ShowExclamation(string exclamation) { MessageBox.Show(exclamation, "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } public void ShowError(string error) { MessageBox.Show(error, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } {/codecitation} {codecitation style="brush: xml;"} using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace TextEditor { public interface IMainForm { string FilePath { get; } string Content { get; set; } void SetSymbolCount(int count); event EventHandler FileOpenClick; event EventHandler FileSaveClick; event EventHandler ContentChanged; } public partial class Form1 : Form,IMainForm { public Form1() { InitializeComponent(); butOpenFile.Click += new EventHandler(butOpenFile_Click); butSaveFile.Click += new EventHandler(butSaveFile_Click); fldContent.TextChanged += new EventHandler(fldContent_TextChanged); butSelectFile.Click+=new EventHandler(butSelectFile_Click); numFont.ValueChanged += new EventHandler(numFont_ValueChanged); } #region Проброс событий void fldContent_TextChanged(object sender, EventArgs e) { if (ContentChanged != null) ContentChanged(this, EventArgs.Empty); } void butSaveFile_Click(object sender, EventArgs e) { if (FileSaveClick != null) FileSaveClick(this, EventArgs.Empty); } void butOpenFile_Click(object sender, EventArgs e) { if (FileOpenClick != null) FileOpenClick(this, EventArgs.Empty); } #endregion #region IMainForm public string FilePath { get { return fldFilePath.Text; } } public string Content { get { return fldContent.Text; } set { fldContent.Text=value; } } public void SetSymbolCount(int count) { lblSymbolCount.Text=count.ToString(); } public event EventHandler FileOpenClick; public event EventHandler FileSaveClick; public event EventHandler ContentChanged; #endregion void numFont_ValueChanged(object sender, EventArgs e) { fldContent.Font = new Font("Calibri", (float)numFont.Value); } void butSelectFile_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "Текстовые файлы|*.txt|Все файлы|*.*"; if (dlg.ShowDialog() == DialogResult.OK) { fldFilePath.Text = dlg.FileName; if (FileOpenClick != null) FileOpenClick(this, EventArgs.Empty); } } } } {/codecitation} {codecitation style="brush: xml;"} using System; using TextEditor.BL; namespace TextEditor { public class MainPresenter { private readonly IMainForm _view; private readonly IFileManager _manager; private readonly IMessageService _messageService; private string _currentFilePath; public MainPresenter(IMainForm view, IFileManager manager, IMessageService sevice) { _view = view; _manager = manager; _messageService = sevice; _view.SetSymbolCount(0); _view.ContentChanged += new EventHandler(_view_ContentChanged); _view.FileOpenClick += new EventHandler(_view_FileOpenClick); _view.FileSaveClick += new EventHandler(_view_FileSaveClick); } void _view_FileSaveClick(object sender, EventArgs e) { try { string content = _view.Content; _manager.SaveContent(content, _currentFilePath); _messageService.ShowMessage("Файл успешно сохранен"); } catch (Exception ex) { _messageService.ShowError(ex.Message); } } void _view_FileOpenClick(object sender, EventArgs e) { try { string filePath = _view.FilePath; bool isExist = _manager.IsExist(filePath); if (!isExist) { _messageService.ShowExclamation("Выбранный файл не существует."); return; } _currentFilePath = filePath; string content = _manager.GetContent(filePath); int count = _manager.GetSymbolCount(content); _view.Content = content; _view.SetSymbolCount(count); } catch (Exception ex) { _messageService.ShowError(ex.Message); } } void _view_ContentChanged(object sender, EventArgs e) { string content=_view.Content; int count = _manager.GetSymbolCount(content); _view.SetSymbolCount(count); } } } {/codecitation} {codecitation style="brush: xml;"} using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using TextEditor.BL; namespace TextEditor { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MessageService service = new MessageService(); FileManager manager = new FileManager(); Form1 form = new Form1(); MainPresenter presenter = new MainPresenter(form, manager, service); Application.Run(form); } } } {/codecitation} {codecitation style="brush: xml;"} namespace TextEditor { partial class Form1 { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.butSelectFile = new System.Windows.Forms.Button(); this.butOpenFile = new System.Windows.Forms.Button(); this.fldFilePath = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.numFont = new System.Windows.Forms.NumericUpDown(); this.fldContent = new System.Windows.Forms.TextBox(); this.butSaveFile = new System.Windows.Forms.Button(); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); this.lblSymbolCount = new System.Windows.Forms.ToolStripStatusLabel(); ((System.ComponentModel.ISupportInitialize)(this.numFont)).BeginInit(); this.statusStrip1.SuspendLayout(); this.SuspendLayout(); // // butSelectFile // this.butSelectFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.butSelectFile.Location = new System.Drawing.Point(524, 12); this.butSelectFile.Name = "butSelectFile"; this.butSelectFile.Size = new System.Drawing.Size(75, 23); this.butSelectFile.TabIndex = 0; this.butSelectFile.Text = "Select File"; this.butSelectFile.UseVisualStyleBackColor = true; // // butOpenFile // this.butOpenFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.butOpenFile.Location = new System.Drawing.Point(618, 12); this.butOpenFile.Name = "butOpenFile"; this.butOpenFile.Size = new System.Drawing.Size(75, 23); this.butOpenFile.TabIndex = 1; this.butOpenFile.Text = "Open File"; this.butOpenFile.UseVisualStyleBackColor = true; // // fldFilePath // this.fldFilePath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.fldFilePath.Location = new System.Drawing.Point(111, 15); this.fldFilePath.Name = "fldFilePath"; this.fldFilePath.Size = new System.Drawing.Size(390, 20); this.fldFilePath.TabIndex = 2; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 17); this.label1.Name = "label1"; this.label1.RightToLeft = System.Windows.Forms.RightToLeft.Yes; this.label1.Size = new System.Drawing.Size(74, 13); this.label1.TabIndex = 3; this.label1.Text = "Путь к файлу"; // // label2 // this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 56); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(41, 13); this.label2.TabIndex = 4; this.label2.Text = "Шрифт"; // // numFont // this.numFont.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.numFont.Location = new System.Drawing.Point(111, 49); this.numFont.Maximum = new decimal(new int[] { 72, 0, 0, 0}); this.numFont.Minimum = new decimal(new int[] { 6, 0, 0, 0}); this.numFont.Name = "numFont"; this.numFont.Size = new System.Drawing.Size(120, 20); this.numFont.TabIndex = 5; this.numFont.Value = new decimal(new int[] { 11, 0, 0, 0}); // // fldContent // this.fldContent.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.fldContent.Location = new System.Drawing.Point(12, 87); this.fldContent.Multiline = true; this.fldContent.Name = "fldContent"; this.fldContent.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.fldContent.Size = new System.Drawing.Size(681, 178); this.fldContent.TabIndex = 6; // // butSaveFile // this.butSaveFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.butSaveFile.Location = new System.Drawing.Point(618, 282); this.butSaveFile.Name = "butSaveFile"; this.butSaveFile.Size = new System.Drawing.Size(75, 23); this.butSaveFile.TabIndex = 7; this.butSaveFile.Text = "Save"; this.butSaveFile.UseVisualStyleBackColor = true; // // statusStrip1 // this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripStatusLabel1, this.lblSymbolCount}); this.statusStrip1.Location = new System.Drawing.Point(0, 319); this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Size = new System.Drawing.Size(705, 22); this.statusStrip1.TabIndex = 8; this.statusStrip1.Text = "statusStrip1"; // // toolStripStatusLabel1 // this.toolStripStatusLabel1.Name = "toolStripStatusLabel1"; this.toolStripStatusLabel1.Size = new System.Drawing.Size(130, 17); this.toolStripStatusLabel1.Text = "Колличество символов :"; // // lblSymbolCount // this.lblSymbolCount.Name = "lblSymbolCount"; this.lblSymbolCount.Size = new System.Drawing.Size(0, 17); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(705, 341); this.Controls.Add(this.statusStrip1); this.Controls.Add(this.butSaveFile); this.Controls.Add(this.fldContent); this.Controls.Add(this.numFont); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.fldFilePath); this.Controls.Add(this.butOpenFile); this.Controls.Add(this.butSelectFile); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.numFont)).EndInit(); this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button butSelectFile; private System.Windows.Forms.Button butOpenFile; private System.Windows.Forms.TextBox fldFilePath; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.NumericUpDown numFont; private System.Windows.Forms.TextBox fldContent; private System.Windows.Forms.Button butSaveFile; private System.Windows.Forms.StatusStrip statusStrip1; private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; private System.Windows.Forms.ToolStripStatusLabel lblSymbolCount; } } {/codecitation}