vb 取得单击textBox选中指定内容

'函数名字: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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值