Results 1 to 13 of 13

Thread: SelStart At Mouse Position

  1. #1

    Thread Starter
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 2008
    Posts
    303

    Question SelStart At Mouse Position

    I have a textbox "txtNum" with the following codes

    Private Sub txtNum_GotFocus()
    txtNum.Text = Format(txtNum.Text, "General Number")
    End Sub

    Private Sub txtNum_LostFocus()
    txtNum.Text = Format(txtNum.Text, "Standard")
    End Sub

    Now, when I click the text box, thus it gets focus, it is formatted as expected. However, the cursor is always at the beginning of the text.
    How can I set the cursor position to where I click?

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,735

    Re: SelStart At Mouse Position

    Is there a special reason for executing the code in the GotFocus event?

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: SelStart At Mouse Position

    However, the cursor is always at the beginning of the text.
    you can force the cursor to a specific position using the selstart property

    try like
    Code:
    Private Sub Text1_GotFocus()
    s = Text1.SelStart
    Text1.Text = Format(Text1.Text, "General Number")
    Text1.SelStart = s
    End Sub
    Last edited by westconn1; Nov 29th, 2013 at 09:12 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 2008
    Posts
    303

    Re: SelStart At Mouse Position

    Quote Originally Posted by Arnoutdv View Post
    Is there a special reason for executing the code in the GotFocus event?
    Say the input text is "1500.5". I want the textbox to format "1500.5" to "1,500.50" when it loses focus and format back again to General Number ("1500.5") when it gets focused, but still I want the textbox cursor (caret) be positioned where I click (e.g. between 1 and 5 in "1500.5")

  5. #5

    Thread Starter
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 2008
    Posts
    303

    Re: SelStart At Mouse Position

    Quote Originally Posted by westconn1 View Post
    you can force the cursor to a specific position using the selstart property

    try like
    Code:
    Private Sub Text1_GotFocus()
    s = Text1.SelStart
    Text1.Text = Format(Text1.Text, "General Number")
    Text1.SelStart = s
    End Sub
    Thank you! There's the idea.Very close! However, the problem is the length of the text is different when focused and when loses focus making the SelStart property variable.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: SelStart At Mouse Position

    does this take care of it? (text length changes because of the comma)---only works up to 999,999.9 (one comma)
    Code:
    Private Sub txtNum_GotFocus()
    Dim s As Integer
    Dim y As Integer
    s = txtnum.SelStart
    y = InStr(1, txtnum.Text, ",")
    txtnum.Text = Format(txtnum.Text, "General Number")
    If y > 0 Then
        txtnum.SelStart = s - 1
    Else
        txtnum.SelStart = s
    End If
    End Sub
    
    Private Sub txtNum_LostFocus()
    txtnum.Text = Format(txtnum.Text, "Standard")
    End Sub
    EDIT: SORRY, won't work if selecting before first or second chars....still testing.
    Last edited by SamOscarBrown; Dec 4th, 2013 at 01:06 PM.

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: SelStart At Mouse Position

    This seems to work (up to 999,999,999.9 anyway)
    Code:
    Private Sub txtNum_GotFocus()
    Dim s As Integer
    Dim y As Integer
    s = txtnum.SelStart
    y = InStr(1, txtnum.Text, ",")
    txtnum.Text = Format(txtnum.Text, "General Number")
    If s <> 0 Then
        Select Case y
        Case 0
            txtnum.SelStart = s
        Case Else
            txtnum.SelStart = s - 1
        End Select
    End If
    End Sub
    I used the Case statement because I was testing y, you can use an If instead if desired.

  8. #8

    Thread Starter
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 2008
    Posts
    303

    Re: SelStart At Mouse Position

    Sam it doesn't seem to be accurate in position the caret to where I click

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: SelStart At Mouse Position

    see if this works correctly for you
    Code:
    Private Sub Text1_GotFocus()
    s = Text1.SelStart
    num = CDbl(Left(Text1.Text, s))
    Text1.Text = Format(Text1.Text, "General Number")
    Text1.SelStart = Len(num)
    End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: SelStart At Mouse Position

    Quote Originally Posted by doctrin13th View Post
    Sam it doesn't seem to be accurate in position the caret to where I click
    Hmmmm. worked in all my test....oh well.....have a great day.

  11. #11
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: SelStart At Mouse Position

    why don't you use a formatted textbox instead? It's a forms 2.0 control and allows you to not worry about how the text is formatted because the control does all the work for you.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  12. #12

    Thread Starter
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 2008
    Posts
    303

    Re: SelStart At Mouse Position

    Quote Originally Posted by SamOscarBrown View Post
    Hmmmm. worked in all my test....oh well.....have a great day.
    With two textboxes, focus to the other textbox (not the txtNum)
    Using this:
    (This is your code)

    Code:
    Private Sub txtNum_GotFocus()
    Dim s As Integer
    Dim y As Integer
    s = Txtnum.SelStart
    y = InStr(1, Txtnum.Text, ",")
    Txtnum.Text = Format(Txtnum.Text, "General Number")
    If s <> 0 Then
        Select Case y
        Case 0
            Txtnum.SelStart = s
        Case Else
            Txtnum.SelStart = s - 1
        End Select
    End If
    End Sub
    and

    Code:
    Private Sub Txtnum_LostFocus()
     Txtnum.Text = Format$(Txtnum.Text, "Standard")
    End Sub
    With "1,500,300.50" as the txtNum.Text property, try clicking between "3" and "0" in the "300". The caret is positioned between the two zeros in "300".

    Anyways, thank you for your time.

  13. #13

    Thread Starter
    Hyperactive Member doctrin13th's Avatar
    Join Date
    Sep 2008
    Posts
    303

    Re: SelStart At Mouse Position

    Quote Originally Posted by Lord Orwell View Post
    why don't you use a formatted textbox instead? It's a forms 2.0 control and allows you to not worry about how the text is formatted because the control does all the work for you.
    Actually I'm developing my numeric textbox (ActiveX control) for custom behaviors and properties. Just like MartinLiss' numberbox but offers a choice whether to include digit grouping separator ",".

Tags for this Thread

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