简单记事本的代码?
各位高手帮帮忙!!!需要一个简单记事本里的代码!!!就是电脑自带的记事本!!!
Dim BlnEdited As Boolean = False '定义一个逻辑变量查看文本是否被编辑过
Dim StrFilePath As String '定义一个字符串变量存放文件路径
Dim BlnSaved As Boolean = False '定义一个逻辑变量查看文本是否被保存过
Private TargetPosition As Integer
Dim sFind As String
Private Sub FindText(ByVal start_at As Integer)
Dim pos As Integer
'Dim target As Integer
pos = InStr(start_at, RichTextBox1.Text, sFind) '获取用户输入要查的字符串的位置
If pos > 0 Then '找到了匹配的字符串
TargetPosition = pos
RichTextBox1.SelectionStart = TargetPosition - 1 '找到的字符串的第一个位置
RichTextBox1.SelectionLength = Len(sFind) '选中找到的字符串
'text1.SetFocus()
Else '没有找到匹配的字符串
MsgBox("没找到!")
'text1.SetFocus()
sFind = InputBox("请输入要查找的字·词: ", "查找内容", sFind)
End If
End Sub
'编写FileSave()函数来实现保存文件操作
Private Sub FileSave() '定义一个没有返回值的函数
FileOpen(1, StrFilePath, OpenMode.Output) '打开文件,并定义文件号为1
PrintLine(1, RichTextBox1.Text) '用PrintLine()函数将文本内容写入文件
FileClose(1) '关闭文件
BlnEdited = False '保存完文件,设置文件为未改动状态
BlnSaved = True '表明文件已经保存过
End Sub
'编写SaveFile()函数来保存文件
Private Function SaveFile() As Boolean '定义一个返回值为逻辑类型的函数
If BlnSaved = True Then
FileSave() '调用FileSave()函数保存文件
Return True
Else '如果文件没有被保存过,则表明还没有文件路径
'设置文件类型
SaveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Return False '用户取消保存,则返回函数值为False
Exit Function '跳出函数
Else
StrFilePath = SaveFileDialog1.FileName
FileSave() '保存文件
Return True
End If
End If
End Function
'编写FormClose()函数来进行关闭当前文件时的操作
Private Function FormClose() As Boolean
Dim StrClick As String '定义变量用来返回MessageBox的返回值
If BlnEdited = False Then
Return True '如果文件没有被修改,则返回True
Else
StrClick = MessageBox.Show("文件已经改变,是否保存?", "", MessageBoxButtons.YesNoCancel)
If StrClick = DialogResult.Yes Then
If SaveFile() = True Then
Return True '如果用户将文件保存了,则返回True
Exit Function
Else
Return False '如果用户没有保存文件,则返回False
End If
ElseIf StrClick = DialogResult.No Then
Return True '如果用户不保存文件,则返回True
Else
Return False '如果用户取消操作,则返回False
End If
End If
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'If FormClose() = False Then
mnucut.Enabled = False
mnucopy.Enabled = False
剪切TToolStripMenuItem.Enabled = False
复制CToolStripMenuItem.Enabled = False
删除DToolStripMenuItem.Enabled = False
撤消UToolStripMenuItem.Enabled = False
重做RToolStripMenuItem.Enabled = False
红色RToolStripMenuItem.Enabled = False
绿色GToolStripMenuItem.Enabled = False
蓝色BToolStripMenuItem.Enabled = False
黑色ToolStripMenuItem.Enabled = False
'End If
End Sub
Private Sub 新建NToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 新建NToolStripMenuItem.Click
If FormClose() = False Then
Exit Sub '如果函数返回值为False,表明用户取消操作,则跳出该操作
Else '表明可以新建文件了
BlnEdited = False
RichTextBox1.Text = "" '新建一个文件,并把文件设为未修改和无保存路径状态
BlnEdited = False
End If
End Sub
Private Sub 打开OToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开OToolStripMenuItem.Click
Dim StrOpentext As String = ""
If FormClose() = False Then '调用FormClose()函数进行判断
Exit Sub
Else
OpenFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
Exit Sub
Else
RichTextBox1.Text = "" '先把文本框中的文本清空
StrFilePath = OpenFileDialog1.FileName
FileOpen(1, StrFilePath, OpenMode.Binary)
While Not EOF(1)
StrOpentext = StrOpentext + InputString(1, 1) '用while语句读入文件内容
End While
'注意,对于InputString函数,文件只能用Input或Binary方式打开
FileClose(1)
RichTextBox1.Text = StrOpentext
BlnSaved = True '文件已有路径
BlnEdited = False
End If
End If
End Sub
Private Sub 保存SToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存SToolStripMenuItem.Click
SaveFile()
'With SaveFileDialog1
' .Filter = "rtf文件|*.rtf|txt文件(*.txt)|*.txt"
' .Title = "输入要保存的文件"
' .DefaultExt = "rtf"
' If .ShowDialog = Windows.Forms.DialogResult.OK Then
' RichTextBox1.SaveFile(.FileName)
' Me.Text = .FileName
' End If
'end with
End Sub
Private Sub 另存为AToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 另存为AToolStripMenuItem.Click
BlnEdited = False
SaveFile()
End Sub
Private Sub 退出XToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出XToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub 撤消UToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 撤消UToolStripMenuItem.Click
RichTextBox1.Undo()
重做RToolStripMenuItem.Enabled = True
End Sub
Private Sub 重做RToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 重做RToolStripMenuItem.Click
RichTextBox1.Redo()
End Sub
Private Sub 剪切TToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 剪切TToolStripMenuItem.Click
RichTextBox1.Cut()
End Sub
Private Sub 复制CToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 复制CToolStripMenuItem.Click
RichTextBox1.Copy()
粘贴PToolStripMenuItem.Enabled = True
End Sub
Private Sub 粘贴PToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 粘贴PToolStripMenuItem.Click
RichTextBox1.Paste()
End Sub
Private Sub 删除DToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除DToolStripMenuItem.Click
'RichTextBox1.Text = ""
RichTextBox1.SelectedText = ""
End Sub
Private Sub 全选AToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 全选AToolStripMenuItem.Click
RichTextBox1.SelectAll()
End Sub
Private Sub 插入时间DToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 插入时间DToolStripMenuItem.Click
RichTextBox1.SelectedText = CStr(Now())
End Sub
Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged
If RichTextBox1.SelectedText <> "" Then
mnucut.Enabled = True
mnucopy.Enabled = True
剪切TToolStripMenuItem.Enabled = True
复制CToolStripMenuItem.Enabled = True
删除DToolStripMenuItem.Enabled = True
红色RToolStripMenuItem.Enabled = True
绿色GToolStripMenuItem.Enabled = True
蓝色BToolStripMenuItem.Enabled = True
黑色ToolStripMenuItem.Enabled = True
Else
mnucut.Enabled = False
mnucopy.Enabled = False
剪切TToolStripMenuItem.Enabled = False
复制CToolStripMenuItem.Enabled = False
删除DToolStripMenuItem.Enabled = False
红色RToolStripMenuItem.Enabled = False
绿色GToolStripMenuItem.Enabled = False
蓝色BToolStripMenuItem.Enabled = False
黑色ToolStripMenuItem.Enabled = False
End If
If RichTextBox1.Text <> "" Then
撤消UToolStripMenuItem.Enabled = True
'重做RToolStripMenuItem.Enabled = True
Else
撤消UToolStripMenuItem.Enabled = False
'重做RToolStripMenuItem.Enabled = False
End If
End Sub
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
BlnEdited = True
ToolStripStatusLabel1.Text = "文本长度" + CStr(RichTextBox1.Text.Length)
End Sub
Private Sub 格式OToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 格式OToolStripMenuItem.Click
End Sub
Private Sub 查找ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查找ToolStripMenuItem.Click
sFind = InputBox("请输入要查找的字·词:", "查找内容", sFind)
FindText(1)
End Sub
Private Sub 查找下一个NToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查找下一个NToolStripMenuItem.Click
FindText(TargetPosition + 1)
End Sub
Private Sub 颜色CToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 颜色CToolStripMenuItem.Click
ColorDialog1.ShowDialog()
RichTextBox1.ForeColor = ColorDialog1.Color
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.OK Then
RichTextBox1.Font = FontDialog1.Font
End If
End Sub
Private Sub New_toolbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles New_toolbtn.Click
If FormClose() = False Then
Exit Sub
Else
BlnEdited = False
RichTextBox1.Text = ""
BlnEdited = False
End If
End Sub
Private Sub Open_toolbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Open_toolbtn.Click
Dim StrOpentext As String = ""
If FormClose() = False Then
Exit Sub
Else
OpenFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
Exit Sub
Else
RichTextBox1.Text = ""
StrFilePath = OpenFileDialog1.FileName
FileOpen(1, StrFilePath, OpenMode.Binary)
While Not EOF(1)
StrOpentext = StrOpentext + InputString(1, 1)
End While
FileClose(1)
RichTextBox1.Text = StrOpentext
BlnSaved = True
BlnEdited = False
End If
End If
End Sub
Private Sub Save_toolbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save_toolbtn.Click
SaveFile()
End Sub
Private Sub Copy_toolbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Copy_toolbtn.Click
RichTextBox1.Copy()
End Sub
Private Sub Cut_toolbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cut_toolbtn.Click
RichTextBox1.Cut()
End Sub
Private Sub Past_toolbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Past_toolbtn.Click
RichTextBox1.Paste()
End Sub
Private Sub Undo_toolbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Undo_toolbtn.Click
RichTextBox1.Undo()
End Sub
Private Sub Find_toolbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Find_toolbtn.Click
sFind = InputBox("请输入要查找的字·词:", "查找内容", sFind)
FindText(1)
End Sub
Private Sub 红色RToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 红色RToolStripMenuItem.Click
RichTextBox1.SelectionColor = Color.Red
End Sub
Private Sub 绿色GToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 绿色GToolStripMenuItem.Click
RichTextBox1.SelectionColor = Color.Green
End Sub
Private Sub 蓝色BToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 蓝色BToolStripMenuItem.Click
RichTextBox1.SelectionColor = Color.Blue
End Sub
Private Sub 黑色ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 黑色ToolStripMenuItem.Click
RichTextBox1.SelectionColor = Color.Black
End Sub
Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As ) Handles ContextMenuStrip1.Opening
End Sub
Private Sub 文件FToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 文件FToolStripMenuItem.Click
End Sub
Private Sub 编辑EToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 编辑EToolStripMenuItem.Click
End Sub
Private Sub mnucut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnucut.Click
RichTextBox1.Cut()
mnuPaste.Enabled = True
剪切TToolStripMenuItem.Enabled = True
End Sub
Private Sub mnucopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnucopy.Click
RichTextBox1.Copy()
RichTextBox1.Enabled = True
粘贴PToolStripMenuItem.Enabled = True
End Sub
Private Sub mnuPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPaste.Click
RichTextBox1.Paste()
End Sub