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

📄 notepad.cs

📁 一本很好的教材.C#开发者必备.内容全面,很难得哦.
💻 CS
字号:
// 记事本程序
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace NotePad
{
	/// <summary>
	/// NotePad的摘要说明。
	/// </summary>
	public class NotePad : Form
	{
		private TextBox textBox;
		private MenuItem menuFile;
		private MenuItem menuFile_Open;
		private MenuItem menuFile_SaveAs;
		private MenuItem menuFile_Exit;
		private MenuItem menuHelp_About;
		private MenuItem menuFile_Line1;
		private OpenFileDialog openFileDialog;
		private SaveFileDialog saveFileDialog;
		private MainMenu mainMenu;
		private System.ComponentModel.IContainer components;
		private MenuItem menuFile_Save;
		private MenuItem menuEdit;
		private MenuItem menuEdit_Undo;
		private MenuItem menuEdit_Line1;
		private MenuItem menuEdit_Cut;
		private MenuItem menuEdit_Copy;
		private MenuItem menuEdit_Paste;
		private MenuItem menuEdit_Del;
		private MenuItem menuFile_New;
		//初始打开文件的路径及名称(为空)。
		public string oldFileName = "";
		public NotePad()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
		}
		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox = new System.Windows.Forms.TextBox();
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.menuFile = new System.Windows.Forms.MenuItem();
			this.menuFile_New = new System.Windows.Forms.MenuItem();
			this.menuFile_Open = new System.Windows.Forms.MenuItem();
			this.menuFile_Save = new System.Windows.Forms.MenuItem();
			this.menuFile_SaveAs = new System.Windows.Forms.MenuItem();
			this.menuFile_Line1 = new System.Windows.Forms.MenuItem();
			this.menuFile_Exit = new System.Windows.Forms.MenuItem();
			this.menuEdit = new System.Windows.Forms.MenuItem();
			this.menuEdit_Undo = new System.Windows.Forms.MenuItem();
			this.menuEdit_Cut = new System.Windows.Forms.MenuItem();
			this.menuEdit_Copy = new System.Windows.Forms.MenuItem();
			this.menuEdit_Paste = new System.Windows.Forms.MenuItem();
			this.menuEdit_Del = new System.Windows.Forms.MenuItem();
			this.menuHelp_About = new System.Windows.Forms.MenuItem();
			this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
			this.menuEdit_Line1 = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// textBox
			// 
			this.textBox.Dock = System.Windows.Forms.DockStyle.Fill;
			this.textBox.Font = new System.Drawing.Font("新宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox.MaxLength = 1048576;
			this.textBox.Multiline = true;
			this.textBox.Name = "textBox";
			this.textBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textBox.Size = new System.Drawing.Size(492, 253);
			this.textBox.TabIndex = 0;
			this.textBox.Text = "";
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuFile,
																					 this.menuEdit});
			// 
			// menuFile
			// 
			this.menuFile.Index = 0;
			this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuFile_New,
																					 this.menuFile_Open,
																					 this.menuFile_Save,
																					 this.menuFile_SaveAs,
																					 this.menuFile_Line1,
																					 this.menuFile_Exit});
			this.menuFile.Text = "文件";
			// 
			// menuFile_New
			// 
			this.menuFile_New.Index = 0;
			this.menuFile_New.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
			this.menuFile_New.Text = "新建";
			this.menuFile_New.Click += new System.EventHandler(this.menuFile_New_Click);
			// 
			// menuFile_Open
			// 
			this.menuFile_Open.Index = 1;
			this.menuFile_Open.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
			this.menuFile_Open.Text = "打开";
			this.menuFile_Open.Click += new System.EventHandler(this.menuFile_Open_Click);
			// 
			// menuFile_Save
			// 
			this.menuFile_Save.Index = 2;
			this.menuFile_Save.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
			this.menuFile_Save.Text = "保存";
			this.menuFile_Save.Click += new System.EventHandler(this.menuFile_Save_Click);
			// 
			// menuFile_SaveAs
			// 
			this.menuFile_SaveAs.Index = 3;
			this.menuFile_SaveAs.Shortcut = System.Windows.Forms.Shortcut.CtrlShiftS;
			this.menuFile_SaveAs.Text = "保存为...";
			this.menuFile_SaveAs.Click += new System.EventHandler(this.menuFile_SaveAs_Click);
			// 
			// menuFile_Line1
			// 
			this.menuFile_Line1.Index = 4;
			this.menuFile_Line1.Text = "-";
			// 
			// menuFile_Exit
			// 
			this.menuFile_Exit.Index = 5;
			this.menuFile_Exit.Shortcut = System.Windows.Forms.Shortcut.CtrlE;
			this.menuFile_Exit.Text = "退出";
			this.menuFile_Exit.Click += new System.EventHandler(this.menuFile_Exit_Click);
			// 
			// menuEdit
			// 
			this.menuEdit.Index = 1;
			this.menuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuEdit_Undo,
																					 this.menuEdit_Cut,
																					 this.menuEdit_Copy,
																					 this.menuEdit_Paste,
																					 this.menuEdit_Del});
			this.menuEdit.Text = "编辑";
			// 
			// menuEdit_Undo
			// 
			this.menuEdit_Undo.Index = 0;
			this.menuEdit_Undo.Shortcut = System.Windows.Forms.Shortcut.CtrlZ;
			this.menuEdit_Undo.Text = "撤消";
			this.menuEdit_Undo.Click += new System.EventHandler(this.menuEdit_Undo_Click);
			// 
			// menuEdit_Cut
			// 
			this.menuEdit_Cut.Index = 1;
			this.menuEdit_Cut.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
			this.menuEdit_Cut.Text = "剪切";
			this.menuEdit_Cut.Click += new System.EventHandler(this.menuEdit_Cut_Click);
			// 
			// menuEdit_Copy
			// 
			this.menuEdit_Copy.Index = 2;
			this.menuEdit_Copy.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
			this.menuEdit_Copy.Text = "复制";
			this.menuEdit_Copy.Click += new System.EventHandler(this.menuEdit_Copy_Click);
			// 
			// menuEdit_Paste
			// 
			this.menuEdit_Paste.Index = 3;
			this.menuEdit_Paste.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
			this.menuEdit_Paste.Text = "粘贴";
			this.menuEdit_Paste.Click += new System.EventHandler(this.menuEdit_Paste_Click);
			// 
			// menuEdit_Del
			// 
			this.menuEdit_Del.Index = 4;
			this.menuEdit_Del.Shortcut = System.Windows.Forms.Shortcut.Del;
			this.menuEdit_Del.Text = "删除";
			// 
			// openFileDialog
			// 
			this.openFileDialog.DefaultExt = "*.TXT, *.CS";
			this.openFileDialog.Filter = "(*.TXT; *.CS)|*.TXT; *.CS|(所有文件)|*.*";
			// 
			// saveFileDialog
			// 
			this.saveFileDialog.FileName = "FileName";
			this.saveFileDialog.Filter = "Text File (*.TXT)|*.TXT|C#文件 (*.CS))|*.CS";
			// 
			// menuEdit_Line1
			// 
			this.menuEdit_Line1.Index = -1;
			this.menuEdit_Line1.Text = "";
			// 
			// NotePad
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(492, 253);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.textBox});
			this.Menu = this.mainMenu;
			this.Name = "NotePad";
			this.Text = "我的记事本";
			this.ResumeLayout(false);
		}
		#endregion
		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new NotePad());
		}
		private void menuFile_Open_Click(object sender, EventArgs e)
		{
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				StreamReader sr = new StreamReader(openFileDialog.FileName, Encoding.Default);
				oldFileName = openFileDialog.FileName;
				textBox.Text = sr.ReadToEnd();
				sr.Close();
			}
		}
		private void menuFile_New_Click(object sender, System.EventArgs e)
		{
			textBox.Text = "";
			oldFileName = "";
		}
		private void menuFile_Save_Click(object sender, EventArgs e)	
		{
			if (oldFileName == "")
			{
				menuFile_SaveAs_Click(sender, e);
			}
			else
			{
				StreamWriter sw = new StreamWriter(oldFileName);
				sw.Write(textBox.Text);
				sw.Close();
			}
		}
		private void menuFile_SaveAs_Click(object sender, EventArgs e)
		{
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				StreamWriter asw = new StreamWriter(saveFileDialog.FileName, true);
				asw.Write(textBox.Text);
				asw.Close();
			}
		}
		private void menuFile_Exit_Click(object sender, EventArgs e)
		{
			Close();
		}
		private void menuEdit_Undo_Click(object sender, System.EventArgs e)
		{
			textBox.Undo();
		}
		private void menuEdit_Cut_Click(object sender, System.EventArgs e)
		{
			textBox.Cut();
		}
		private void menuEdit_Copy_Click(object sender, System.EventArgs e)
		{
			textBox.Copy();
		}
		private void menuEdit_Paste_Click(object sender, System.EventArgs e)
		{
			textBox.Paste();
		}
		private void menuEdit_Del_Click(object sender, System.EventArgs e)
		{
			textBox.SelectedText = "";
		}
	}
}

⌨️ 快捷键说明

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