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

📄 findform.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Option Strict On
Public Class FindForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents bttnFind As System.Windows.Forms.Button
    Friend WithEvents bttnFindNext As System.Windows.Forms.Button
    Friend WithEvents bttnReplace As System.Windows.Forms.Button
    Friend WithEvents bttnReplaceAll As System.Windows.Forms.Button
    Friend WithEvents srchWord As System.Windows.Forms.TextBox
    Friend WithEvents chkCase As System.Windows.Forms.CheckBox
    Friend WithEvents replaceWord As System.Windows.Forms.TextBox

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.replaceWord = New System.Windows.Forms.TextBox()
        Me.bttnReplace = New System.Windows.Forms.Button()
        Me.bttnFind = New System.Windows.Forms.Button()
        Me.bttnReplaceAll = New System.Windows.Forms.Button()
        Me.chkCase = New System.Windows.Forms.CheckBox()
        Me.srchWord = New System.Windows.Forms.TextBox()
        Me.bttnFindNext = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'replaceWord
        '
        Me.replaceWord.Location = New System.Drawing.Point(8, 40)
        Me.replaceWord.Name = "replaceWord"
        Me.replaceWord.Size = New System.Drawing.Size(280, 20)
        Me.replaceWord.TabIndex = 1
        Me.replaceWord.Text = ""
        '
        'bttnReplace
        '
        Me.bttnReplace.Enabled = False
        Me.bttnReplace.Location = New System.Drawing.Point(296, 40)
        Me.bttnReplace.Name = "bttnReplace"
        Me.bttnReplace.TabIndex = 3
        Me.bttnReplace.Text = "Replace"
        '
        'bttnFind
        '
        Me.bttnFind.Location = New System.Drawing.Point(296, 8)
        Me.bttnFind.Name = "bttnFind"
        Me.bttnFind.TabIndex = 2
        Me.bttnFind.Text = "Find"
        '
        'bttnReplaceAll
        '
        Me.bttnReplaceAll.Enabled = False
        Me.bttnReplaceAll.Location = New System.Drawing.Point(384, 40)
        Me.bttnReplaceAll.Name = "bttnReplaceAll"
        Me.bttnReplaceAll.TabIndex = 5
        Me.bttnReplaceAll.Text = "Replace All"
        '
        'chkCase
        '
        Me.chkCase.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.chkCase.Location = New System.Drawing.Point(8, 72)
        Me.chkCase.Name = "chkCase"
        Me.chkCase.Size = New System.Drawing.Size(168, 16)
        Me.chkCase.TabIndex = 6
        Me.chkCase.Text = "Case-sensitive search"
        '
        'srchWord
        '
        Me.srchWord.Location = New System.Drawing.Point(8, 8)
        Me.srchWord.Name = "srchWord"
        Me.srchWord.Size = New System.Drawing.Size(280, 20)
        Me.srchWord.TabIndex = 0
        Me.srchWord.Text = ""
        '
        'bttnFindNext
        '
        Me.bttnFindNext.Enabled = False
        Me.bttnFindNext.Location = New System.Drawing.Point(384, 8)
        Me.bttnFindNext.Name = "bttnFindNext"
        Me.bttnFindNext.TabIndex = 4
        Me.bttnFindNext.Text = "Find Next"
        '
        'FindForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 109)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.replaceWord, Me.chkCase, Me.bttnReplaceAll, Me.bttnReplace, Me.bttnFindNext, Me.bttnFind, Me.srchWord})
        Me.Name = "FindForm"
        Me.Text = "Find & Replace"
        Me.TopMost = True
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub bttnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnFind.Click
        Dim selStart As Integer
        Dim srchMode As CompareMethod

        srchMode = SetSearchMode()
        selStart = InStr(MDIForm.activeChildForm.Editor.Text, srchWord.Text, srchMode)
        If selStart = 0 Then
            MsgBox("Can't find word")
            Exit Sub
        End If

        MDIForm.activeChildForm.Editor.Select(selStart - 1, srchWord.Text.Length)
        MDIForm.activeChildForm.Editor.ScrollToCaret()

        bttnFindNext.Enabled = True
        bttnReplace.Enabled = True
        bttnReplaceAll.Enabled = True
    End Sub

    Private Sub bttnFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnFindNext.Click
        Dim selStart As Integer
        Dim srchMode As CompareMethod
        Dim srchStart As Integer
        srchMode = SetSearchMode()

        srchStart = MDIForm.activeChildForm.Editor.SelectionStart + MDIForm.activeChildForm.Editor.SelectionLength
        selStart = InStr(srchStart + 1, MDIForm.activeChildForm.Editor.Text, srchWord.Text, srchMode)
        If selStart = 0 Then
            MsgBox("There are no more instances of the specified word")
            bttnFindNext.Enabled = False
            Exit Sub
        End If

        MDIForm.activeChildForm.Editor.Select(selStart - 1, srchWord.Text.Length)
        MDIForm.activeChildForm.Editor.ScrollToCaret()

        bttnFindNext.Enabled = True
        bttnReplace.Enabled = True
        bttnReplaceAll.Enabled = True
    End Sub

    Private Sub bttnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnReplace.Click
        MDIForm.activeChildForm.Editor.SelectedText = replaceWord.Text
        bttnFindNext_Click(sender, e)
    End Sub

    Private Sub bttnReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnReplaceAll.Click
        Dim srchMode As CompareMethod
        Dim srchStart As Integer
        srchMode = SetSearchMode()
        Dim pointerLocation As Integer = MDIForm.activeChildForm.Editor.SelectionStart
        MDIForm.activeChildForm.Editor.Text = Replace(MDIForm.activeChildForm.Editor.Text, srchWord.Text, replaceWord.Text, , , srchMode)
        MDIForm.activeChildForm.Editor.SelectionStart = pointerLocation
    End Sub

    Function SetSearchMode() As CompareMethod
        If chkCase.Checked = True Then
            Return CompareMethod.Binary
        Else
            Return CompareMethod.Text
        End If
    End Function

    Private Sub FindForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        e.Cancel = True
        Me.Hide()
    End Sub

    Private Sub FindForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        srchWord.Focus()
    End Sub
End Class

⌨️ 快捷键说明

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