关于VB搜索字符的问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 19:29:47
text1(支持多行输入)中有这样一段文字
123 ac
124 bc
125 cc
127 cc
怎样在text1中搜索“123”如果搜索到,则显示出它后面的字符,即“ac"
如果搜索不到,则msgbox"未搜索到相关字符"

Private Sub Command1_Click()
Text1.Text = "123 ac" & vbCrLf & "124 bc" & vbCrLf & "125 cc" & vbCrLf & "127 cc"
End Sub

Private Sub Command2_Click()
Dim numPos As Long
Dim newLinePos As Long
Dim result As String

numPos = InStr(1, Text1.Text, "123")
If numPos <> 0 Then
newLinePos = InStr(numPos + 1, Text1.Text, vbNewLine)
result = Mid(Text1.Text, numPos + 4, newLinePos - numPos - 3)
MsgBox result
Else
MsgBox "未搜索到相关字符"
End If

End Sub