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

📄 searchform.vb

📁 多文档编辑器程序
💻 VB
字号:
Public Class SearchForm
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    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

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents CloseBtn As System.Windows.Forms.Button
    Friend WithEvents CheckBox2 As System.Windows.Forms.CheckBox
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    Friend WithEvents SearchBtn As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents SearchTxt As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.CloseBtn = New System.Windows.Forms.Button()
        Me.CheckBox2 = New System.Windows.Forms.CheckBox()
        Me.CheckBox1 = New System.Windows.Forms.CheckBox()
        Me.SearchBtn = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.SearchTxt = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(240, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 23)
        Me.Button1.TabIndex = 13
        Me.Button1.Text = "从文件头开始搜索"
        '
        'CloseBtn
        '
        Me.CloseBtn.Location = New System.Drawing.Point(240, 64)
        Me.CloseBtn.Name = "CloseBtn"
        Me.CloseBtn.Size = New System.Drawing.Size(96, 23)
        Me.CloseBtn.TabIndex = 12
        Me.CloseBtn.Text = "关闭"
        '
        'CheckBox2
        '
        Me.CheckBox2.Location = New System.Drawing.Point(8, 64)
        Me.CheckBox2.Name = "CheckBox2"
        Me.CheckBox2.TabIndex = 11
        Me.CheckBox2.Text = "全字匹配(&W)"
        '
        'CheckBox1
        '
        Me.CheckBox1.Location = New System.Drawing.Point(8, 40)
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox1.Size = New System.Drawing.Size(120, 24)
        Me.CheckBox1.TabIndex = 10
        Me.CheckBox1.Text = "大小写匹配(&C)"
        '
        'SearchBtn
        '
        Me.SearchBtn.Location = New System.Drawing.Point(240, 0)
        Me.SearchBtn.Name = "SearchBtn"
        Me.SearchBtn.Size = New System.Drawing.Size(96, 23)
        Me.SearchBtn.TabIndex = 9
        Me.SearchBtn.Text = "查找下一个(&F)"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(56, 16)
        Me.Label1.TabIndex = 8
        Me.Label1.Text = "查找内容"
        '
        'SearchTxt
        '
        Me.SearchTxt.Location = New System.Drawing.Point(72, 8)
        Me.SearchTxt.Name = "SearchTxt"
        Me.SearchTxt.Size = New System.Drawing.Size(152, 21)
        Me.SearchTxt.TabIndex = 7
        Me.SearchTxt.Text = ""
        '
        'SearchForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(344, 93)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.CloseBtn, Me.CheckBox2, Me.CheckBox1, Me.SearchBtn, Me.Label1, Me.SearchTxt})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "SearchForm"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "查找"
        Me.ResumeLayout(False)

    End Sub

#End Region
    '定义搜索的初始位置
    Dim SearchPosition As Integer = 0
    Dim PreViousColor As Color

    Private Sub SearchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchBtn.Click
        Dim f As docForm = Me.Owner()
        Dim SearchStr As String = SearchTxt.Text
        If SearchStr.Length <= 0 Then
            MessageBox.Show("请输入要查询的内容!")
            Return
        End If
        If SearchPosition <> 0 Then
            f.M_RichTextBox.SelectionColor = PreViousColor
        End If

        Dim pos As Integer
        If SearchPosition + SearchStr.Length > f.M_RichTextBox.TextLength Then
            MessageBox.Show("已到达文件尾,是否从文件头开始搜索?", "查找", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
        '大小写匹配且全字匹配
        If CheckBox1.Checked And CheckBox2.Checked Then
            pos = f.M_RichTextBox.Find(SearchStr, SearchPosition, RichTextBoxFinds.MatchCase, RichTextBoxFinds.WholeWord)
            '大小写匹配
        ElseIf CheckBox1.Checked Then
            pos = f.M_RichTextBox.Find(SearchStr, SearchPosition, RichTextBoxFinds.MatchCase)
            '全字匹配
        ElseIf CheckBox2.Checked Then
            pos = f.M_RichTextBox.Find(SearchStr, SearchPosition, RichTextBoxFinds.WholeWord)
        Else
            pos = f.M_RichTextBox.Find(SearchStr, SearchPosition, RichTextBoxFinds.None)
        End If
        If pos >= 0 Then
            f.M_RichTextBox.SelectionStart = pos
            f.M_RichTextBox.SelectionLength = SearchStr.Length()
            PreViousColor = f.M_RichTextBox.SelectionColor
            f.M_RichTextBox.SelectionColor = Color.Red
            SearchPosition = pos + SearchStr.Length()
        Else
            MessageBox.Show("未找到指定的文本", "查找")
        End If

    End Sub

    Private Sub CloseBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseBtn.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SearchPosition = 0
    End Sub

   
    Private Sub SearchForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

⌨️ 快捷键说明

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