⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 用VB.NET开发的记事本,功能强大 用VB.NET开发的记事本,功能强大
💻 VB
📖 第 1 页 / 共 2 页
字号:
            删除ToolStripMenuItem.Enabled = False
        End If
    End Sub

    Private Sub ToolStrip1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStrip1.MouseHover

        '控件何时可用
        If RichTextBox1.SelectedText <> "" Then
            '工具栏
            剪切UToolStripButton.Enabled = True
            复制CToolStripButton.Enabled = True
            删除ToolStripButton1.Enabled = True
        Else
            剪切UToolStripButton.Enabled = False
            复制CToolStripButton.Enabled = False
            删除ToolStripButton1.Enabled = False
        End If
    End Sub

    Private Sub 时间日期ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 时间日期ToolStripMenuItem.Click
        时间日期.Show()
    End Sub


    Private Sub 帮助ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 帮助ToolStripMenuItem1.Click
        MessageBox.Show("暂时未定!")
    End Sub
    Private Sub 音乐1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 音乐1ToolStripMenuItem.Click
        My.Computer.Audio.Play("背景1.wav", AudioPlayMode.Background)
    End Sub


    Private Sub 音乐2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 音乐2ToolStripMenuItem.Click
        My.Computer.Audio.Play("背景2.wav", AudioPlayMode.Background)
    End Sub
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If oldfilehandle() Then
            e.Cancel = False
        Else
            e.Cancel = True
        End If
    End Sub

    Private Sub form1_closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Application.Exit()
    End Sub






    '记事本查找

    '定义搜索的初始位置
    Dim chushiweizhi As Integer = 0

    '定义初始颜色位置
    Dim previouscolor As Color

    '查找
    Private Sub 开始_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 开始.Click
        chushiweizhi = 0
        Dim cz As String = chazhao.Text
        If cz.Length <= 0 Then
            MessageBox.Show("请输入要查的内容!")
            Return
        End If
        Dim pos As Integer
        pos = RichTextBox1.Find(cz, chushiweizhi, RichTextBoxFinds.MatchCase)
    End Sub


    '查找
    Private Sub 查找下_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查找下.Click

        Dim cz As String = chazhao.Text
        If cz.Length <= 0 Then
            MessageBox.Show("请输入要查的内容!")
            Return
        End If

        If chushiweizhi <> 0 Then
            RichTextBox1.SelectionColor = previouscolor
        End If

        If chushiweizhi + cz.Length > RichTextBox1.TextLength Then
            '消息框包含“确定”按钮(MessageBoxButtons.OK)    感叹号(MessageBoxIcon.Exclamation)
            MessageBox.Show("已经达文件尾, 是否从文件头开始搜索?", "查找", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

        '实现查找的代码
        Dim pos As Integer
        pos = RichTextBox1.Find(cz, chushiweizhi, RichTextBoxFinds.MatchCase)
        If pos >= 0 Then
            RichTextBox1.SelectionStart = pos
            RichTextBox1.SelectionLength = cz.Length()
            previouscolor = RichTextBox1.SelectionColor
            chushiweizhi = pos + cz.Length()
        Else
            MessageBox.Show("未找到指定的文本", "查找")
        End If
    End Sub


    '替换
    Private Sub 替换_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 替换.Click
        Dim cz As String = chazhao.Text
        If cz.Length <= 0 Then
            MessageBox.Show("请输入要查的内容!")
            Return
        End If

        If chushiweizhi <> 0 Then
            RichTextBox1.SelectionColor = RichTextBox1.SelectionColor
        End If

        If chushiweizhi + cz.Length > RichTextBox1.TextLength Then
            '消息框包含“确定”按钮(MessageBoxButtons.OK)    感叹号(MessageBoxIcon.Exclamation)
            MessageBox.Show("已经达文件尾, 是否从文件头开始搜索?", "查找", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

        '实现查找的代码
        Dim pos As Integer
        pos = RichTextBox1.Find(cz, chushiweizhi, RichTextBoxFinds.MatchCase)
        If pos >= 0 Then
            RichTextBox1.SelectionStart = pos
            RichTextBox1.SelectionLength = cz.Length()
            previouscolor = RichTextBox1.SelectionColor
            RichTextBox1.SelectedText = RichTextBox1.SelectedText.Replace(chazhao.Text, tihuan.Text)
            chushiweizhi = pos + cz.Length()
        Else
            MessageBox.Show("未找到指定的文本", "查找")
        End If
    End Sub

    Private Sub 全替换_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 全替换.Click
        Dim cz As String = chazhao.Text
        If cz.Length <= 0 Then
            MessageBox.Show("请输入要查的内容!")
            Return
        End If

        If chushiweizhi <> 0 Then
            RichTextBox1.SelectionColor = RichTextBox1.SelectionColor
        End If

        If chushiweizhi + cz.Length > RichTextBox1.TextLength Then
            '消息框包含“确定”按钮(MessageBoxButtons.OK)    感叹号(MessageBoxIcon.Exclamation)
            MessageBox.Show("已经达文件尾, 是否从文件头开始搜索?", "查找", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

        '实现查找的代码
        Dim pos As Integer
        pos = RichTextBox1.Find(cz, chushiweizhi, RichTextBoxFinds.MatchCase)
        If pos >= 0 Then
            RichTextBox1.SelectionStart = pos
            RichTextBox1.SelectionLength = cz.Length()
            previouscolor = RichTextBox1.SelectionColor
            RichTextBox1.Text = RichTextBox1.Text.Replace(chazhao.Text, tihuan.Text)
            chushiweizhi = pos + cz.Length()
        Else
            MessageBox.Show("未找到指定的文本", "查找")
        End If
    End Sub








    '记事本工具栏

    Private Sub 新建NToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 新建NToolStripButton.Click
        If oldfilehandle() Then
            RichTextBox1.Clear()
            mfilename = ""
        End If
    End Sub

    Private Sub 打开OToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开OToolStripButton.Click
        If oldfilehandle() Then
            readfile()
        End If
    End Sub

    Private Sub 保存SToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存SToolStripButton.Click
        If mfilename = "" Then
            saveasfile()
        Else
            writefile()
        End If
    End Sub

    Private Sub 打印PToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打印PToolStripButton.Click
        Dim dialog As New PrintDialog
        '设置打印文档
        dialog.Document = pdoc
        If dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            '打印文档
            pdoc.Print()
        End If
    End Sub

    Private Sub 打印预览ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打印预览ToolStripButton1.Click
        Dim ppd As New PrintPreviewDialog
        Try
            ppd.Document = pdoc
            ppd.ShowDialog()
        Catch ex As Exception
            MessageBox.Show("加载打印文档时出现异常,请确认打印机已连接", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub 复制CToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 复制CToolStripButton.Click
        RichTextBox1.Copy()
    End Sub

    Private Sub 剪切UToolStripButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles 剪切UToolStripButton.Click
        RichTextBox1.Cut()
    End Sub

    Private Sub 粘贴PToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 粘贴PToolStripButton.Click
        RichTextBox1.Paste()
    End Sub

    Private Sub 删除ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripButton1.Click
        RichTextBox1.SelectedText = ""
    End Sub

    Private Sub 重复ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 重复ToolStripButton2.Click
        RichTextBox1.Redo()
    End Sub

    Private Sub 撤销ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 撤销ToolStripButton3.Click
        RichTextBox1.Undo()
    End Sub


    Private Sub 查找ToolStripButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查找ToolStripButton1.Click
        ToolStrip2.Visible = Not ToolStrip2.Visible
    End Sub






    '记事本右键栏

    Private Sub 新建PToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 新建PToolStripMenuItem.Click
        If oldfilehandle() Then
            RichTextBox1.Clear()
            mfilename = ""
        End If
    End Sub

    Private Sub 撤销UToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 撤销UToolStripMenuItem.Click
        RichTextBox1.Undo()
    End Sub

    Private Sub 剪贴ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 剪贴ToolStripMenuItem1.Click
        RichTextBox1.Cut()
    End Sub

    Private Sub 复制ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 复制ToolStripMenuItem1.Click
        RichTextBox1.Copy()
    End Sub

    Private Sub 粘贴ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 粘贴ToolStripMenuItem1.Click
        RichTextBox1.Paste()
    End Sub

    Private Sub 删除ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem1.Click
        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 ContextMenuStrip1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContextMenuStrip1.MouseHover

        '控件何时可用
        If RichTextBox1.Text = "" Then
            '右键
            全选AToolStripMenuItem.Enabled = False
        Else
            全选AToolStripMenuItem.Enabled = True
        End If

        If RichTextBox1.SelectedText <> "" Then
            '右键
            剪贴ToolStripMenuItem1.Enabled = True
            复制ToolStripMenuItem1.Enabled = True
            删除ToolStripMenuItem1.Enabled = True
        Else
            剪贴ToolStripMenuItem1.Enabled = False
            复制ToolStripMenuItem1.Enabled = False
            删除ToolStripMenuItem1.Enabled = False
        End If
    End Sub

    Private Sub 退出XToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出XToolStripMenuItem.Click
        Me.Close()
    End Sub


    '    '关联TXT文件,通过WritteRgedit1—WritteRgedit2的修改注册表实现------------------'---网上找的资料
    '    Private Sub 关联ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 关联ToolStripMenuItem.Click

    '        If 关联ToolStripMenuItem.Checked = True Then
    '            关联ToolStripMenuItem.Checked = False
    '        Else
    '            关联ToolStripMenuItem.Checked = True
    '        End If
    '        '关联ToolStripMenuItem.Checked = Not 关联ToolStripMenuItem.Checked
    '        Dim regbiao As Boolean
    '        regbiao = 关联ToolStripMenuItem.Checked
    '        Call WritteRgedit1(regbiao)
    '        Call WritteRgedit2(regbiao)
    '    End Sub

    '    Public Function WritteRgedit1(ByVal flag As Boolean) As Boolean
    '        Dim Reg As Microsoft.Win32.RegistryKey
    '        Try
    '            Reg = Registry.ClassesRoot.CreateSubKey("txtfile\shell\open\command")
    '            If flag = True Then
    '                Reg.SetValue("", Application.ExecutablePath & " %1")
    '            Else
    '                Reg.SetValue("", "%SystemRoot%\system32\NOTEPAD.EXE %1")
    '            End If
    '            Return True
    '        Catch ex As Exception
    '            Return False
    '        End Try
    '    End Function

    '    Public Function WritteRgedit2(ByVal flag As Boolean) As Boolean
    '        Dim sty As String
    '        sty = Registry.Users.GetSubKeyNames(6)
    '        sty = Mid(sty, 1, Len(sty) - 8)
    '        Dim Reg As Microsoft.Win32.RegistryKey
    '        Try
    '            Reg = Registry.Users.CreateSubKey(sty & "\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt")
    '            If flag = True Then
    '                Dim a As String
    '                a = Mid(Application.ExecutablePath, Len(Application.StartupPath) + 2)
    '                Reg.SetValue("Application", a)
    '            Else
    '                Reg.SetValue("Application", "NOTEPAD.EXE")
    '            End If
    '            Return True
    '        Catch ex As Exception
    '            Return False
    '        End Try
    '    End Function
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -