VB.NET完成一个功能增强的记事本

前端之家收集整理的这篇文章主要介绍了VB.NET完成一个功能增强的记事本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
原址:http://www.jb51.cc/article/p-eqvvftcw-bhk.html
想必大家都听说过Notepad2,Notepad++一类的记事本增强软件,想自己也写一个吗?

今天教大家用用VB.NET写了一个功能增强的记事本(支持文字统计、选中部分字数统计支持doc、rtf格式等)

先上图:


感觉如果不错的话,看看源码吧:

  1. PublicClassMainForm
  2. DimFileNameAsString="无标题"
  3. DimwordAsString=""
  4. DimMLAsBoolean=False
  5. PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
  6. Me.StartPosition=FormStartPosition.CenterScreen
  7. Me.Text=FileName+"-"+"记事本——AngelHacker"
  8. RichTextBox1.WordWrap=False
  9. RichTextBox1.ContextMenuStrip=ContextMenuStrip1
  10. RichTextBox1.ScrollBars=RichTextBoxScrollBars.Both
  11. 新建NToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.N
  12. 打开OToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.O
  13. 保存SToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.S
  14. 剪切TToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.X
  15. 复制CToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.C
  16. 粘贴PToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.V
  17. 全选AToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.A
  18. 删除LToolStripMenuItem.ShortcutKeys=Keys.Delete
  19. 查找FToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.F
  20. 查找下一个NToolStripMenuItem.ShortcutKeys=Keys.F3
  21. 查找下一个NToolStripMenuItem.Enabled=False
  22. EndSub
  23. PrivateSub新建NToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles新建NToolStripMenuItem.Click
  24. RichTextBox1.Text=""
  25. FileName="无标题"
  26. PrivateSub打开OToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles打开OToolStripMenuItem.Click
  27. OpenFileDialog1.Filter="文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
  28. OpenFileDialog1.FileName=""
  29. IfOpenFileDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
  30. ExitSub
  31. Else
  32. RichTextBox1.Text=""
  33. FileName=OpenFileDialog1.FileName
  34. SelectCaseOpenFileDialog1.FilterIndex
  35. Case"1"
  36. RichTextBox1.LoadFile(FileName,RichTextBoxStreamType.PlainText)
  37. Case"2"
  38. BoxStreamType.RichText)
  39. CaseElse
  40. OnErrorGoTotxt
  41. RichTextBox1.LoadFile(FileName)
  42. EndSelect
  43. EndIf
  44. ExitSub
  45. txt:
  46. RichTextBox1.LoadFile(FileName,RichTextBoxStreamType.PlainText)
  47. PrivateSub保存SToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles保存SToolStripMenuItem.Click
  48. IfMe.Text="无标题-记事本——AngelHacker"Then
  49. SaveFileDialog1.Filter="文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
  50. SaveFileDialog1.FileName=""
  51. SaveFileDialog1.CheckPathExists=True
  52. IfSaveFileDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
  53. SelectCaseSaveFileDialog1.FilterIndex
  54. RichTextBox1.SaveFile(SaveFileDialog1.FileName,108) 3px solid; padding-bottom:0px!important; line-height:18px; border-right-style:none; list-style-type:decimal-leading-zero; margin:0px; padding-left:10px!important; padding-right:3px!important; border-top-style:none; padding-top:0px!important"> RichTextBox1.SaveFile(SaveFileDialog1.FileName)
  55. EndSelect
  56. Me.Text=SaveFileDialog1.FileName+"-"+"记事本——AngelHacker"
  57. EndIf
  58. Else
  59. SelectCaseMid(Me.Text,Me.Text.Length-21,3)
  60. Case"txt"
  61. RichTextBox1.SaveFile(Mid(Me.Text,1,Me.Text.Length-18),RichTextBoxStreamType.PlainText)
  62. Case"rtf"
  63. BoxStreamType.RichText)
  64. CaseElse
  65. Me.Text.Length-18))
  66. PrivateSub另存为AToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles另存为AToolStripMenuItem.Click
  67. SaveFileDialog1.Filter="文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
  68. SaveFileDialog1.FileName=""
  69. SaveFileDialog1.CheckPathExists=True
  70. IfSaveFileDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
  71. SelectCaseSaveFileDialog1.FilterIndex
  72. Case"1"
  73. RichTextBox1.SaveFile(SaveFileDialog1.FileName,108) 3px solid; padding-bottom:0px!important; line-height:18px; border-right-style:none; list-style-type:decimal-leading-zero; margin:0px; padding-left:10px!important; padding-right:3px!important; border-top-style:none; padding-top:0px!important"> Case"2"
  74. BoxStreamType.RichText)
  75. RichTextBox1.SaveFile(SaveFileDialog1.FileName)
  76. Me.Text=SaveFileDialog1.FileName+"-"+"记事本——AngelHacker"
  77. EndSub
  78. PrivateSub退出XToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles退出XToolStripMenuItem.Click
  79. End
  80. PrivateSub重置UToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles重置UToolStripMenuItem.Click,重置UToolStripMenuItem1.Click
  81. RichTextBox1.SelectionStart=1
  82. word=""
  83. RichTextBox1.Font=Font
  84. PrivateSub剪切TToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles剪切TToolStripMenuItem.Click,剪切TToolStripMenuItem1.Click
  85. RichTextBox1.Cut()
  86. PrivateSub复制CToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles复制CToolStripMenuItem.Click,复制CToolStripMenuItem1.Click
  87. RichTextBox1.Copy()
  88. PrivateSub粘贴PToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles粘贴PToolStripMenuItem.Click,粘贴PToolStripMenuItem1.Click
  89. RichTextBox1.Paste()
  90. PrivateSub全选AToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles全选AToolStripMenuItem.Click,全选AToolStripMenuItem1.Click
  91. RichTextBox1.SelectAll()
  92. PrivateSub删除LToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles删除LToolStripMenuItem.Click,删除DToolStripMenuItem.Click
  93. RichTextBox1.Text=RichTextBox1.Text.Remove(RichTextBox1.SelectionStart,RichTextBox1.SelectionLength)
  94. PrivateSub字数统计ToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles字数统计ToolStripMenuItem.Click,字数统计SToolStripMenuItem.Click
  95. IfRichTextBox1.SelectedText=""Then
  96. MsgBox("全文共"+RichTextBox1.TextLength.ToString+"字符")
  97. MsgBox("选中的字符数为"+RichTextBox1.SelectionLength.ToString)
  98. PrivateSub自动换行WToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles自动换行WToolStripMenuItem.Click
  99. IfML=FalseThen
  100. RichTextBox1.WordWrap=True
  101. 自动换行WToolStripMenuItem.Checked=True
  102. ML=True
  103. RichTextBox1.WordWrap=False
  104. 自动换行WToolStripMenuItem.Checked=False
  105. ML=False
  106. PrivateSub关于AToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles关于AToolStripMenuItem.Click
  107. AboutBox1.ShowDialog()
  108. PrivateSub字体FToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles字体FToolStripMenuItem.Click
  109. IfFontDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
  110. RichTextBox1.Font=FontDialog1.Font
  111. PrivateSub查找FToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles查找FToolStripMenuItem.Click
  112. word=InputBox("查找内容","查找")
  113. Ifword=""Then
  114. DimaAsInteger=RichTextBox1.Find(word,RichTextBox1.SelectionStart,RichTextBoxFinds.MatchCase)
  115. Ifa=-1Then
  116. MsgBox("找不到"+""""+word+"""")
  117. 查找下一个NToolStripMenuItem.Enabled=True
  118. PrivateSub查找下一个NToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles查找下一个NToolStripMenuItem.Click
  119. DimwzAsInteger
  120. IfRichTextBox1.SelectionStart=RichTextBox1.TextLengthThen
  121. MsgBox("找不到"+""""+word+"""")
  122. wz=RichTextBox1.SelectionStart
  123. wz+=1
  124. DimaAsInteger=RichTextBox1.Find(word,wz,RichTextBoxFinds.MatchCase)
  125. Ifa=-1Then
  126. PrivateSub时间日期DToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles时间日期DToolStripMenuItem.Click
  127. IfML=FalseThen
  128. RichTextBox1.Text+=Date.Now
  129. RichTextBox1.Text+=Chr(13)&Chr(10)&Date.Now
  130. EndClass

基本上没用什么属性设置,都是靠代码实现的,由于功能十分简单,使用的是面向过程的方法实现,看起来简单点。

微软的记事本其实很简单,给大家开阔下思路。

需要完整源码的可以到这里下载:http://download.csdn.net/source/3412684

欢迎转载:http://blog.csdn.com/AngelHacker/

猜你在找的VB相关文章