初始化界面:
关于记事本窗体:
查找与替换窗体:
右键弹出菜单:
字体设置:
关闭提示:
另存为:
页面设置:
打印:
颜色对话框:
状态栏:
Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace demo28
{
public partial class Form1 : Form
{
/*布尔变量b用于判断文件是新建的还是从磁盘打开的
true表示文件是从磁盘打开的,false表示文件是新建的,默认值为false;
*/
bool b = false;
/*布尔变量s用于判断文件是否被保存
true表示文件是已经被保存的,false表示文件未被保存,默认值是true;
*/
bool s = true;
private string wrongMessage = "你好像遇到了错误...";
private string helpUrl = "https://cn.bing.com";
public Form1()
{
InitializeComponent();
}
//新建选项
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
//判断当前文件是否从磁盘打开,或者新建时文档不为空,并且文件未被保存
if (b == true || richTextBox1.Text.Trim() != "")
{
//若文件未保存
if (s == false)
{
string result;
result = MessageBox.Show("文件尚未保存,是否保存?", "保存文件", MessageBoxButtons.YesNoCancel).ToString();
switch (result)
{
case "Yes":
//若文件是从磁盘打开的
if (b == true)
{
//按文件打开的路径保存文件
richTextBox1.SaveFile(openFileDialog1.FileName);
}
//若文件不是从磁盘打开的
else if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName);
}
s = true;
richTextBox1.Text = "";
break;
case "No":
b = false;
richTextBox1.Text = "";
break;
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "共" + richTextBox1.Text.Length.ToString() + "个字符 ";
int totalline = richTextBox1.GetLineFromCharIndex(richTextBox1.Text.Length);//得到总行数
int index = richTextBox1.GetFirstCharIndexOfCurrentLine();//得到当前行第一个字符的索引
int line = richTextBox1.GetLineFromCharIndex(index) + 1;//得到当前行的行号
int col = richTextBox1.SelectionStart - index + 1;//.SelectionStart得到光标所在位置的索引 - 当前行第一个字符的索引 = 光标所在的列数
toolStripStatusLabel2.Text = "第" + line + "行,第" + col + "列 ";
toolStripStatusLabel3.Text = DateTime.Now.ToString() + " ";
}
private void Form1_Load(object sender, EventArgs e)
{
toolStripStatusLabel3.Text = DateTime.Now.ToString();
}
public void richTextBox1_TextChanged(object sender, EventArgs e)
{
//文本被修改后,设置s为false,表示文件未保存
s = false;
}
//打开选项
private void 打开OCtrlShiftNToolStripMenuItem_Click(object sender, EventArgs e)
{
//判断当前文件是否从磁盘打开,或者新建时文档不为空,并且文件未被保存
if (b == true || richTextBox1.Text.Trim() != "")
{
//若文件未保存
if (s == false)
{
string result;
result = MessageBox.Show("文件尚未保存,是否保存?", "保存文件", MessageBoxButtons.YesNoCancel).ToString();
switch (result)
{
case "Yes":
//若文件是从磁盘打开的
if (b == true)
{
//按文件打开的路径保存文件
richTextBox1.SaveFile(openFileDialog1.FileName);
}
//若文件不是从磁盘打开的
else if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName);
}
s = true;
break;
case "No":
b = false;
richTextBox1.Text = "";
break;
}
}
}
openFileDialog1.RestoreDirectory = true;
if ((openFileDialog1.ShowDialog() == DialogResult.OK) && openFileDialog1.FileName != "")
{
richTextBox1.LoadFile(openFileDialog1.FileName);//打开代码语句
b = true;
}
s = true;
}
private void 保存SCtrlSToolStripMenuItem_Click(object sender, EventArgs e)
{
if (b == true && richTextBox1.Modified == true)
{
richTextBox1.SaveFile(openFileDialog1.FileName);
s = true;
}
else if (b == false && richTextBox1.Text.Trim() != "" && saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName);//保存语句
s = true;
b = true;
openFileDialog1.FileName = saveFileDialog1.FileName;
}
}
private void 另存为ACtrlShiftSToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName);
s = true;
}
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 页面设置UToolStripMenuItem_Click(