'函数名字:getTextboxSelLength
'函数作用:单击textBox,根据光标所在位置,选中光标前后处指定内容。
'strText:textBox控件
'numPosition:光标单击位置
'strPrev:指定前面内容
'strNext:指定后面内容
Public Sub getTextboxSelLength(strText As TextBox, numPosition As Integer, strPrev As String, strNext As String)
On Error GoTo er
'//取得前面的位置
Dim numPrev As Integer, strFronCode As String
strFronCode = Mid(strText, 1, numPosition)
If InStr(strFronCode, strPrev) > 0 Then
Dim N As Integer
N = 1
Do While N <> Len(strFronCode)
'MsgBox "字母:" & Mid(strFronCode, numPosition - N + 1, 1) & " N:" & N & " 总长:" & Len(strFronCode)
If Mid(strFronCode, numPosition - N + 1, 1) = strPrev Then
numPrev = numPosition - N + 1
Exit Do
End If
N = N + 1
If N = 1000 Then Exit Sub '1000个退出避免死循环
Loop
Else
numPrev = 0
End If
'MsgBox "上一个:" & numPrev
'//取得后面的位置
Dim numNext As Integer
numNext = InStr(numPosition, strText, strNext) - 1
'MsgBox "下一个:" & numNext
'//取得选中内容
strText.SelStart = numPrev
strText.SelLength = numNext - numPrev
Exit Sub
er:
MsgBox err.Description, , "提示"
End Sub
vb 取得单击textBox选中指定内容
最新推荐文章于 2021-07-04 11:03:30 发布