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?
Re: SelStart At Mouse Position
Is there a special reason for executing the code in the GotFocus event?
Re: SelStart At Mouse Position
Quote:
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
Re: SelStart At Mouse Position
Quote:
Originally Posted by
Arnoutdv
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")
Re: SelStart At Mouse Position
Quote:
Originally Posted by
westconn1
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.
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.
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.
Re: SelStart At Mouse Position
Sam it doesn't seem to be accurate in position the caret to where I click
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
Re: SelStart At Mouse Position
Quote:
Originally Posted by
doctrin13th
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.
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.
Re: SelStart At Mouse Position
Quote:
Originally Posted by
SamOscarBrown
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.
Re: SelStart At Mouse Position
Quote:
Originally Posted by
Lord Orwell
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 ",".