namespace Notepad
{
public partial class frmNotepad : Form
{
//*************************************************************************
/* 布尔变量b用于判断文件是新建的还是从磁盘打开的,
true表示文件是从磁盘打开的,false表示文件是新建的,默认值为false*/
bool b = false;
/* 布尔变量s用于判断文件件是否被保存,
true表示文件是已经被保存了,false表示文件未被保存,默认值为true*/
bool s = true;
//*************************************************************************
public frmNotepad()
{
InitializeComponent();
}
private void 文件FToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
// 判断当前文件是否从磁盘打开,或者新建时文档不为空,并且文件未被保存
if (b == true || rtxtNotepad.Text.Trim() != "")
{
// 若文件未保存
if (s == false)
{
string result;
result = MessageBox.Show("文件尚未保存,是否保存?",
"保存文件", MessageBoxButtons.YesNoCancel).ToString();
switch (result)
{
case "Yes":
// 若文件是从磁盘打开的
if (b == true)
{
// 按文件打开的路径保存文件
rtxtNotepad.SaveFile(odlgNotepad.FileName);
}
// 若文件不是从磁盘打开的
else if (sdlgNotepad.ShowDialog() == DialogResult.OK)
{
rtxtNotepad.SaveFile(sdlgNotepad.FileName);
}
s = true;
rtxtNotepad.Text = "";
break;
case "No":
b = false;