⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 edittxt.cs

📁 需要的下
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace FileManager
{
	/// <summary>
	/// EditTxt 的摘要说明。
	/// </summary>
	public class EditTxt : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.RichTextBox txtContent;
		private System.Windows.Forms.OpenFileDialog dlgOpenFile;
		private System.Windows.Forms.SaveFileDialog dlgSaveFile;
		private System.Windows.Forms.Label lbFullname;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.MenuItem OpenMenu;
		private System.Windows.Forms.MenuItem NewMenu;
		private System.Windows.Forms.MenuItem SaveAsMenu;
		private System.Windows.Forms.MenuItem ExitMenu;
		private System.Windows.Forms.MenuItem SaveMenu;
		private System.Windows.Forms.MenuItem menuItemBold;
		private System.Windows.Forms.MenuItem menuItemItalic;
		private System.Windows.Forms.MenuItem menuItemUnderline;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public EditTxt()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.OpenMenu = new System.Windows.Forms.MenuItem();
			this.NewMenu = new System.Windows.Forms.MenuItem();
			this.SaveMenu = new System.Windows.Forms.MenuItem();
			this.SaveAsMenu = new System.Windows.Forms.MenuItem();
			this.ExitMenu = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.menuItemBold = new System.Windows.Forms.MenuItem();
			this.menuItemItalic = new System.Windows.Forms.MenuItem();
			this.menuItemUnderline = new System.Windows.Forms.MenuItem();
			this.lbFullname = new System.Windows.Forms.Label();
			this.txtContent = new System.Windows.Forms.RichTextBox();
			this.dlgOpenFile = new System.Windows.Forms.OpenFileDialog();
			this.dlgSaveFile = new System.Windows.Forms.SaveFileDialog();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem7});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.OpenMenu,
																					  this.NewMenu,
																					  this.SaveMenu,
																					  this.SaveAsMenu,
																					  this.ExitMenu});
			this.menuItem1.Text = "编辑(&E)";
			// 
			// OpenMenu
			// 
			this.OpenMenu.Index = 0;
			this.OpenMenu.Text = "打开(&O)";
			this.OpenMenu.Click += new System.EventHandler(this.OpenMenu_Click);
			// 
			// NewMenu
			// 
			this.NewMenu.Index = 1;
			this.NewMenu.Text = "新建(&N)";
			this.NewMenu.Click += new System.EventHandler(this.NewMenu_Click);
			// 
			// SaveMenu
			// 
			this.SaveMenu.Index = 2;
			this.SaveMenu.Text = "保存(&S)";
			this.SaveMenu.Click += new System.EventHandler(this.SaveMenu_Click);
			// 
			// SaveAsMenu
			// 
			this.SaveAsMenu.Index = 3;
			this.SaveAsMenu.Text = "另存为...(&A)";
			this.SaveAsMenu.Click += new System.EventHandler(this.SaveAsMenu_Click);
			// 
			// ExitMenu
			// 
			this.ExitMenu.Index = 4;
			this.ExitMenu.Text = "退出(&X)";
			this.ExitMenu.Click += new System.EventHandler(this.ExitMenu_Click);
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 1;
			this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemBold,
																					  this.menuItemItalic,
																					  this.menuItemUnderline});
			this.menuItem7.Text = "选项(&P)";
			// 
			// menuItemBold
			// 
			this.menuItemBold.Index = 0;
			this.menuItemBold.Text = "Bold(&B)";
			this.menuItemBold.Click += new System.EventHandler(this.menuItemBold_Click);
			// 
			// menuItemItalic
			// 
			this.menuItemItalic.Index = 1;
			this.menuItemItalic.Text = "Italic(&I)";
			this.menuItemItalic.Click += new System.EventHandler(this.menuItemItalic_Click);
			// 
			// menuItemUnderline
			// 
			this.menuItemUnderline.Index = 2;
			this.menuItemUnderline.Text = "Underline(&U)";
			this.menuItemUnderline.Click += new System.EventHandler(this.menuItemUnderline_Click);
			// 
			// lbFullname
			// 
			this.lbFullname.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.lbFullname.Location = new System.Drawing.Point(112, 8);
			this.lbFullname.Name = "lbFullname";
			this.lbFullname.Size = new System.Drawing.Size(272, 30);
			this.lbFullname.TabIndex = 0;
			this.lbFullname.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// txtContent
			// 
			this.txtContent.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.txtContent.Location = new System.Drawing.Point(0, 48);
			this.txtContent.Name = "txtContent";
			this.txtContent.Size = new System.Drawing.Size(392, 232);
			this.txtContent.TabIndex = 1;
			this.txtContent.Text = "";
			// 
			// dlgOpenFile
			// 
			this.dlgOpenFile.Filter = "Text Document(*.txt)|*.txt";
			this.dlgOpenFile.FilterIndex = 2;
			// 
			// dlgSaveFile
			// 
			this.dlgSaveFile.Filter = "Text Document(*.txt)|*.txt";
			this.dlgSaveFile.FilterIndex = 2;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.TabIndex = 2;
			this.label1.Text = "当前的文本路径:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// EditTxt
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(392, 286);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.txtContent);
			this.Controls.Add(this.lbFullname);
			this.Menu = this.mainMenu1;
			this.Name = "EditTxt";
			this.Text = "EditTxt";
			this.Load += new System.EventHandler(this.EditTxt_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void EditTxt_Load(object sender, System.EventArgs e)
		{
			try
			{
				FileStream fs = new FileStream(lbFullname.Text,FileMode.Open,FileAccess.Read);
				StreamReader reader = new StreamReader(fs);
				txtContent.Text = reader.ReadToEnd();
				fs.Close();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void OpenMenu_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(dlgOpenFile.ShowDialog() == DialogResult.OK)
				{
					string fileName = dlgOpenFile.FileName;
					FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
					StreamReader reader = new StreamReader(fs);
					lbFullname.Text = fileName;
					txtContent.Text = reader.ReadToEnd();
					fs.Close();
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void NewMenu_Click(object sender, System.EventArgs e)
		{
			this.txtContent.Clear();
		}

		private void SaveAsMenu_Click(object sender, System.EventArgs e)
		{
			if(dlgSaveFile.ShowDialog() == DialogResult.OK)
			{
				string fileName = dlgSaveFile.FileName;
				try
				{
					Stream stream = File.OpenWrite(fileName);
					using(StreamWriter writer = new StreamWriter(stream))
					{
						writer.Write(txtContent.Text);
						writer.Flush();
					}
					MessageBox.Show("文件" + lbFullname.Text + "保存成功!");
				}
				catch(Exception ex)
				{
					MessageBox.Show(ex.Message);
				}
			}
		}

		private void ExitMenu_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void SaveMenu_Click(object sender, System.EventArgs e)
		{
			try
			{
				FileStream fs = new FileStream(lbFullname.Text,FileMode.Open,FileAccess.Write);
				StreamWriter writer = new StreamWriter(fs);
				writer.Write(txtContent.Text);
				writer.Flush();
				fs.Close();
				MessageBox.Show("文件" + lbFullname.Text + "保存成功!");
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void menuItemBold_Click(object sender, System.EventArgs e)
		{
			Font font = new Font(txtContent.SelectionFont,(txtContent.SelectionFont.Bold?txtContent.SelectionFont.Style&~FontStyle.Bold:txtContent.SelectionFont.Style|FontStyle.Bold));
			txtContent.Font = font;
			bool check = menuItemBold.Checked;
			menuItemBold.Checked = !check;
		}

		private void menuItemItalic_Click(object sender, System.EventArgs e)
		{
			Font font = new Font(txtContent.SelectionFont,(txtContent.SelectionFont.Italic?txtContent.SelectionFont.Style&~FontStyle.Italic:txtContent.SelectionFont.Style|FontStyle.Italic));
			txtContent.Font = font;
			bool check = menuItemItalic.Checked;
			menuItemItalic.Checked = !check;
		}

		private void menuItemUnderline_Click(object sender, System.EventArgs e)
		{
			Font font = new Font(txtContent.SelectionFont,(txtContent.SelectionFont.Underline?txtContent.SelectionFont.Style&~FontStyle.Underline:txtContent.SelectionFont.Style|FontStyle.Underline));
			txtContent.Font = font;
			bool check = menuItemUnderline.Checked;
			menuItemUnderline.Checked = !check;
		}

		public string lbFullName
		{
			get
			{
				return this.lbFullname.Text.ToString();
			}
			set
			{
				this.lbFullname.Text = value;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -