1. 设计一个窗体应用程序,模拟写字板应用程序的基本功能。具体功能要求如下:
(1)“文件”菜单中有“新建”、“打开”、“保存”、“退出”子菜单。
(2)“编辑”菜单中有“剪切”、“复制”、“粘贴”、“撤销”、“重复”、“全选”子菜单。
(3)工具菜单中有“颜色”、“字体”、“状态栏”功能。
(4)帮助菜单中有“关于”菜单功能。
(5)实现“编辑”菜单中、“工具”栏目中“剪切”、“复制”、“粘贴”、“撤销”、“重复”、“全选”的功能。
(6)实现文件的新建、打开、保存功能。(利用RichTextBox控件)
(7)文件菜单添加打开文本文件、保存文本文件菜单项,实现文件的打开、保存功能。(利用文件读写类)

提示:
(1)添加菜单栏、工具栏、RichTextBox控件。
(2)在打开对话框中要想只显示文本文档,可以在通用对话框Filter属性中设置*.txt类别。richTextBox的LoadFile、SaveFile方法中要指定文件流为RichTextBoxStreamType.PlainText
(3)点击菜单和工具栏相关按钮都实现同一个功能,所以会调用同一个方法。设计一系列公有方法实现相关功能。建议完成一个方法,就运行测试一个方法,问题解决后再编写下一个方法,避免出现太多错误。

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace MyNote
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 this.toolStripStatusLabel1.Text = "Welcome To My Note";
18 this.toolStripStatusLabel2.Text = DateTime.Now.ToString();
19
20 }
21
22 #region 自定义方法
23
24 public void TextCopy()
25 {
26 this.richTextBox1.Copy();
27 }
28
29 public void TextCut()
30 {
31 this.richTextBox1.Cut();
32 }
33
34 public void TextPaste()
35 {
36 this.richTextBox1.Paste();
37 }
38
39 public void TextUndo()
40 {
41 this.richTextBox1.Undo();
42 }
43
44 public void TextRedo()
45 {
46 this.richTextBox1.Redo();
47 }
48
49 public void TextSelectAll()
50 {
51 this.richTextBox1.SelectAll();
52 }
53
54 public void FileOpenMethod()
55 {
56 OpenFileDialog dia = new OpenFileDialog();
57 dia.Filter = "文本文件(*.txt)|*.txt|所有文件|*.*";
58
59 //dia.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
60 if (dia.ShowDialog() == DialogResult.OK)
61 {
62 this.richTextBox1.LoadFile(dia.FileName, RichTextBoxStreamType.PlainText);
63 this.toolStripStatusLabel1.Text = dia.SafeFileName;
64 this.toolStripStatusLabel2.Text = DateTime.Now.ToString();
65 }
66 }
67
68 public void FileSaveMethod()
69 {
70 SaveFileDialog dia = new SaveFileDialog();
71 dia.Filter = "文本文件(*.txt)|*.txt|所有文件|*.*";
72
73 //dia.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
74 if (dia.ShowDialog() == DialogResult.OK)
75 {
76 this.richTextBox1.SaveFile(dia.FileName, RichTextBoxStreamType.PlainText);
77 }
78 }
79
80 public void About()
81 {
82 AboutBox1 dia = new AboutBox1();
83 dia.ShowDialog();
84 }
85 #endregion
86
87
88 #region 菜单栏方法
89 private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
90 {
91 this.toolStripStatusLabel1.Text = "New blank Page";
92 this.toolStripStatusLabel2.Text = DateTime.Now.ToString();
93 this.richTextBox1.Text = "";
94 }
95
96 private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
97 {
98 FileOpenMethod();
99 }
100
101 private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
102 {
103 FileSaveMethod();
104 }
105
106 private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
107 {
108 TextCut();
109 }
110 private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
111 {
112 TextCopy();
113 }
114
115 private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
116 {
117 TextPaste();
118 }
119
120 private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
121 {
122 TextUndo();
123 }
124
125 private void 重复ToolStripMenuItem_Click(object sender, EventArgs e)
126 {
127 TextRedo();
128 }
129 private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
130 {
131 TextSelectAll();
132 }
133
134 private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
135 {
136 ColorDialog dia = new ColorDialog();
137 dia.Color = this.richTextBox1.SelectionColor;
138 if (dia.ShowDialog() == DialogResult.OK)
139 {
140 this.richTextBox1.SelectionColor = dia.Color;
141 }
142 }
143
144 private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
145 {
146 FontDialog dia = new FontDialog();
147 dia.Font = this.richTextBox1.SelectionFont;
148 dia.Color = this.richTextBox1.SelectionColor;
149 dia.ShowColor = true;
150 if (dia.ShowDialog() == DialogResult.OK)
151 {
152 this.richTextBox1.SelectionFont = dia.Font;
153 this.richTextBox1.SelectionColor = dia.Color;
154 }
155 }
156
157 private void 状态框ToolStripMenuItem_Click(object sender, EventArgs e)
158 {
159 this.statusStrip1.Visible = !this.statusStrip1.Visible;
160 状态框ToolStripMenuItem.Checked = this.statusStrip1.Visible;
161 }
162
163 private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
164 {
165 About();
166 }
167
168 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
169 {
170 Application.Exit();
171 }
172
173 #endregion
174
175
176
177 #region 工具栏方法
178
179 //新建按键
180 private void toolStripButton1_Click(object sender, EventArgs e)
181 {
182 this.toolStripStatusLabel1.Text = "New blank Page";
183 this.toolStripStatusLabel2.Text = DateTime.Now.ToString();
184 this.richTextBox1.Text = "";
185 }
186
187 //打开按键
188 private void toolStripButton2_Click(object sender, EventArgs e)
189 {
190 FileOpenMethod();
191 }
192
193 //保存按键
194 private void toolStripButton3_Click(object sender, EventArgs e)
195 {
196 FileSaveMethod();
197 }
198
199 //复制按钮
200 private void toolStripButton4_Click(object sender, EventArgs e)
201 {
202 TextCopy();
203 }
204
205 //剪切按钮
206 private void toolStripButton5_Click(object sender, EventArgs e)
207 {
208 TextCut();
209 }
210
211 //粘贴按钮
212 private void toolStripButton6_Click(object sender, EventArgs e)
213 {
214 TextPaste();
215 }
216
217 //撤销按钮
218 private void toolStripButton7_Click(object sender, EventArgs e)
219 {
220 TextUndo();
221 }
222
223 //重复按钮
224 private void toolStripButton8_Click(object sender, EventArgs e)
225 {
226 TextRedo();
227 }
228
229 private void toolStripButton9_Click(object sender, EventArgs e)
230 {
231 About();
232 }
233 #endregion
234 }
235 }
