请问各位大虾有谁能告诉我,如何在 RichTextBox 中实现1、查找关键字
2、查找下一个
3、替换关键字
4、全部替换请各位多多帮忙,因为我要参加一个小软件设计的比赛,它对我来说很重要,由于我只有60可用分(全给了,还请大家多多包含),所以请大家多多帮忙,小弟感激不尽,Thanks!

解决方案 »

  1.   

    richtextbox有一个类似sendmessage的函数,不记得啦。你自己找找。。
      

  2.   

    1.Richtextbox控件有一个Find方法用于查找,也可以使用Instr和InstrRev函数;
    2.用一变量保存查找时的开始位置,然后用Find或Instr或InstrRev进行查找即可;
    3.当查找到后用SelStart和SelLength选中要替换的内容后把替换内容写到SelText属性中即可;
    4.用Replace()函数可以替换所有相同内容
      

  3.   

    http://expert.csdn.net/Expert/topic/1596/1596016.xml
    http://expert.csdn.net/Expert/topic/1596/1596021.xml
      

  4.   

    不同意 FSoft(),原因是Richtextbox控件的Find方法不支持中文(或说不能很好支持)
      

  5.   

    同意使用sendmessage的函数  http://www.51study.net/detail.asp?ArtId=227
      

  6.   

    请参考
    Dim iWz as long
    Private Sub FindStr()  '向下和所有
      With RichTextBox1
        If .TextRTF = "" Or cmbFind.Text = "" Then Exit Sub   '没有内容或没有查找内容则退出
        Dim iP As Integer   '记录上一次的位置
        If bReplace Then
          If .SelText = cmbFind.Text Then
            .SelText = cmbReplace.Text
            .SelStart = .SelStart + Len(cmbReplace.Text)
            Exit Sub
          End If
        End If
        If bEnd = True Then
          If .SelStart >= iWz Then
            bEnd = False
              sMsgFindInfo
            Exit Sub
          End If
        End If
        iP = .SelStart
        .SelStart = .SelStart + .SelLength
        sFindStyle
        If .SelLength = 0 Then    '没有找到
          If iWz = 0 Then
            sMsgFindInfo
          Else
            bEnd = True           '标记从头开始找
            .SelStart = 0
            sFindStyle
            If .SelLength = 0 Then
              sMsgFindInfo
            Else
              If .SelStart >= iWz Then
                .SelStart = iP
                sFindStyle
                sMsgFindInfo
              Else
                If bReplace Then
                  .SelText = cmbReplace.Text
                  iWz = iWz + Len(cmbReplace.Text) - Len(cmbFind.Text)
                End If
              End If
            End If
          End If
        Else
          If bReplace Then
            .SelText = cmbReplace.Text
          End If
        End If
      End With
    End Sub
      

  7.   

    替换所有的:
    Private Sub ReplaceAll()
      Dim I As Integer, I1 As Integer
      Dim iP As Integer
      With RichTextBox1
        If Trim(.SelText) = cmbFind.Text Then
          .SelText = cmbReplace.Text
          .SelStart = .SelStart + Len(cmbReplace.Text)
          I1 = 1
        End If
        For I = 0 To Len(.TextRTF)
          iP = .SelStart
          .SelStart = .SelStart + .SelLength
          sFindStyle
          If bEnd = True Then
            If iWz <= .SelStart Then
              bEnd = False
              sMsgReplaceInfo I + I1
              Exit For
            End If
          End If
          If .SelLength = 0 Then
            If iWz = 0 Then
              bEnd = False
              sMsgReplaceInfo I + I1
              Exit For
            Else
              bEnd = True           '到达未尾
              .SelStart = 0
              sFindStyle
              If .SelLength = 0 Then
                bEnd = False
                sMsgReplaceInfo I + I1
                Exit For
              Else
                If iWz <= .SelStart Then
                  bEnd = False
                  sMsgReplaceInfo I + I1
                  Exit For
                Else
                  .SelText = cmbReplace.Text
                  iWz = iWz + Len(cmbReplace.Text) - Len(cmbFind.Text)
                End If
              End If
            End If
          Else
            .SelText = cmbReplace.Text
          End If
        Next
      End With
    End Sub
      

  8.   

    需加以下这句:
    Dim bEnd As Boolean, bReplace As Boolean
      

  9.   

    补充:
    Private Sub sFindStyle()
      With RichTextBox1
        'chkQZPP 全字匹配  chkDXX 区分大小写
        If chkQZPP And chkDXX Then
          .Find cmbFind.Text, , Len(.TextRTF), rtfWholeWord Or rtfMatchCase
        ElseIf chkQZPP Then
          .Find cmbFind.Text, , Len(.TextRTF), rtfWholeWord
        ElseIf chkDXX Then
          .Find cmbFind.Text, , Len(.TextRTF), rtfMatchCase
        Else
          .Find cmbFind.Text, , Len(.TextRTF)
        End If
      End With
    End Sub
      

  10.   

    把这句删掉
    sMsgReplaceInfo及sMsgFindInfocmbFind.Text 为要进行查找的Combo框
    chkQZPP 及chkDXX 为CheckBox框