请问各位大虾有谁能告诉我,如何在 RichTextBox 中实现1、查找关键字
2、查找下一个
3、替换关键字
4、全部替换请各位多多帮忙,因为我要参加一个小软件设计的比赛,它对我来说很重要,由于我只有60可用分(全给了,还请大家多多包含),所以请大家多多帮忙,小弟感激不尽,Thanks!
2、查找下一个
3、替换关键字
4、全部替换请各位多多帮忙,因为我要参加一个小软件设计的比赛,它对我来说很重要,由于我只有60可用分(全给了,还请大家多多包含),所以请大家多多帮忙,小弟感激不尽,Thanks!
2.用一变量保存查找时的开始位置,然后用Find或Instr或InstrRev进行查找即可;
3.当查找到后用SelStart和SelLength选中要替换的内容后把替换内容写到SelText属性中即可;
4.用Replace()函数可以替换所有相同内容
http://expert.csdn.net/Expert/topic/1596/1596021.xml
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
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
Dim bEnd As Boolean, bReplace As Boolean
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
sMsgReplaceInfo及sMsgFindInfocmbFind.Text 为要进行查找的Combo框
chkQZPP 及chkDXX 为CheckBox框