📄 frmfind.vb
字号:
Public Class frmFind
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
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 Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents btnFindNext As System.Windows.Forms.Button
Friend WithEvents btnReplace As System.Windows.Forms.Button
Friend WithEvents btnReplaceAll As System.Windows.Forms.Button
Friend WithEvents btnClose As System.Windows.Forms.Button
Friend WithEvents cboFind As System.Windows.Forms.ComboBox
Friend WithEvents chkMatchCase As System.Windows.Forms.CheckBox
Friend WithEvents chkWholeWord As System.Windows.Forms.CheckBox
Friend WithEvents cboReplace As System.Windows.Forms.ComboBox
Friend WithEvents btnGoHome As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.cboFind = New System.Windows.Forms.ComboBox
Me.cboReplace = New System.Windows.Forms.ComboBox
Me.btnFindNext = New System.Windows.Forms.Button
Me.btnReplace = New System.Windows.Forms.Button
Me.btnReplaceAll = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.chkMatchCase = New System.Windows.Forms.CheckBox
Me.btnClose = New System.Windows.Forms.Button
Me.chkWholeWord = New System.Windows.Forms.CheckBox
Me.btnGoHome = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'cboFind
'
Me.cboFind.Location = New System.Drawing.Point(40, 48)
Me.cboFind.Name = "cboFind"
Me.cboFind.Size = New System.Drawing.Size(144, 20)
Me.cboFind.TabIndex = 0
'
'cboReplace
'
Me.cboReplace.Location = New System.Drawing.Point(40, 96)
Me.cboReplace.Name = "cboReplace"
Me.cboReplace.Size = New System.Drawing.Size(144, 20)
Me.cboReplace.TabIndex = 0
'
'btnFindNext
'
Me.btnFindNext.Location = New System.Drawing.Point(208, 48)
Me.btnFindNext.Name = "btnFindNext"
Me.btnFindNext.Size = New System.Drawing.Size(80, 24)
Me.btnFindNext.TabIndex = 1
Me.btnFindNext.Text = "查找下一个"
'
'btnReplace
'
Me.btnReplace.Location = New System.Drawing.Point(208, 80)
Me.btnReplace.Name = "btnReplace"
Me.btnReplace.Size = New System.Drawing.Size(80, 24)
Me.btnReplace.TabIndex = 2
Me.btnReplace.Text = "替 换"
'
'btnReplaceAll
'
Me.btnReplaceAll.Location = New System.Drawing.Point(208, 112)
Me.btnReplaceAll.Name = "btnReplaceAll"
Me.btnReplaceAll.Size = New System.Drawing.Size(80, 24)
Me.btnReplaceAll.TabIndex = 1
Me.btnReplaceAll.Text = "全部替换"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(40, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(91, 17)
Me.Label1.TabIndex = 3
Me.Label1.Text = "要查找的文本:"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(40, 80)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(91, 17)
Me.Label2.TabIndex = 3
Me.Label2.Text = "要替换的文本:"
'
'chkMatchCase
'
Me.chkMatchCase.Location = New System.Drawing.Point(40, 128)
Me.chkMatchCase.Name = "chkMatchCase"
Me.chkMatchCase.TabIndex = 4
Me.chkMatchCase.Text = "区分大小写"
'
'btnClose
'
Me.btnClose.Location = New System.Drawing.Point(208, 144)
Me.btnClose.Name = "btnClose"
Me.btnClose.Size = New System.Drawing.Size(80, 24)
Me.btnClose.TabIndex = 1
Me.btnClose.Text = "关 闭"
'
'chkWholeWord
'
Me.chkWholeWord.Location = New System.Drawing.Point(40, 152)
Me.chkWholeWord.Name = "chkWholeWord"
Me.chkWholeWord.TabIndex = 5
Me.chkWholeWord.Text = "整字匹配"
'
'btnGoHome
'
Me.btnGoHome.Location = New System.Drawing.Point(208, 16)
Me.btnGoHome.Name = "btnGoHome"
Me.btnGoHome.Size = New System.Drawing.Size(80, 24)
Me.btnGoHome.TabIndex = 6
Me.btnGoHome.Text = "回到开头"
'
'frmFind
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(336, 190)
Me.ControlBox = False
Me.Controls.Add(Me.btnGoHome)
Me.Controls.Add(Me.chkWholeWord)
Me.Controls.Add(Me.chkMatchCase)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.btnReplace)
Me.Controls.Add(Me.btnFindNext)
Me.Controls.Add(Me.cboFind)
Me.Controls.Add(Me.cboReplace)
Me.Controls.Add(Me.btnReplaceAll)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.btnClose)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "frmFind"
Me.Text = "查找与替换"
Me.TopMost = True
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Visible = False
End Sub
Private Sub btnFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindNext.Click
FindNext()
End Sub
'要处理的控件
Public rt1 As RichTextBox = Nothing
'当前查找的位置
Private curPos As Long = 0
Private Sub FindNext()
Dim WordToFind As String
Dim options As System.Windows.Forms.RichTextBoxFinds = RichTextBoxFinds.None
Dim i As Integer
If rt1 Is Nothing OrElse Me.cboFind.Text = String.Empty Then
Exit Sub
Else
WordToFind = Me.cboFind.Text
End If
If Me.chkMatchCase.Checked = True Then
options = options Or RichTextBoxFinds.MatchCase
End If
If Me.chkWholeWord.Checked = True Then
options = options Or RichTextBoxFinds.WholeWord
End If
'Me.AddTextToList(Me.cboFind)
i = rt1.Find(WordToFind, curPos, -1, options)
If i <> -1 Then
curPos = i + WordToFind.Length
Else
'回到开头
curPos = 0
MsgBox("没能找到匹配项", MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, "提示信息")
End If
End Sub
'替换选中的字符
Private Sub Replace()
Dim start, length As Integer
start = Me.rt1.SelectionStart
length = Me.rt1.SelectionLength
If Me.cboReplace.Text = String.Empty OrElse length = 0 Then
Exit Sub
End If
'替换
Me.rt1.SelectedText = Me.cboReplace.Text
'重新选中
Me.rt1.SelectionStart = start
Me.rt1.SelectionLength = Me.cboReplace.Text.Length
End Sub
Private Sub ReplaceAll()
Dim WordToFind As String
Dim options As System.Windows.Forms.RichTextBoxFinds = RichTextBoxFinds.None
Dim i, j As Integer
Dim count As Integer
Dim start As Integer
If rt1 Is Nothing OrElse Me.cboFind.Text = String.Empty Then
Exit Sub
Else
WordToFind = Me.cboFind.Text
End If
If Me.chkMatchCase.Checked = True Then
options = options Or RichTextBoxFinds.MatchCase
End If
If Me.chkWholeWord.Checked = True Then
options = options Or RichTextBoxFinds.WholeWord
End If
i = rt1.Find(WordToFind, 0, -1, options)
'的确存在
If i <> -1 Then
j = 0
count = 0
'不断查找
While j <> -1
start = Me.rt1.SelectionStart
'替换
Me.rt1.SelectedText = Me.cboReplace.Text
count += 1 '计数
j = rt1.Find(WordToFind, start + WordToFind.Length, -1, options)
End While
MsgBox("替换完成,共替换" & count & "处。", MsgBoxStyle.Information Or MsgBoxStyle.OKOnly, "提示信息")
Else
'回到开头
curPos = 0
MsgBox("没能找到匹配项。", MsgBoxStyle.OKOnly Or MsgBoxStyle.Information, "提示信息")
End If
End Sub
Private Sub cboFind_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFind.SelectedIndexChanged
'当内容改变时,从头进行搜索
curPos = 0
End Sub
Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
Replace()
End Sub
Private Sub btnReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceAll.Click
ReplaceAll()
End Sub
Private Sub btnGoHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoHome.Click
'重置当前位置
curPos = 0
'回到开头
Me.rt1.SelectionStart = 0
Me.rt1.SelectionLength = 0
End Sub
Private Sub AddTextToList(ByVal cbo As ComboBox)
If cbo.Text = String.Empty Then
Exit Sub
End If
If cbo.Items.IndexOf(cbo.Text) = -1 Then
cbo.Items.Add(cbo.Text)
End If
End Sub
Private Sub cboFind_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboFind.LostFocus
Me.AddTextToList(Me.cboFind)
End Sub
Private Sub cboReplace_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboReplace.SelectedIndexChanged
End Sub
Private Sub cboReplace_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboReplace.LostFocus
Me.AddTextToList(Me.cboReplace)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -