VB.NET完成一个功能增强的记事本
http://lilisalo.iteye.com/blog/1223384
想必大家都听说过Notepad2,Notepad++一类的记事本增强软件,想自己也写一个吗?
今天教大家用用VB.NET写了一个功能增强的记事本(支持全文字数统计、选中部分字数统计、支持doc、rtf格式等)
感觉如果不错的话,看看源码吧:
PublicClassMainForm
DimFileNameAsString="无标题"
DimwordAsString=""
DimMLAsBoolean=False
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
Me.StartPosition=FormStartPosition.CenterScreen
Me.Text=FileName+"-"+"记事本——AngelHacker"
RichTextBox1.WordWrap=False
RichTextBox1.ContextMenuStrip=ContextMenuStrip1
RichTextBox1.ScrollBars=RichTextBoxScrollBars.Both
新建NToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.N
打开OToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.O
保存SToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.S
剪切TToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.X
复制CToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.C
粘贴PToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.V
全选AToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.A
删除LToolStripMenuItem.ShortcutKeys=Keys.Delete
查找FToolStripMenuItem.ShortcutKeys=Keys.Control+Keys.F
查找下一个NToolStripMenuItem.ShortcutKeys=Keys.F3
查找下一个NToolStripMenuItem.Enabled=False
EndSub
PrivateSub新建NToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles新建NToolStripMenuItem.Click
RichTextBox1.Text=""
FileName="无标题"
Me.Text=FileName+"-"+"记事本——AngelHacker"
EndSub
PrivateSub打开OToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles打开OToolStripMenuItem.Click
OpenFileDialog1.Filter="文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
OpenFileDialog1.FileName=""
IfOpenFileDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
ExitSub
Else
RichTextBox1.Text=""
FileName=OpenFileDialog1.FileName
SelectCaseOpenFileDialog1.FilterIndex
Case"1"
RichTextBox1.LoadFile(FileName,RichTextBoxStreamType.PlainText)
Case"2"
RichTextBox1.LoadFile(FileName,RichTextBoxStreamType.RichText)
CaseElse
OnErrorGoTotxt
RichTextBox1.LoadFile(FileName)
EndSelect
Me.Text=FileName+"-"+"记事本——AngelHacker"
EndIf
ExitSub
txt:
RichTextBox1.LoadFile(FileName,RichTextBoxStreamType.PlainText)
EndSub
PrivateSub保存SToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles保存SToolStripMenuItem.Click
IfMe.Text="无标题-记事本——AngelHacker"Then
SaveFileDialog1.Filter="文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
SaveFileDialog1.FileName=""
SaveFileDialog1.CheckPathExists=True
IfSaveFileDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
ExitSub
Else
SelectCaseSaveFileDialog1.FilterIndex
Case"1"
RichTextBox1.SaveFile(SaveFileDialog1.FileName,RichTextBoxStreamType.PlainText)
Case"2"
RichTextBox1.SaveFile(SaveFileDialog1.FileName,RichTextBoxStreamType.RichText)
CaseElse
RichTextBox1.SaveFile(SaveFileDialog1.FileName)
EndSelect
Me.Text=SaveFileDialog1.FileName+"-"+"记事本——AngelHacker"
EndIf
Else
SelectCaseMid(Me.Text,Me.Text.Length-21,3)
Case"txt"
RichTextBox1.SaveFile(Mid(Me.Text,1,Me.Text.Length-18),RichTextBoxStreamType.PlainText)
Case"rtf"
RichTextBox1.SaveFile(Mid(Me.Text,1,Me.Text.Length-18),RichTextBoxStreamType.RichText)
CaseElse
RichTextBox1.SaveFile(Mid(Me.Text,1,Me.Text.Length-18))
EndSelect
EndIf
EndSub
PrivateSub另存为AToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles另存为AToolStripMenuItem.Click
SaveFileDialog1.Filter="文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
SaveFileDialog1.FileName=""
SaveFileDialog1.CheckPathExists=True
IfSaveFileDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
ExitSub
Else
SelectCaseSaveFileDialog1.FilterIndex
Case"1"
RichTextBox1.SaveFile(SaveFileDialog1.FileName,RichTextBoxStreamType.PlainText)
Case"2"
RichTextBox1.SaveFile(SaveFileDialog1.FileName,RichTextBoxStreamType.RichText)
CaseElse
RichTextBox1.SaveFile(SaveFileDialog1.FileName)
EndSelect
Me.Text=SaveFileDialog1.FileName+"-"+"记事本——AngelHacker"
EndIf
EndSub
PrivateSub退出XToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles退出XToolStripMenuItem.Click
End
EndSub
PrivateSub重置UToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles重置UToolStripMenuItem.Click,重置UToolStripMenuItem1.Click
RichTextBox1.Text=""
RichTextBox1.SelectionStart=1
word=""
查找下一个NToolStripMenuItem.Enabled=False
RichTextBox1.Font=Font
EndSub
PrivateSub剪切TToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles剪切TToolStripMenuItem.Click,剪切TToolStripMenuItem1.Click
RichTextBox1.Cut()
EndSub
PrivateSub复制CToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles复制CToolStripMenuItem.Click,复制CToolStripMenuItem1.Click
RichTextBox1.Copy()
EndSub
PrivateSub粘贴PToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles粘贴PToolStripMenuItem.Click,粘贴PToolStripMenuItem1.Click
RichTextBox1.Paste()
EndSub
PrivateSub全选AToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles全选AToolStripMenuItem.Click,全选AToolStripMenuItem1.Click
RichTextBox1.SelectAll()
EndSub
PrivateSub删除LToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles删除LToolStripMenuItem.Click,删除DToolStripMenuItem.Click
RichTextBox1.Text=RichTextBox1.Text.Remove(RichTextBox1.SelectionStart,RichTextBox1.SelectionLength)
EndSub
PrivateSub字数统计ToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles字数统计ToolStripMenuItem.Click,字数统计SToolStripMenuItem.Click
IfRichTextBox1.SelectedText=""Then
MsgBox("全文共"+RichTextBox1.TextLength.ToString+"字符")
Else
MsgBox("选中的字符数为"+RichTextBox1.SelectionLength.ToString)
EndIf
EndSub
PrivateSub自动换行WToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles自动换行WToolStripMenuItem.Click
IfML=FalseThen
RichTextBox1.WordWrap=True
自动换行WToolStripMenuItem.Checked=True
ML=True
Else
RichTextBox1.WordWrap=False
自动换行WToolStripMenuItem.Checked=False
ML=False
EndIf
EndSub
PrivateSub关于AToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles关于AToolStripMenuItem.Click
AboutBox1.ShowDialog()
EndSub
PrivateSub字体FToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles字体FToolStripMenuItem.Click
IfFontDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
ExitSub
Else
RichTextBox1.Font=FontDialog1.Font
EndIf
EndSub
PrivateSub查找FToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles查找FToolStripMenuItem.Click
word=InputBox("查找内容","查找")
Ifword=""Then
ExitSub
Else
DimaAsInteger=RichTextBox1.Find(word,RichTextBox1.SelectionStart,RichTextBoxFinds.MatchCase)
Ifa=-1Then
MsgBox("找不到"+""""+word+"""")
Else
查找下一个NToolStripMenuItem.Enabled=True
ExitSub
EndIf
EndIf
EndSub
PrivateSub查找下一个NToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles查找下一个NToolStripMenuItem.Click
DimwzAsInteger
IfRichTextBox1.SelectionStart=RichTextBox1.TextLengthThen
MsgBox("找不到"+""""+word+"""")
Else
wz=RichTextBox1.SelectionStart
wz+=1
DimaAsInteger=RichTextBox1.Find(word,wz,RichTextBoxFinds.MatchCase)
Ifa=-1Then
MsgBox("找不到"+""""+word+"""")
Else
ExitSub
EndIf
EndIf
EndSub
PrivateSub时间日期DToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles时间日期DToolStripMenuItem.Click
IfML=FalseThen
RichTextBox1.Text+=Date.Now
Else
RichTextBox1.Text+=Chr(13)&Chr(10)&Date.Now
EndIf
EndSub
EndClass
Public Class MainForm
Dim FileName As String = "无标题"
Dim word As String = ""
Dim ML As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.StartPosition = FormStartPosition.CenterScreen
Me.Text = FileName + " - " + "记事本——AngelHacker"
RichTextBox1.WordWrap = False
RichTextBox1.ContextMenuStrip = ContextMenuStrip1
RichTextBox1.ScrollBars = RichTextBoxScrollBars.Both
新建NToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.N
打开OToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.O
保存SToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.S
剪切TToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.X
复制CToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.C
粘贴PToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.V
全选AToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.A
删除LToolStripMenuItem.ShortcutKeys = Keys.Delete
查找FToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.F
查找下一个NToolStripMenuItem.ShortcutKeys = Keys.F3
查找下一个NToolStripMenuItem.Enabled = False
End Sub
Private Sub 新建NToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 新建NToolStripMenuItem.Click
RichTextBox1.Text = ""
FileName = "无标题"
Me.Text = FileName + " - " + "记事本——AngelHacker"
End Sub
Private Sub 打开OToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开OToolStripMenuItem.Click
OpenFileDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
OpenFileDialog1.FileName = ""
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Exit Sub
Else
RichTextBox1.Text = ""
FileName = OpenFileDialog1.FileName
Select Case OpenFileDialog1.FilterIndex
Case "1"
RichTextBox1.LoadFile(FileName, RichTextBoxStreamType.PlainText)
Case "2"
RichTextBox1.LoadFile(FileName, RichTextBoxStreamType.RichText)
Case Else
On Error GoTo txt
RichTextBox1.LoadFile(FileName)
End Select
Me.Text = FileName + " - " + "记事本——AngelHacker"
End If
Exit Sub
txt:
RichTextBox1.LoadFile(FileName, RichTextBoxStreamType.PlainText)
End Sub
Private Sub 保存SToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存SToolStripMenuItem.Click
If Me.Text = "无标题 - 记事本——AngelHacker" Then
SaveFileDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
SaveFileDialog1.FileName = ""
SaveFileDialog1.CheckPathExists = True
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Exit Sub
Else
Select Case SaveFileDialog1.FilterIndex
Case "1"
RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
Case "2"
RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.RichText)
Case Else
RichTextBox1.SaveFile(SaveFileDialog1.FileName)
End Select
Me.Text = SaveFileDialog1.FileName + " - " + "记事本——AngelHacker"
End If
Else
Select Case Mid(Me.Text, Me.Text.Length - 21, 3)
Case "txt"
RichTextBox1.SaveFile(Mid(Me.Text, 1, Me.Text.Length - 18), RichTextBoxStreamType.PlainText)
Case "rtf"
RichTextBox1.SaveFile(Mid(Me.Text, 1, Me.Text.Length - 18), RichTextBoxStreamType.RichText)
Case Else
RichTextBox1.SaveFile(Mid(Me.Text, 1, Me.Text.Length - 18))
End Select
End If
End Sub
Private Sub 另存为AToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 另存为AToolStripMenuItem.Click
SaveFileDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
SaveFileDialog1.FileName = ""
SaveFileDialog1.CheckPathExists = True
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Exit Sub
Else
Select Case SaveFileDialog1.FilterIndex
Case "1"
RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
Case "2"
RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.RichText)
Case Else
RichTextBox1.SaveFile(SaveFileDialog1.FileName)
End Select
Me.Text = SaveFileDialog1.FileName + " - " + "记事本——AngelHacker"
End If
End Sub
Private Sub 退出XToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出XToolStripMenuItem.Click
End
End Sub
Private Sub 重置UToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 重置UToolStripMenuItem.Click, 重置UToolStripMenuItem1.Click
RichTextBox1.Text = ""
RichTextBox1.SelectionStart = 1
word = ""
查找下一个NToolStripMenuItem.Enabled = False
RichTextBox1.Font = Font
End Sub
Private Sub 剪切TToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 剪切TToolStripMenuItem.Click, 剪切TToolStripMenuItem1.Click
RichTextBox1.Cut()
End Sub
Private Sub 复制CToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 复制CToolStripMenuItem.Click, 复制CToolStripMenuItem1.Click
RichTextBox1.Copy()
End Sub
Private Sub 粘贴PToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 粘贴PToolStripMenuItem.Click, 粘贴PToolStripMenuItem1.Click
RichTextBox1.Paste()
End Sub
Private Sub 全选AToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 全选AToolStripMenuItem.Click, 全选AToolStripMenuItem1.Click
RichTextBox1.SelectAll()
End Sub
Private Sub 删除LToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除LToolStripMenuItem.Click, 删除DToolStripMenuItem.Click
RichTextBox1.Text = RichTextBox1.Text.Remove(RichTextBox1.SelectionStart, RichTextBox1.SelectionLength)
End Sub
Private Sub 字数统计ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 字数统计ToolStripMenuItem.Click, 字数统计SToolStripMenuItem.Click
If RichTextBox1.SelectedText = "" Then
MsgBox("全文共" + RichTextBox1.TextLength.ToString + "字符")
Else
MsgBox("选中的字符数为" + RichTextBox1.SelectionLength.ToString)
End If
End Sub
Private Sub 自动换行WToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 自动换行WToolStripMenuItem.Click
If ML = False Then
RichTextBox1.WordWrap = True
自动换行WToolStripMenuItem.Checked = True
ML = True
Else
RichTextBox1.WordWrap = False
自动换行WToolStripMenuItem.Checked = False
ML = False
End If
End Sub
Private Sub 关于AToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 关于AToolStripMenuItem.Click
AboutBox1.ShowDialog()
End Sub
Private Sub 字体FToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 字体FToolStripMenuItem.Click
If FontDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Exit Sub
Else
RichTextBox1.Font = FontDialog1.Font
End If
End Sub
Private Sub 查找FToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查找FToolStripMenuItem.Click
word = InputBox("查找内容", "查找")
If word = "" Then
Exit Sub
Else
Dim a As Integer = RichTextBox1.Find(word, RichTextBox1.SelectionStart, RichTextBoxFinds.MatchCase)
If a = -1 Then
MsgBox("找不到" + """" + word + """")
Else
查找下一个NToolStripMenuItem.Enabled = True
Exit Sub
End If
End If
End Sub
Private Sub 查找下一个NToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查找下一个NToolStripMenuItem.Click
Dim wz As Integer
If RichTextBox1.SelectionStart = RichTextBox1.TextLength Then
MsgBox("找不到" + """" + word + """")
Else
wz = RichTextBox1.SelectionStart
wz += 1
Dim a As Integer = RichTextBox1.Find(word, wz, RichTextBoxFinds.MatchCase)
If a = -1 Then
MsgBox("找不到" + """" + word + """")
Else
Exit Sub
End If
End If
End Sub
Private Sub 时间日期DToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 时间日期DToolStripMenuItem.Click
If ML = False Then
RichTextBox1.Text += Date.Now
Else
RichTextBox1.Text += Chr(13) & Chr(10) & Date.Now
End If
End Sub
End Class
基本上没用什么属性设置,都是靠代码实现的,由于功能十分简单,使用的是面向过程的方法实现,看起来简单点。
微软的记事本其实很简单,给大家开阔下思路。
需要完整源码的可以到这里下载:http://download.csdn.net/source/3412684
http://wenku.baidu.com/view/8dadb7ea998fcc22bcd10d8a.html
欢迎转载:http://blog.csdn.com/AngelHacker/
http://blog.csdn.net/angelhacker/article/details/6580425