《查找、替换、查下一个》代码示例

发表于:2007-05-25来源:作者:点击数: 标签:代码示例
在这里新建二个TEXT.分别为text1、text2. 新建三个按钮。为command1、COMMAND2、COMMAND3 FORM1窗口中的text5为查找范围。 Option Explicit Private TargetPosition As Integer Private Sub FindText(ByVal start_at As Integer) Dim pos As Integer Dim tar
 在这里新建二个TEXT.分别为text1、text2.
新建三个按钮。为command1、COMMAND2、COMMAND3
FORM1窗口中的text5为查找范围。

Option Explicit
Private TargetPosition As Integer

      Private Sub FindText(ByVal start_at As Integer)
      Dim pos As Integer
      Dim target As String
      '获取用户输入的要查找的字符串
      target = Text1.Text
      pos = InStr(start_at, Form1.Text5.Text, target)
      If pos > 0 Then
            '找到了匹配字符串
            TargetPosition = pos
            Form1.Text5.SelStart = TargetPosition - 1
            '选中找到的字符串
            Form1.Text5.SelLength = Len(target)
            Form1.Text5.SetFocus
      Else '没有找到匹配的字符串
      MsgBox "没找到!"
      Text1.SetFocus
      End If
      End Sub
Private Sub Command1_Click()
FindText 1

End Sub

Private Sub Command2_Click()
FindText TargetPosition + 1
End Sub

Private Sub Command3_Click()
FindText 1
Form1.Text5.SelText = Text2.Text

End Sub

 

原文转自:http://www.ltesting.net