近一周寫的關於記事本的代碼,高仿記事本。本人C#入門不久,其中存在代碼冗余,但懶得修改了。
經測試運行正常。
一、主窗體設計及代碼

1 namespace BestEditor 2 { 3 public partial class Main : Form 4 { 5 private bool isTextChanged; 6 private string path;//記錄文件路徑(剛新建的文件路徑為"",打開的文件路徑為原路徑) 7 8 public Main() 9 { 10 InitializeComponent(); 11 this.Text = "無標題 - 記事本"; 12 path = ""; 13 } 14 15 /// <summary> 16 /// 初始化窗體時調用 17 /// </summary> 18 /// <param name="sender"></param> 19 /// <param name="e"></param> 20 private void Main_Load(object sender, EventArgs e) 21 { 22 //初始化,撤銷、剪切、復制、刪除 不可用 23 撤消UToolStripMenuItem.Enabled = false; 24 剪切TToolStripMenuItem.Enabled = false; 25 復制CToolStripMenuItem.Enabled = false; 26 刪除LToolStripMenuItem.Enabled = false; 27 28 if (richTextBoxBoard.Equals("")) 29 { 30 查找FToolStripMenuItem.Enabled = false; 31 查找下一個NToolStripMenuItem.Enabled = false; 32 } 33 else 34 { 35 查找FToolStripMenuItem.Enabled = true; 36 查找下一個NToolStripMenuItem.Enabled = true; 37 } 38 39 if (Clipboard.ContainsText()) 40 粘貼PToolStripMenuItem.Enabled = true; 41 else 42 粘貼PToolStripMenuItem.Enabled = false; 43 44 toolStripStatusLabel2.Text = "第 1 行,第 1 列"; 45 } 46 47 private void 新建NToolStripMenuItem_Click(object sender, EventArgs e) 48 { 49 //如果輸入框文字發生變動 50 if (isTextChanged) 51 { 52 saveFileDialog1.FileName = "*.txt"; 53 DialogResult dr = MessageBox.Show("是否將更改保存到 " + this.Text + "?", "記事本", 54 MessageBoxButtons.YesNoCancel); 55 if (dr == DialogResult.Yes) 56 { 57 //獲取或設置指定要在 SaveFileDialog 中顯示的文件類型和說明的篩選器字符串 58 saveFileDialog1.Filter = @"文本文檔(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln"; 59 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 60 { 61 richTextBoxBoard.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); 62 richTextBoxBoard.Text = ""; 63 path = ""; 64 } 65 } 66 else if(dr == DialogResult.No) 67 { 68 richTextBoxBoard.Text = ""; 69 path = ""; 70 } 71 } 72 else 73 { 74 richTextBoxBoard.Text = ""; 75 this.Text = "無標題 - 記事本"; 76 path = ""; 77 } 78 } 79 80 /// <summary> 81 /// 輸入框發生變化時觸發 82 /// </summary> 83 /// <param name="sender"></param> 84 /// <param name="e"></param> 85 private void richTextBoxBoard_TextChanged(object sender, EventArgs e) 86 { 87 isTextChanged = true; 88 } 89 90 private void 打開OToolStripMenuItem_Click(object sender, EventArgs e) 91 { 92 if (isTextChanged) 93 { 94 saveFileDialog1.FileName = "*.txt"; 95 DialogResult dr = MessageBox.Show("是否將更改保存到 " + this.Text + "?", "記事本", 96 MessageBoxButtons.YesNoCancel); 97 if (dr == DialogResult.Yes) 98 { 99 //獲取或設置指定要在 SaveFileDialog 中顯示的文件類型和說明的篩選器字符串 100 saveFileDialog1.Filter = @"文本文檔(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln"; 101 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 102 { 103 richTextBoxBoard.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); 104 Text = saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.LastIndexOf("\\")+1)+ 105 " - 記事本"; 106 } 107 } 108 } 109 110 openFileDialog1.FileName = ""; 111 112 if (openFileDialog1.ShowDialog() == DialogResult.OK) 113 { 114 path = openFileDialog1.FileName; 115 Text = path.Substring(path.LastIndexOf("\\") + 1) + " - 記事本"; 116 Console.WriteLine("path={0}",path); 117 richTextBoxBoard.LoadFile(path, RichTextBoxStreamType.PlainText); 118 isTextChanged = false; 119 } 120 } 121 122 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) 123 { 124 if (!("".Equals(path))) 125 { 126 richTextBoxBoard.SaveFile(path, RichTextBoxStreamType.PlainText); 127 isTextChanged = false; 128 } 129 else 130 { 131 saveFileDialog1.Filter = @"文本文檔(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln"; 132 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 133 { 134 richTextBoxBoard.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); 135 path = saveFileDialog1.FileName; 136 this.Text = path.Substring(path.LastIndexOf("\\") + 1) + " - 記事本"; 137 isTextChanged = false; 138 } 139 } 140 } 141 142 private void 另存為AToolStripMenuItem_Click(object sender, EventArgs e) 143 { 144 saveFileDialog1.Filter = @"文本文檔(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln"; 145 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 146 { 147 richTextBoxBoard.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); 148 path = saveFileDialog1.FileName; 149 this.Text = path.Substring(path.LastIndexOf("\\") + 1) + " - 記事本"; 150 isTextChanged = false; 151 } 152 } 153 154 private void 頁面設置UToolStripMenuItem_Click(object sender, EventArgs e) 155 { 156 pageSetupDialog1.Document = printDocument1; 157 pageSetupDialog1.ShowDialog(); 158 } 159 160 private void 打印PToolStripMenuItem_Click(object sender, EventArgs e) 161 { 162 printDialog1.Document = printDocument1; 163 printDialog1.ShowDialog(); 164 } 165 166 private void 退出XToolStripMenuItem_Click(object sender, EventArgs e) 167 { 168 this.Close(); 169 } 170 171 private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e) 172 { 173 richTextBoxBoard.Undo(); 174 } 175 176 private void 編輯EToolStripMenuItem_Click(object sender, EventArgs e) 177 { 178 if (richTextBoxBoard.CanUndo) 179 撤消UToolStripMenuItem.Enabled = true; 180 181 if (richTextBoxBoard.SelectionLength > 0) 182 { 183 剪切TToolStripMenuItem.Enabled = true; 184 復制CToolStripMenuItem.Enabled = true; 185 刪除LToolStripMenuItem.Enabled = true; 186 } 187 else 188 { 189 剪切TToolStripMenuItem.Enabled = false; 190 復制CToolStripMenuItem.Enabled = false; 191 刪除LToolStripMenuItem.Enabled = false; 192 } 193 194 if (richTextBoxBoard.Equals("")) 195 { 196 查找FToolStripMenuItem.Enabled = false; 197 查找下一個NToolStripMenuItem.Enabled = false; 198 } 199 else 200 { 201 查找FToolStripMenuItem.Enabled = true; 202 查找下一個NToolStripMenuItem.Enabled = true; 203 } 204 205 if (Clipboard.ContainsText()) 206 粘貼PToolStripMenuItem.Enabled = true; 207 else 208 粘貼PToolStripMenuItem.Enabled = false; 209 } 210 211 private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e) 212 { 213 richTextBoxBoard.Cut(); 214 } 215 216 private void 復制CToolStripMenuItem_Click(object sender, EventArgs e) 217 { 218 richTextBoxBoard.Copy(); 219 } 220 221 private void 粘貼PToolStripMenuItem_Click(object sender, EventArgs e) 222 { 223 richTextBoxBoard.Paste(); 224 } 225 226 private void 刪除LToolStripMenuItem_Click(object sender, EventArgs e) 227 { 228 richTextBoxBoard.SelectedText = ""; 229 } 230 231 /// <summary> 232 /// 不同窗體之間通訊 233 /// </summary> 234 /// <param name="sender"></param> 235 /// <param name="e"></param> 236 private void 查找FToolStripMenuItem_Click(object sender, EventArgs e) 237 { 238 Search search = new Search(); 239 search.Owner = this; 240 search.Show(); 241 } 242 243 private void 查找下一個NToolStripMenuItem_Click(object sender, EventArgs e) 244 { 245 Search search = new Search(); 246 search.Owner = this; 247 search.Show(); 248 } 249 250 private void 替換RToolStripMenuItem_Click(object sender, EventArgs e) 251 { 252 Change change = new Change(); 253 change.Owner = this; 254 change.Show(); 255 } 256 257 private void 轉到GToolStripMenuItem_Click(object sender, EventArgs e) 258 { 259 Goto gt = new Goto(); 260 gt.Owner = this; 261 gt.Show(); 262 } 263 264 private void 全選AToolStripMenuItem_Click(object sender, EventArgs e) 265 { 266 richTextBoxBoard.SelectAll(); 267 } 268 269 private void 時間日期DToolStripMenuItem_Click(object sender, EventArgs e) 270 { 271 string front = richTextBoxBoard.Text.Substring(0, richTextBoxBoard.SelectionStart); 272 string back = richTextBoxBoard.Text.Substring(richTextBoxBoard.SelectionStart, 273 richTextBoxBoard.Text.Length - richTextBoxBoard.SelectionStart); 274 richTextBoxBoard.Text = front + DateTime.Now.ToString() + back; 275 } 276 277 private void 自動換行WToolStripMenuItem_Click(object sender, EventArgs e) 278 { 279 if (richTextBoxBoard.WordWrap) 280 { 281 自動換行WToolStripMenuItem.Checked = false; 282 richTextBoxBoard.WordWrap = false; 283 } 284 else 285 { 286 自動換行WToolStripMenuItem.Checked = true; 287 richTextBoxBoard.WordWrap = true; 288 } 289 } 290 291 private void 字體FToolStripMenuItem_Click(object sender, EventArgs e) 292 { 293 fontDialog1.ShowDialog(); 294 richTextBoxBoard.SelectionFont = fontDialog1.Font; 295 } 296 297 /// <summary> 298 /// 控制底部狀態欄顯示與否 299 /// </summary> 300 /// <param name="sender"></param> 301 /// <param name="e"></param> 302 private void 狀態欄SToolStripMenuItem_Click(object sender, EventArgs e) 303 { 304 if (狀態欄SToolStripMenuItem.Checked) 305 { 306 狀態欄SToolStripMenuItem.Checked = false; 307 statusStrip1.Visible = false; 308 } 309 else 310 { 311 狀態欄SToolStripMenuItem.Checked = true; 312 statusStrip1.Visible = true; 313 } 314 } 315 316 /// <summary> 317 /// 輸入框光標位置變化時觸發 318 /// </summary> 319 /// <param name="sender"></param> 320 /// <param name="e"></param> 321 private void richTextBoxBoard_SelectionChanged(object sender, EventArgs e) 322 { 323 string[] str = richTextBoxBoard.Text.Split('\r', '\n'); 324 int row = 1, column = 1, pos = richTextBoxBoard.SelectionStart; 325 326 foreach(string s in str) 327 Console.WriteLine(s); 328 Console.WriteLine("pos={0}",pos); 329 330 for (int i = 0; pos - str[i].Length > 0; i++) 331 { 332 pos = pos - str[i].Length - 1; 333 row = i + 2; 334 } 335 column = pos + 1; 336 toolStripStatusLabel2.Text = "第 " + row + " 行,第 " + column + " 列"; 337 } 338 339 private void 關於記事本AToolStripMenuItem_Click(object sender, EventArgs e) 340 { 341 AboutBox ab = new AboutBox(); 342 ab.Show(); 343 } 344 345 private void 查看幫助HToolStripMenuItem_Click(object sender, EventArgs e) 346 { 347 //調用系統內部的notepad.chm文件 348 } 349 350 /// <summary> 351 /// 點擊窗體右上角關閉按鈕觸發 352 /// </summary> 353 /// <param name="sender"></param> 354 /// <param name="e"></param> 355 private void Main_FormClosing(object sender, FormClosingEventArgs e) 356 { 357 if (isTextChanged) 358 { 359 if (!("".Equals(path))) 360 { 361 DialogResult dr = MessageBox.Show("是否將更改保存到"+path+"?","記事本", 362 MessageBoxButtons.YesNoCancel); 363 if (dr == DialogResult.Yes) 364 richTextBoxBoard.SaveFile(path, RichTextBoxStreamType.PlainText); 365 else if (dr == DialogResult.No) 366 e.Cancel = false; 367 else 368 e.Cancel = true;//不關閉 369 } 370 else 371 { 372 DialogResult dr = MessageBox.Show("是否將更改保存到 無標題?", "記事本", 373 MessageBoxButtons.YesNoCancel); 374 if (dr == DialogResult.Yes) 375 { 376 saveFileDialog1.Filter = @"文本文檔(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln"; 377 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 378 richTextBoxBoard.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); 379 else 380 e.Cancel = true; 381 } 382 else if (dr == DialogResult.No) 383 e.Cancel = false; 384 else 385 e.Cancel = true; 386 } 387 } 388 } 389 } 390 }
二、查找子窗體設計及代碼

1 namespace BestEditor 2 { 3 public partial class Search : Form 4 { 5 private RichTextBox rtb = new RichTextBox(); 6 7 public Search() 8 { 9 InitializeComponent(); 10 } 11 12 /// <summary> 13 /// 初始化時得到主窗口的通訊數據 14 /// </summary> 15 /// <param name="sender"></param> 16 /// <param name="e"></param> 17 private void Search_Load(object sender, EventArgs e) 18 { 19 Main main = (Main)this.Owner; 20 this.rtb = main.richTextBoxBoard; 21 } 22 23 /// <summary> 24 /// 取消 25 /// </summary> 26 /// <param name="sender"></param> 27 /// <param name="e"></param> 28 private void button2_Click(object sender, EventArgs e) 29 { 30 this.Close(); 31 } 32 33 private void button1_Click(object sender, EventArgs e) 34 { 35 string str = rtb.Text;//文件內容 36 string subSearch = textBox1.Text;//查找內容 37 string initString = subSearch; 38 int pos = rtb.SelectionStart;//光標位置 39 40 if (!checkBox1.Checked) 41 { 42 str = str.ToLower(); 43 subSearch = subSearch.ToLower(); 44 } 45 46 if (radioButton1.Checked)//向上查找 47 { 48 if (rtb.SelectionLength > 0) 49 pos = pos + rtb.SelectionLength - 1; 50 51 str = str.Substring(0, pos); 52 if (subSearch != "" && (pos = str.LastIndexOf(subSearch, pos)) != -1) 53 { 54 //輸入框得到焦點並選中查找的內容 55 rtb.Focus(); 56 rtb.SelectionStart = pos; 57 rtb.SelectionLength = subSearch.Length; 58 } 59 else 60 MessageBox.Show("找不到\"" + initString + "\"", "記事本", 61 MessageBoxButtons.OK, MessageBoxIcon.Information); 62 } 63 else 64 { 65 if (rtb.SelectionLength > 0) 66 pos = pos + 1; 67 if (subSearch != "" && (pos = str.IndexOf(subSearch, pos)) != -1) 68 { 69 rtb.Focus(); 70 rtb.SelectionStart = pos; 71 rtb.SelectionLength = subSearch.Length; 72 } 73 else 74 MessageBox.Show("找不到\"" + subSearch + "\"", "記事本", 75 MessageBoxButtons.OK, MessageBoxIcon.Information); 76 } 77 } 78 } 79 }
三、替換子窗體設計及其代碼

1 namespace BestEditor 2 { 3 public partial class Change : Form 4 { 5 private RichTextBox rtb = new RichTextBox(); 6 7 public Change() 8 { 9 InitializeComponent(); 10 } 11 12 private void Change_Load(object sender, EventArgs e) 13 { 14 Main main = (Main)this.Owner; 15 this.rtb = main.richTextBoxBoard; 16 button1.Enabled = false; 17 button2.Enabled = false; 18 button3.Enabled = false; 19 } 20 21 /// <summary> 22 /// 查找下一個 23 /// </summary> 24 /// <param name="sender"></param> 25 /// <param name="e"></param> 26 private void button1_Click(object sender, EventArgs e) 27 { 28 string str = rtb.Text; 29 string subSearch = textBox1.Text; 30 string initString = subSearch; 31 int pos = rtb.SelectionStart; 32 33 if (!checkBox1.Checked) 34 { 35 str = str.ToLower(); 36 subSearch = subSearch.ToLower(); 37 } 38 39 if (rtb.SelectionLength > 0) 40 pos = pos + 1; 41 if ((pos = str.IndexOf(subSearch, pos)) != -1) 42 { 43 rtb.Focus(); 44 rtb.SelectionStart = pos; 45 rtb.SelectionLength = subSearch.Length; 46 } 47 else 48 MessageBox.Show("找不到\"" + initString + "\"", "記事本", 49 MessageBoxButtons.OK, MessageBoxIcon.Information); 50 } 51 52 private void textBox1_TextChanged(object sender, EventArgs e) 53 { 54 bool flag = textBox1.Text != ""; 55 button1.Enabled = flag; 56 button2.Enabled = flag; 57 button3.Enabled = flag; 58 } 59 60 /// <summary> 61 /// 取消 62 /// </summary> 63 /// <param name="sender"></param> 64 /// <param name="e"></param> 65 private void button4_Click(object sender, EventArgs e) 66 { 67 this.Close(); 68 } 69 70 /// <summary> 71 /// 替換 72 /// </summary> 73 /// <param name="sender"></param> 74 /// <param name="e"></param> 75 private void button2_Click(object sender, EventArgs e) 76 { 77 string str = rtb.Text; 78 string subSearch = textBox1.Text; 79 string initString = subSearch; 80 string changeTo = textBox2.Text; 81 string front; 82 string dest; 83 string back; 84 int pos = rtb.SelectionStart; 85 86 if (!checkBox1.Checked) 87 { 88 str = str.ToLower(); 89 subSearch = subSearch.ToLower(); 90 } 91 92 if (rtb.SelectionLength > 0) 93 { 94 if (rtb.SelectedText.Equals(subSearch)) 95 { 96 //將文本框字符串分段,替換后再組合 97 front = rtb.Text.Substring(0,pos); 98 dest = changeTo; 99 back = rtb.Text.Substring(pos + subSearch.Length, rtb.Text.Length - pos - subSearch.Length); 100 rtb.Text = front + dest + back; 101 } 102 pos = pos + 1; 103 } 104 105 if ((pos = str.IndexOf(subSearch, pos)) != -1) 106 { 107 rtb.Focus(); 108 rtb.SelectionStart = pos; 109 rtb.SelectionLength = subSearch.Length; 110 } 111 else 112 MessageBox.Show("找不到\"" + initString + "\"", "記事本", 113 MessageBoxButtons.OK, MessageBoxIcon.Information); 114 } 115 116 /// <summary> 117 /// 全部替換 118 /// </summary> 119 /// <param name="sender"></param> 120 /// <param name="e"></param> 121 private void button3_Click(object sender, EventArgs e) 122 { 123 string str = rtb.Text; 124 string subSearch = textBox1.Text; 125 string changeTo = textBox2.Text; 126 string front; 127 string dest; 128 string back; 129 int pos = 0; 130 131 if (!checkBox1.Checked) 132 { 133 str = str.ToLower(); 134 subSearch = subSearch.ToLower(); 135 } 136 137 while ((pos=str.IndexOf(subSearch,pos))!=-1) 138 { 139 front = rtb.Text.Substring(0, pos); 140 dest = changeTo; 141 back = rtb.Text.Substring(pos + subSearch.Length, rtb.Text.Length - pos - subSearch.Length); 142 rtb.Text = front + dest + back; 143 if (!checkBox1.Checked) 144 str = rtb.Text.ToLower(); 145 } 146 } 147 } 148 }
四、轉到子窗體設計及其代碼

1 namespace BestEditor 2 { 3 public partial class Goto : Form 4 { 5 private RichTextBox rtb = new RichTextBox(); 6 7 public Goto() 8 { 9 InitializeComponent(); 10 } 11 12 private void Goto_Load(object sender, EventArgs e) 13 { 14 Main main = (Main)this.Owner; 15 this.rtb = main.richTextBoxBoard; 16 } 17 18 private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 19 { 20 if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13) 21 { 22 e.Handled = true; 23 MessageBox.Show("只能接收數字","記事本",MessageBoxButtons.OK,MessageBoxIcon.Error); 24 } 25 } 26 27 private void button2_Click(object sender, EventArgs e) 28 { 29 this.Close(); 30 } 31 32 /// <summary> 33 /// 確定 34 /// </summary> 35 /// <param name="sender"></param> 36 /// <param name="e"></param> 37 private void button1_Click(object sender, EventArgs e) 38 { 39 int row = int.Parse(textBox1.Text); 40 int pos = 0; 41 string[] str = rtb.Text.Split('\r', '\n'); 42 43 if (row < 1|| row > str.Length) 44 MessageBox.Show("行數超出范圍", "記事本 - 跳行", MessageBoxButtons.OK); 45 else 46 { 47 for (int i = 1; i < row; i++) 48 pos = pos + str[i-1].Length + 1; 49 this.Close(); 50 rtb.Focus(); 51 rtb.SelectionStart = pos; 52 } 53 } 54 } 55 }
五、“關於”對話框
直接插入“關於”對話框即可
代碼下載鏈接:
http://pan.baidu.com/s/1o6mJtSu
Ps:
編寫過程中有參考到以下代碼:http://blog.csdn.net/mathewsking/article/details/3645753,在此謝謝博主的思路啟發!