📄 start.cs
字号:
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Text;
namespace notepad
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Start : System.Windows.Forms.Form
{
private System.Windows.Forms.MenuItem menuAbout;
private System.Windows.Forms.MenuItem menuFile;
private System.Windows.Forms.MenuItem menuNew;
private System.Windows.Forms.MenuItem menuOpen;
private System.Windows.Forms.MenuItem menuSave;
private System.Windows.Forms.MenuItem menuSaveas;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuExit;
private System.Windows.Forms.TextBox showtext;
private System.Windows.Forms.MainMenu StartMenu;
private string file,apath,afilename;
public Start(string path,string filename)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
this.Text = filename;
OpenFile(path,filename);
file = showtext.Text;
apath = path;
afilename = filename;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
static void Main()
{
Application.Run(new Start("","untitled.txt"));
}
#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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Start));
this.StartMenu = new System.Windows.Forms.MainMenu();
this.menuAbout = new System.Windows.Forms.MenuItem();
this.menuFile = new System.Windows.Forms.MenuItem();
this.menuNew = new System.Windows.Forms.MenuItem();
this.menuOpen = new System.Windows.Forms.MenuItem();
this.menuSave = new System.Windows.Forms.MenuItem();
this.menuSaveas = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuExit = new System.Windows.Forms.MenuItem();
this.showtext = new System.Windows.Forms.TextBox();
//
// StartMenu
//
this.StartMenu.MenuItems.Add(this.menuAbout);
this.StartMenu.MenuItems.Add(this.menuFile);
//
// menuAbout
//
this.menuAbout.Text = "关于";
this.menuAbout.Click += new System.EventHandler(this.menuAbout_Click);
//
// menuFile
//
this.menuFile.MenuItems.Add(this.menuNew);
this.menuFile.MenuItems.Add(this.menuOpen);
this.menuFile.MenuItems.Add(this.menuSave);
this.menuFile.MenuItems.Add(this.menuSaveas);
this.menuFile.MenuItems.Add(this.menuItem1);
this.menuFile.MenuItems.Add(this.menuExit);
this.menuFile.Text = "文件";
//
// menuNew
//
this.menuNew.Text = "新建";
this.menuNew.Click += new System.EventHandler(this.menuNew_Click);
//
// menuOpen
//
this.menuOpen.Text = "打开";
this.menuOpen.Click += new System.EventHandler(this.menuOpen_Click);
//
// menuSave
//
this.menuSave.Text = "保存";
this.menuSave.Click += new System.EventHandler(this.menuSave_Click);
//
// menuSaveas
//
this.menuSaveas.Text = "另存为";
this.menuSaveas.Click += new System.EventHandler(this.menuSaveas_Click);
//
// menuItem1
//
this.menuItem1.Text = "-";
//
// menuExit
//
this.menuExit.Text = "退出";
this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
//
// showtext
//
this.showtext.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular);
this.showtext.Multiline = true;
this.showtext.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.showtext.Size = new System.Drawing.Size(176, 180);
this.showtext.Text = "";
//
// Start
//
this.Controls.Add(this.showtext);
this.Menu = this.StartMenu;
}
#endregion
private void OpenFile(string path,string filename)
{
if(path!="")
{
showtext.Text = "";
string input;
using (StreamReader fs = new StreamReader(path+filename,System.Text.Encoding.Default,true))
{
while ((input=fs.ReadLine())!=null)
showtext.Text += input + "\r\n";
}
}
}
private void menuExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void menuAbout_Click(object sender, System.EventArgs e)
{
About about = new About();
about.ShowDialog();
}
private void menuOpen_Click(object sender, System.EventArgs e)
{
if(showtext.Text!=file)
{
if(MessageBox.Show("请注意您编辑的信息尚未保存,如果打开新文件将会丢失已修改内容!您真的要这样做吗?", this.Text,
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2).ToString()=="Yes")
{
Treeview tree = new Treeview();
tree.ShowDialog();
}
}
else
{
Treeview tree = new Treeview();
tree.ShowDialog();
}
}
private void menuSave_Click(object sender, System.EventArgs e)
{
if(apath=="")
{
Saveas saveas = new Saveas(apath,afilename,showtext.Text);
saveas.ShowDialog();
}
else
{
string filepath = apath+afilename;
//写入文件
using (StreamWriter fs = new StreamWriter(filepath,false,System.Text.Encoding.Default))
{
string[] content = System.Text.RegularExpressions.Regex.Split(showtext.Text,"\r\n");
for(int i=0;i<content.Length;i++)
fs.WriteLine(content[i]);
fs.Close();
}
}
}
private void StartNew()
{
Start start = new Start("","untitled.txt");
start.Show();
}
private void menuNew_Click(object sender, System.EventArgs e)
{
if(showtext.Text!=file)
{
if(MessageBox.Show("请注意您编辑的信息尚未保存,如果打开新文件将会丢失已修改内容!您真的要这样做吗?", this.Text,
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2).ToString()=="Yes")
{
StartNew();
}
}
else
StartNew();
}
private void menuSaveas_Click(object sender, System.EventArgs e)
{
Saveas saveas = new Saveas(apath,afilename,showtext.Text);
saveas.ShowDialog();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -