Results 1 to 21 of 21

Thread: Prevent Mouse from changing caret location in Textbox.

Threaded View

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Prevent Mouse from changing caret location in Textbox.

    I want my textbox to be read-only. I've got that part.

    The Locked property takes care of that but only that.

    But the user can still move the caret around which can screw up the text if it's in the middle of putting something on screen.

    In the Keydown sub I just set the Keycode to 0 so they can't do it using keys, but they still can using the mouse.

    This comes up because I have a "typing" algorithm that simulates typing by putting down one character at a time with a random, very short pause after each character.

    During that time the user can click on the textbox and change where the text ends up - usually right in the middle of something else.

    This is not a serious app so it's not a huge big deal but I'd still like to know how to do it without actually turning off the mouse which could be maybe a little bit rude to the user.

    Code:
    Public Sub SimulateTyping(ByRef Token As String, ByRef Textbox As VB.Textbox, Optional ByRef PauseMS As Long = 150)
    Dim n As Long
    Dim s As String
    
    ' Adds delay between characters to simulate typing.
    ' Starts 'typing' wherever the caret is.
    
    If Len(Token) = 0 Then Exit Sub
    
    With Textbox
    
      For n = 1 To Len(Token)
    
        .SelText = Mid$(Token, n, 1)
    
        Sleep RollDie(PauseMS)
    
      Next n
    
    End With
    
    End Sub
    
    
    Public Function RollDie(ParamArray Die()) As Long
    Dim nValue As Long
    Dim nTotal As Long
    Dim n As Long
    
    ' Returns Sum of all Die Rolled.
    
    nTotal = 0
    
    For n = LBound(Die) To UBound(Die)
    
      nValue = Int(Rnd * Die(n)) + 1
    
      nTotal = nTotal + nValue
    
    Next n
    
    RollDie = nTotal
    
    End Function
    Last edited by cafeenman; Dec 20th, 2024 at 03:38 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width