📄 txtedit.cs
字号:
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Text;
namespace File_browser
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class textedit : System.Windows.Forms.Form
{
private System.Windows.Forms.MenuItem menuFile;
private System.Windows.Forms.MenuItem menuSave;
private System.Windows.Forms.MenuItem menuSaveas;
private System.Windows.Forms.TextBox showtext;
private System.Windows.Forms.MainMenu StartMenu;
private string file,apath,afilename;
public static string spath,sname;
private System.Windows.Forms.MenuItem menuExit;
public textedit(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;
spath = path;
sname = filename;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
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.StartMenu = new System.Windows.Forms.MainMenu();
this.menuExit = new System.Windows.Forms.MenuItem();
this.menuFile = new System.Windows.Forms.MenuItem();
this.menuSave = new System.Windows.Forms.MenuItem();
this.menuSaveas = new System.Windows.Forms.MenuItem();
this.showtext = new System.Windows.Forms.TextBox();
//
// StartMenu
//
this.StartMenu.MenuItems.Add(this.menuExit);
this.StartMenu.MenuItems.Add(this.menuFile);
//
// menuExit
//
this.menuExit.Text = "退出";
this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
//
// menuFile
//
this.menuFile.MenuItems.Add(this.menuSave);
this.menuFile.MenuItems.Add(this.menuSaveas);
this.menuFile.Text = "文件";
//
// 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);
//
// 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 = "";
//
// textedit
//
this.Controls.Add(this.showtext);
this.Menu = this.StartMenu;
}
#endregion
private void menuExit_Click(object sender, System.EventArgs e)
{
if(file!=showtext.Text)
{
if(MessageBox.Show("当前文件内容已更改,您还没有保存,要保存并退出吗?", this.Text,
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2).ToString()=="Yes")
FileSave();
}
this.Close();
}
private void menuSave_Click(object sender, System.EventArgs e)
{
FileSave();
}
//保存
private void FileSave()
{
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();
file = showtext.Text;
}
}
}
private void menuSaveas_Click(object sender, System.EventArgs e)
{
Saveas saveas = new Saveas(apath,afilename,showtext.Text);
saveas.ShowDialog();
if(apath != spath||afilename != sname)
{
this.Text = sname;
file = showtext.Text;
apath = spath;
afilename = sname;
}
}
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";
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -