VB高手 来解释下意思

来源:百度知道 编辑:UC知道 时间:2024/06/15 03:25:21
Private Sub command1_Click()
Dim found As Integer, n As Integer
Do
found = InStr(Text1.Text, Text2.Text)
If found <> 0 Then
Text1.SelStart = found - 1
Text1.SelLength = Len(Text2.Text)
Text1.SelText = Text3.Text
n = 1
Else
If n = 0 Then MsgBox ("没有找到!")
End If
Loop While found <> 0
End Sub

在 TEXT1 中的所有有 TEXT2的文字, 用 TEXT3 来替换

但这个程式没做error handling
当 TEXT2.TEXT = TEXT3.TEXT
=> 程式进入 infinite loop, 永远跑不出来

Private Sub command1_Click()
Dim found As Integer, n As Integer
Do
found = InStr(Text1.Text, Text2.Text)
'找到Text1中第一个有Text2的位置

If found <> 0 Then
'found=0表找不到

Text1.SelStart = found - 1
'.SelStart是从0开始; 但 found是从1开始,所以要减1
Text1.SelLength = Len(Text2.Text)
'.selStart, .SelLength => 设定 SelText
'=> 将Text1中的Text2相同文字选起来

Text1.SelText = Text3.Text
'置换成Text3文字

n = 1
'设旗标n为1 (表示原Text1中至少有1个Text2文字
Else
'第一次就找不到=>n=0就show出"没有找到!"
'若是第2,3,...次才找不到=>n=1就没有show出"没有找到!"
'=> 因做到这found一定为0,并回离开loop
If n = 0 Then MsgBox ("没有找到!&quo