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

📄 form1.cs

📁 主要是有关文件操作方面的内容。
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace NotePad
{
	/// <summary>
	/// Form1 的摘要说明。

	/// </summary>
	
	public class Form1 : System.Windows.Forms.Form
	{
		public bool issaved;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem9;
		private System.Windows.Forms.MenuItem menuItem11;
		private System.Windows.Forms.MenuItem menuItem13;
		private System.Windows.Forms.MenuItem menuItem18;
		private System.Windows.Forms.MenuItem menuItem21;
		private System.Windows.Forms.MenuItem menuItem24;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private System.Windows.Forms.FontDialog fontDialog1;
		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.MenuItem Mnew;
		private System.Windows.Forms.MenuItem MOpen;
		private System.Windows.Forms.MenuItem Msave;
		private System.Windows.Forms.MenuItem MSaveas;
		private System.Windows.Forms.MenuItem Mpageset;
		private System.Windows.Forms.MenuItem Mprint;
		private System.Windows.Forms.MenuItem Mexit;
		private System.Windows.Forms.MenuItem Mundo;
		private System.Windows.Forms.MenuItem Mcut;
		private System.Windows.Forms.MenuItem Mcopy;
		private System.Windows.Forms.MenuItem Mpaste;
		private System.Windows.Forms.MenuItem Mdelete;
		private System.Windows.Forms.MenuItem Mselectall;
		private System.Windows.Forms.MenuItem Maddtime;
		private System.Windows.Forms.MenuItem Mwrap;
		private System.Windows.Forms.MenuItem Mfont;
		private System.Windows.Forms.MenuItem Mabout;
		private System.Windows.Forms.RichTextBox richtb;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// 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>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.Mnew = new System.Windows.Forms.MenuItem();
			this.MOpen = new System.Windows.Forms.MenuItem();
			this.Msave = new System.Windows.Forms.MenuItem();
			this.MSaveas = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.Mpageset = new System.Windows.Forms.MenuItem();
			this.Mprint = new System.Windows.Forms.MenuItem();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.Mexit = new System.Windows.Forms.MenuItem();
			this.menuItem11 = new System.Windows.Forms.MenuItem();
			this.Mundo = new System.Windows.Forms.MenuItem();
			this.menuItem13 = new System.Windows.Forms.MenuItem();
			this.Mcut = new System.Windows.Forms.MenuItem();
			this.Mcopy = new System.Windows.Forms.MenuItem();
			this.Mpaste = new System.Windows.Forms.MenuItem();
			this.Mdelete = new System.Windows.Forms.MenuItem();
			this.menuItem18 = new System.Windows.Forms.MenuItem();
			this.Mselectall = new System.Windows.Forms.MenuItem();
			this.Maddtime = new System.Windows.Forms.MenuItem();
			this.menuItem21 = new System.Windows.Forms.MenuItem();
			this.Mwrap = new System.Windows.Forms.MenuItem();
			this.Mfont = new System.Windows.Forms.MenuItem();
			this.menuItem24 = new System.Windows.Forms.MenuItem();
			this.Mabout = new System.Windows.Forms.MenuItem();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
			this.fontDialog1 = new System.Windows.Forms.FontDialog();
			this.richtb = new System.Windows.Forms.RichTextBox();
			this.contextMenu1 = new System.Windows.Forms.ContextMenu();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem11,
																					  this.menuItem21,
																					  this.menuItem24});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.Mnew,
																					  this.MOpen,
																					  this.Msave,
																					  this.MSaveas,
																					  this.menuItem6,
																					  this.Mpageset,
																					  this.Mprint,
																					  this.menuItem9,
																					  this.Mexit});
			this.menuItem1.Text = "文件";
			// 
			// Mnew
			// 
			this.Mnew.Index = 0;
			this.Mnew.Text = "新建";
			this.Mnew.Click += new System.EventHandler(this.Mnew_Click);
			// 
			// MOpen
			// 
			this.MOpen.Index = 1;
			this.MOpen.Text = "打开";
			this.MOpen.Click += new System.EventHandler(this.MOpen_Click);
			// 
			// Msave
			// 
			this.Msave.Index = 2;
			this.Msave.Text = "保存";
			this.Msave.Click += new System.EventHandler(this.Msave_Click);
			// 
			// MSaveas
			// 
			this.MSaveas.Index = 3;
			this.MSaveas.Text = "另存为";
			this.MSaveas.Click += new System.EventHandler(this.MSaveas_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 4;
			this.menuItem6.Text = "-";
			// 
			// Mpageset
			// 
			this.Mpageset.Index = 5;
			this.Mpageset.Text = "页面设置";
			// 
			// Mprint
			// 
			this.Mprint.Index = 6;
			this.Mprint.Text = "打印";
			// 
			// menuItem9
			// 
			this.menuItem9.Index = 7;
			this.menuItem9.Text = "-";
			// 
			// Mexit
			// 
			this.Mexit.Index = 8;
			this.Mexit.Text = "退出";
			this.Mexit.Click += new System.EventHandler(this.Mexit_Click);
			// 
			// menuItem11
			// 
			this.menuItem11.Index = 1;
			this.menuItem11.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.Mundo,
																					   this.menuItem13,
																					   this.Mcut,
																					   this.Mcopy,
																					   this.Mpaste,
																					   this.Mdelete,
																					   this.menuItem18,
																					   this.Mselectall,
																					   this.Maddtime});
			this.menuItem11.Text = "编辑";
			// 
			// Mundo
			// 
			this.Mundo.Index = 0;
			this.Mundo.Text = "撤消";
			this.Mundo.Click += new System.EventHandler(this.Mundo_Click);
			// 
			// menuItem13
			// 
			this.menuItem13.Index = 1;
			this.menuItem13.Text = "-";
			// 
			// Mcut
			// 
			this.Mcut.Enabled = false;
			this.Mcut.Index = 2;
			this.Mcut.Text = "剪切";
			this.Mcut.Click += new System.EventHandler(this.Mcut_Click);
			// 
			// Mcopy
			// 
			this.Mcopy.Enabled = false;
			this.Mcopy.Index = 3;
			this.Mcopy.Text = "复制";
			this.Mcopy.Click += new System.EventHandler(this.Mcopy_Click);
			// 
			// Mpaste
			// 
			this.Mpaste.Index = 4;
			this.Mpaste.Text = "粘贴";
			this.Mpaste.Click += new System.EventHandler(this.Mpaste_Click);
			// 
			// Mdelete
			// 
			this.Mdelete.Enabled = false;
			this.Mdelete.Index = 5;
			this.Mdelete.Text = "删除";
			this.Mdelete.Click += new System.EventHandler(this.Mdelete_Click);
			// 
			// menuItem18
			// 
			this.menuItem18.Index = 6;
			this.menuItem18.Text = "-";
			// 
			// Mselectall
			// 
			this.Mselectall.Index = 7;
			this.Mselectall.Text = "全选";
			this.Mselectall.Click += new System.EventHandler(this.Mselectall_Click);
			// 
			// Maddtime
			// 
			this.Maddtime.Index = 8;
			this.Maddtime.Text = "时间/日期   &F5";
			this.Maddtime.Click += new System.EventHandler(this.Maddtime_Click);
			// 
			// menuItem21
			// 
			this.menuItem21.Index = 2;
			this.menuItem21.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.Mwrap,
																					   this.Mfont});
			this.menuItem21.Text = "格式";
			// 
			// Mwrap
			// 
			this.Mwrap.Checked = true;
			this.Mwrap.Index = 0;
			this.Mwrap.Text = "自动换行";
			this.Mwrap.Click += new System.EventHandler(this.Mwrap_Click);
			// 
			// Mfont
			// 
			this.Mfont.Index = 1;
			this.Mfont.Text = "字体";
			this.Mfont.Click += new System.EventHandler(this.Mfont_Click);
			// 
			// menuItem24
			// 
			this.menuItem24.Index = 3;
			this.menuItem24.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.Mabout});
			this.menuItem24.Text = "帮助";
			// 
			// Mabout
			// 
			this.Mabout.Index = 0;
			this.Mabout.Text = "关于";
			this.Mabout.Click += new System.EventHandler(this.Mabout_Click);
			// 
			// saveFileDialog1
			// 
			this.saveFileDialog1.FileName = "doc1";
			this.saveFileDialog1.Filter = "*.cs;*.txt|*.*";
			this.saveFileDialog1.Title = "保存为";
			// 
			// richtb
			// 
			this.richtb.Dock = System.Windows.Forms.DockStyle.Fill;
			this.richtb.Name = "richtb";
			this.richtb.Size = new System.Drawing.Size(456, 333);
			this.richtb.TabIndex = 0;
			this.richtb.Text = "richtb";
			this.richtb.SelectionChanged += new System.EventHandler(this.richtb_SelectionChanged);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(456, 333);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.richtb});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
			
		static void Main() 
		{
			Application.Run(new Form1());
		}


		private void Mexit_Click(object sender, System.EventArgs e)
		{
			if(MessageBox.Show("你确认要退出吗?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes) 
			{

				this.Close();
			}
		}

		private void Mnew_Click(object sender, System.EventArgs e)
		{
			
			richtb.Text="";


		}

		private void MOpen_Click(object sender, System.EventArgs e)
		{
			if(openFileDialog1.ShowDialog()==DialogResult.OK)
			{
				StreamReader sr=new StreamReader(openFileDialog1.FileName,System.Text.Encoding.Default);
				richtb.Text=sr.ReadToEnd();
				sr.Close();
			}

		}

		private void Msave_Click(object sender, System.EventArgs e)
		{
			if(saveFileDialog1.ShowDialog()==DialogResult.OK)
			{
				StreamWriter sw=new StreamWriter(saveFileDialog1.FileName);
				sw.Write(richtb.Text);
				sw.Close();
				
			}
		}

		private void MSaveas_Click(object sender, System.EventArgs e)
		{
			if(saveFileDialog1.ShowDialog()==DialogResult.OK)
			{
				StreamWriter sw=new StreamWriter(saveFileDialog1.FileName);
				sw.Write(richtb.Text);
				sw.Close();
				
			}
		}

		private void Mcut_Click(object sender, System.EventArgs e)
		{
			richtb.Cut();
		}

		private void Mcopy_Click(object sender, System.EventArgs e)
		{
			richtb.Copy();
		}

		private void Mpaste_Click(object sender, System.EventArgs e)
		{
			richtb.Paste();
		}

		private void Mselectall_Click(object sender, System.EventArgs e)
		{
			richtb.SelectAll();
		}

		private void Maddtime_Click(object sender, System.EventArgs e)
		{
			DateTime dt=DateTime.Now;
			
			richtb.AppendText(dt.ToString());
		}

		private void Mwrap_Click(object sender, System.EventArgs e)
		{
			Mwrap.Checked=!Mwrap.Checked;
			if (Mwrap.Checked==true)
			{
				richtb.WordWrap=true;
			}
			else
				richtb.WordWrap=false;

		}

		private void Mfont_Click(object sender, System.EventArgs e)
		{
			if(fontDialog1.ShowDialog()==DialogResult.OK)
			{
				richtb.SelectionFont=fontDialog1.Font;
			}

		}

		private void Mabout_Click(object sender, System.EventArgs e)
		{
			(new about()).ShowDialog();
		}

		private void Mundo_Click(object sender, System.EventArgs e)
		{
			richtb.Undo();
		}

		private void Mdelete_Click(object sender, System.EventArgs e)
		{
			if(richtb.SelectedText!="")
			{
				richtb.SelectedText="";
			}

		}

		private void richtb_SelectionChanged(object sender, System.EventArgs e)
		{
			if(richtb.SelectedText!="")
			{
				Mcut.Enabled=true;
				Mcopy.Enabled=true;
				Mdelete.Enabled=true;
			}
			else
			{
				Mcut.Enabled=false;
				Mcopy.Enabled=false;
				Mdelete.Enabled=false;
			}

		}
	}
}

⌨️ 快捷键说明

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