一、目标实现
运用C#中的控件实现简单的记事本功能。参考电脑中的记事本,完成关于新建,保存,粘贴,复制的基本功能的完成。
解题思路:合理的运用C#中的相关控件。
二、代码实现
1.部分代码
(1)新建功能的代码
private void 新建NCtrlNToolStripMenuItem_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;
rtxtNotepad.Text = "";
break;
}
}
}
}
代码中的b,s是两个用于判断的元素。b用于判断文件是新建的还是从磁盘打开的,s用于判断文件是否被保存。
(2)打开功能代码
private void tsmiOpen_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;
break;
case "No":
b = false;
rtxtNotepad.Text = "";
break;
}
}
}
odlgNotepad.RestoreDirectory = true;
if ((odlgNotepad.ShowDialog() == DialogResult.OK) && odlgNotepad.FileName != "")
{
rtxtNotepad.LoadFile(odlgNotepad.FileName);//打开代码语句
b = true;
}
s = true;
}