注释部分需自行添加
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;//提供了关于文件、数据流python基础教程
的读取和写入操作
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;//提供了用于与事件日志、性能计数器和系统进程进行交互c#教程的类
1.新建文件:
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
if (txtBox.Modified == true)
{
DialogResult dr = MessageBox.Show("文件发生变化,是否更改保存?", "注意", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
保存SToolStripMenuItem_Click(sender, e);
return;
}
else if (dr == DialogResult.Cancel)
{
return;
}
txtBox.Clear();
this.Text = "NewNotepad";
}
else
{
txtBox.Clear();
this.Text = "NewNotepad";
}
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
2.打开:
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog.FileName;
OpenFile();
}
}
protected void OpenFile()
{
try
{
txtBox.Clear();
txtBox.Text = File.ReadAllText(filename);
}
catch
{ MessageBox.Show("Error!"); }
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
3.保存:
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
StreamWriter sw = File.AppendText(Application.ExecutablePath);
sw.Write(txtBox.Text);
sw.Dispose();
}
catch
{
SaveFileDialog sf = new SaveFileDialog();
sf.DefaultExt = "*.txt";
sf.Filter = "文本文档(.txt)|*.txt";
if (sf.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = File.AppendText(sf.FileName);
sw.Write(txtBox.Text);
sw.Dispose();
}
}
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
4.另存为:
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
string name;
SaveFileDialog save = new SaveFileDialog();
save.Filter = "*.txt|*.TXT|(*.*)|*.*";
if (save.ShowDialog() == DialogResult.OK)
{
name = save.FileName;
FileInfo info = new FileInfo(name);
StreamWriter writer = info.CreateText();
writer.Write(txtBox.Text);
writer.Close();
}
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
5.打印:
private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
{
this.printDialog.Document = this.printDocument;
this.printDialog.PrinterSettings = this.pageSetupDialog.PrinterSettings;
if (this.printDialog.ShowDialog() == DialogResult.OK)
{
try
{
this.printDocument.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误信息!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19