
从网上收集的vb.net文本查找替换代码,效果还可以,可以按回车查找下一个,以下代码是从实例程序中直接复制过来的,想必知道vb.net的朋友都可以看懂,主要是看思路。
折叠ASP/Visual Basic Code复制内容到剪贴板
'==============================================================
- '小牛制作2008
- '===========================================
- 'Design calf 流星记事本
- 'homepage:www.spersky.com
- '==============================================================
- Public Class findreplace
- Public kaishi As Integer
- Public pos As Integer
- Public shou As String
- '窗体加载
- Private Sub findreplace_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Dim tempRect As New Rectangle(frmBiJi.Left + (frmBiJi.Width - Me.Width) / 2, frmBiJi.Top + (frmBiJi.Height - Me.Height) / 2, 369, 173)
- ' Set the bounds of the form using the Rectangle object.
- DesktopBounds = tempRect
- If shou <> "" Then
- txtsearch.Text = shou
- End If
- txtsearch.Focus()
- End Sub
- '不给关闭,让窗体隐藏,以便下次调用
- Private Sub findreplace_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
- e.Cancel = True
- Me.Hide()
- End Sub
- '当按回车时,执行查找下一个功能
- Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
- Dim WM_KEYDOWN As Integer = 256
- Dim WM_SYSKEYDOWN As Integer = 260
- If btnreplace.Focused Then
- Exit Function
- End If
- If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
- Select Case keyData
- Case Keys.Enter
- '截获到回车键
- Call xiayige()
- Return False
- End Select
- End If
- End Function
- '实现查找下一个的过程代码
- Private Sub xiayige()
- If ckbupgo.Checked = False Then '如果选中了"向下"
- kaishi = frmBiJi.rtbBj.SelectionStart + frmBiJi.rtbBj.SelectionLength + 1
- shou = txtsearch.Text
- If ckbcase.Checked = True Then '如果选中了"区分大小写"
- pos = InStr(kaishi, frmBiJi.rtbBj.Text, shou)
- Else '如果没有选中"区分大小写"
- pos = InStr(kaishi, frmBiJi.rtbBj.Text.ToLower, shou.ToLower, CompareMethod.Text)
- End If
- If pos > 0 Then '如果找到内容
- frmBiJi.rtbBj.Select(pos - 1, Len(shou))
- 'frmBiJi.rtbbj.Focus()
- lbl1.Text = ""
- Else
- ' MsgBox("找不到 " & """" & shou & """", MsgBoxStyle.Information)
- lbl1.Text = "找不到 " & """" & shou & """"
- End If
- Else '如果选中了"向上"
- kaishi = frmBiJi.rtbBj.SelectionStart
- shou = txtsearch.Text
- If ckbcase.Checked = True Then '如果选中了"区分大小写"
- pos = InStrRev(frmBiJi.rtbBj.Text, shou, kaishi)
- Else '如果没有选中"区分大小写"
- pos = InStrRev(frmBiJi.rtbBj.Text.ToLower, shou.ToLower, kaishi)
- End If
- If pos > 0 Then '如果找到内容
- frmBiJi.rtbBj.Select(pos - 1, Len(shou))
- 'frmBiJi.rtbbj.Focus()
- lbl1.Text = ""
- Else
- 'MsgBox("找不到 " & """" & shou & """", MsgBoxStyle.Information)
- lbl1.Text = "找不到 " & """" & shou & """"
- End If
- End If
- End Sub
- Private Sub btnreplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnreplace.Click
- If frmBiJi.rtbBj.SelectedText <> "" Then
- frmBiJi.rtbBj.SelectedText = frmBiJi.rtbBj.SelectedText.Replace(frmBiJi.rtbBj.SelectedText, txtreplace.Text)
- kaishi = kaishi + Len(txtreplace.Text)
- End If
- '替换后查找下一个
- Call xiayige()
- End Sub
- Private Sub btnreplaceall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnreplaceall.Click
- shou = txtsearch.Text
- frmBiJi.rtbBj.Text = frmBiJi.rtbBj.Text.Replace(shou, txtreplace.Text)
- End Sub
- Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click
- 'frmBiJi.rtbbj.Focus()
- Call xiayige()
- End Sub
- Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
- Me.Close()
- End Sub
- 'Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
- ' If Keys.Enter = Keys.Enter Then
- ' Call xiayige()
- ' End If
- 'End Sub
- End Class