点击事件
1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 } 7 8 // 用于存储当前操作的文件的名称 9 private string textFileName = ""; 10 private string filePath = ""; 11 private void 新建_Click(object sender, EventArgs e) 12 { 13 textFileName = ""; 14 // 新建一个文本时,若输入框中的内容不为空,应先提示“是否保存” 15 if (inputInfo.Text != string.Empty) 16 { 17 // 若用户选择“是”,应弹出保存文件的对话框 18 if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) 19 == DialogResult.Yes) 20 { 21 // 保存文件 22 Save(); 23 Text = "新建-Mickey记事本"; 24 inputInfo.Text = ""; 25 } 26 else if (MessageBox.Show("是否保存当前文件?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) 27 == DialogResult.No) 28 { 29 // 用户选择不保存时将输入框中的内容清除 30 inputInfo.Text = ""; 31 } 32 } 33 } 34 private void 打开_Click(object sender, EventArgs e) 35 { 36 OpenFileDialog openFile = new OpenFileDialog(); 37 openFile.Filter = "文本文件(*.txt)|*.txt"; 38 if (openFile.ShowDialog() == DialogResult.OK) 39 { 40 StreamReader sr = new StreamReader(openFile.FileName); 41 inputInfo.Text = sr.ReadToEnd(); 42 sr.Close(); 43 FileInfo fileInfo = new FileInfo(openFile.FileName); 44 // 把标题改为打开的文件的名称 45 Text = "*" + fileInfo.Name + "-Mickey记事本"; 46 textFileName = fileInfo.Name; 47 } 48 } 49 50 51 private void 保存_Click(object sender, EventArgs e) 52 { 53 Save(); 54 } 55 // “保存” 56 private void Save() 57 { 58 if (!textFileName.Equals("")) 59 { 60 SaveFileDialog saveFile = new SaveFileDialog(); 61 // 默认保存格式 62 saveFile.Filter = "文本文件(*.txt)|*.txt"; 63 StreamWriter sw = new StreamWriter(filePath, false); 64 sw.Write(inputInfo.Text); 65 sw.Close(); 66 67 MessageBox.Show("文件保存成功!", "Mickey温馨提示"); 68 filePath = saveFile.FileName; 69 // 把标题改为打开的文件的名称 70 Text = textFileName + "-Mickey记事本"; 71 } 72 else 73 { 74 // 成员变量为“”,说明文件第一次保存,执行“另存为”操作 75 HoldFile(); 76 } 77 } 78 79 80 private void HoldFile() 81 { 82 // 若用户选择另保存文件 83 SaveFileDialog saveFile = new SaveFileDialog(); 84 // 默认保存格式 85 saveFile.Filter = "文本文件(*.txt)|*.txt"; 86 if (saveFile.ShowDialog() == DialogResult.OK) 87 { 88 StreamWriter sw = new StreamWriter(saveFile.FileName, false); 89 sw.WriteLine(inputInfo.Text); 90 sw.Close(); 91 MessageBox.Show("文件保存成功!", "Mickey温馨提示"); 92 filePath = saveFile.FileName; 93 } 94 // 判断是第一次保存还是第二次 95 if (textFileName.Equals("")) 96 { 97 FileInfo fileInfo = new FileInfo(saveFile.FileName); 98 Text = fileInfo.Name + "-Mickey记事本"; 99 textFileName = fileInfo.Name; 100 } 101 else 102 { 103 // 把标题改为打开的文件的名称 104 Text = textFileName + "-Mickey记事本"; 105 } 106 } 107 108 private void 另存为_Click(object sender, EventArgs e) 109 { 110 HoldFile(); 111 } 112 113 114 private void 退出_Click(object sender, EventArgs e) 115 { 116 // 退出时应提示用户是否保存当前文本文件 117 DialogResult result = MessageBox.Show("是否将更改保存?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); 118 if (result == DialogResult.Yes) 119 { 120 Save(); 121 Application.Exit(); 122 } 123 else if (result == DialogResult.No) 124 { 125 Application.Exit(); 126 } 127 } 128 129 // 这个成员变量用来存储用户选择进行操作的字符串 130 private string selectedInfo = ""; 131 132 private void 编辑_Click(object sender, EventArgs e) 133 { 134 if ((inputInfo.SelectedText.Equals("")) && (selectedInfo.Equals(""))) 135 { 136 剪切.Enabled = false; 137 复制.Enabled = false; 138 粘贴.Enabled = false; 139 删除.Enabled = false; 140 } 141 else 142 { 143 剪切.Enabled = true; 144 复制.Enabled = true; 145 粘贴.Enabled = true; 146 删除.Enabled = true; 147 } 148 } 149 150 private void 撤销_Click(object sender, EventArgs e) 151 { 152 this.inputInfo.Undo(); 153 } 154 private void 剪切_Click(object sender, EventArgs e) 155 { 156 selectedInfo = inputInfo.SelectedText; 157 this.inputInfo.Cut(); 158 } 159 private void 复制_Click(object sender, EventArgs e) 160 { 161 selectedInfo = inputInfo.SelectedText; 162 this.inputInfo.Copy(); 163 } 164 private void 粘贴_Click(object sender, EventArgs e) 165 { 166 this.inputInfo.Paste(); 167 } 168 private void 删除_Click(object sender, EventArgs e) 169 { 170 this.inputInfo.SelectedText = ""; 171 } 172 173 174 175 private void 查找_Click(object sender, EventArgs e) 176 { 177 if (inputInfo.Text == string.Empty) 178 { 179 MessageBox.Show("请确保要查找的文件的内容不为空!", "Mickey温馨提示"); 180 } 181 else 182 { 183 //Form2 fr2 = new Form2(); 184 //fr2.sender(this); 185 //fr2.Show(); 186 } 187 } 188 private void 查找下一个_Click(object sender, EventArgs e) 189 { 190 } 191 192 private void 全选_Click(object sender, EventArgs e) 193 { 194 this.inputInfo.SelectAll(); 195 //全选_Click(sender,e); 196 } 197 private void 日期时间_Click(object sender, EventArgs e) 198 { 199 inputInfo.Text += "现在时间是:" + DateTime.Now.ToString(); 200 } 201 202 203 204 private void 自动换行_Click(object sender, EventArgs e) 205 { 206 if (自动换行.Checked == true) 207 { 208 inputInfo.WordWrap = true; 209 } 210 else 211 { 212 inputInfo.WordWrap = false; 213 } 214 } 215 216 217 private void 字体_Click(object sender, EventArgs e) 218 { 219 FontDialog fontDialog = new FontDialog(); 220 if (fontDialog.ShowDialog() == DialogResult.OK) 221 { 222 inputInfo.Font = fontDialog.Font; 223 } 224 } 225 226 227 private void 查看_Click(object sender, EventArgs e) 228 { 229 if (inputInfo.Text.Length > 0) 230 { 231 状态栏.Enabled = true; 232 } 233 else 234 { 235 状态栏.Enabled = false; 236 } 237 } 238 239 private void 状态栏_Click(object sender, EventArgs e) 240 { 241 if (状态栏.Checked == true) 242 { 243 状态栏.Checked = false; 244 statusStrip1.Visible = false; 245 } 246 else 247 { 248 状态栏.Checked = true; 249 statusStrip1.Visible = true; 250 251 } 252 253 } 254 255 private void 工具栏_Click(object sender, EventArgs e) 256 { 257 if (工具栏.Checked == true) 258 { 259 工具栏.Checked = false; 260 toolStrip1.Visible = false; 261 inputInfo.Location = new Point(0, 24); 262 } 263 else 264 { 265 工具栏.Checked = true; 266 toolStrip1.Visible = true; 267 inputInfo.Location = new Point(0, 49); 268 } 269 270 } 271 272 private void 查看帮助_Click(object sender, EventArgs e) 273 { 274 string help = @"C:Users狗狗MickeyDesktophelp.txt"; 275 Help.ShowHelp(this, help); 276 } 277 278 279 private void 关于记事本_Click(object sender, EventArgs e) 280 { 281 Form about = new Form2(); 282 about.Show(); 283 } 284 private void inputInfo_TextChanged(object sender, EventArgs e) 285 { 286 string num = inputInfo.TextLength.ToString(); 287 toolStripStatusLabel2.Text = num; 288 289 if (inputInfo.Text != "") 290 { 291 撤销UToolStripMenuItem.Enabled = true; 292 剪切TToolStripMenuItem.Enabled = true; 293 复制CToolStripMenuItem.Enabled = true; 294 删除DToolStripMenuItem.Enabled = true; 295 全选AToolStripMenuItem.Enabled = true; 296 } 297 else 298 { 299 撤销UToolStripMenuItem.Enabled = false; 300 剪切TToolStripMenuItem.Enabled = false; 301 复制CToolStripMenuItem.Enabled = false; 302 删除DToolStripMenuItem.Enabled = false; 303 全选AToolStripMenuItem.Enabled = false; 304 } 305 306 //if (inputInfo.) 307 //{ 308 // 粘贴PToolStripMenuItem.Enabled = true; 309 //} 310 311 } 312 private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e) 313 { 314 this.inputInfo.Undo(); 315 } 316 317 private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e) 318 { 319 this.inputInfo.Cut(); 320 321 } 322 323 private void 复制ToolStripMenuItem_Click(object sender, EventArgs e) 324 { 325 this.inputInfo.Copy(); 326 } 327 private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e) 328 { 329 this.inputInfo.Paste(); 330 } 331 private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) 332 { 333 this.inputInfo.SelectedText = ""; 334 } 335 private void 全选ToolStripMenuItem_Click(object sender, EventArgs e) 336 { 337 this.inputInfo.SelectAll(); 338 } 339 }