回 帖 发 新 帖 刷新版面

主题:[讨论]查找替换功能探讨

Public Class Form2
    Inherits System.Windows.Forms.Form
    Dim pos As Integer
    Dim tpos As Integer = 1
    Dim num As Integer
    Dim form3 As New form3
    End Sub
  Private Sub findtext(ByVal start As Integer)  '查找
        Dim text2 As String
        text2 = TextBox1.Text
        pos = InStr(start, form3.RichTextBox1.Text, text2)

        If pos > 0 Then
            tpos = pos

            form3.RichTextBox1.SelectionStart = tpos - 1
            form3.RichTextBox1.SelectionLength = Microsoft.VisualBasic.Len(text2)
            form3.RichTextBox1.Focus()
        Else
            MsgBox("查找完毕!" & "共找到" & num & "个!")
            'TextBox1.Focus()
        End If
    End Sub
    Private Sub findtext1(ByVal start As Integer)  '替换
        Dim text2, text3 As String
        text2 = TextBox1.Text
        text3 = TextBox2.Text

        pos = InStr(start, form3.RichTextBox1.Text, text2)
        If pos > 0 Then
            tpos = pos

            form3.RichTextBox1.SelectionStart = tpos - 1
            form3.RichTextBox1.SelectionLength = Microsoft.VisualBasic.Len(text2)
            form3.RichTextBox1.Focus()
            form3.RichTextBox1.SelectedText = text3
        Else
            MsgBox("替换完毕!" & "共替换" & num & "个!")
            'TextBox1.Focus()
        End If
    End Sub
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '查找按钮
        findtext(1)
        If pos > 0 Then
            num = 1
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click '查找下一个按钮
        findtext(tpos + Len(TextBox1.Text))
        num += 1
    End Sub
   Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        num = 0
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '替换按钮
        findtext(1)
        findtext1(tpos)
        num += 1
    End Sub
   Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        num = 0
    End Sub
如果被查询的窗口只有一个则行的通,
但如果象多文档编辑器有很多个文档该如何确定要查找的是哪个文档?

回复列表 (共2个回复)

沙发

多文档的话,可以将当前获得焦点的文档作为查找对象。
或者
使用另外一个窗体作为查找对话框,然后在创建这个窗体的时候把查找对象的信息传入该窗体。

板凳

首先richtextbox自己有Find()方法的
另外string.replace("old","new")提供整体替换
如果需要额外功能的替换,建议使用正则表达式

我来回复

您尚未登录,请登录后再回复。点此登录或注册