|
-
Nov 29th, 2013, 02:34 AM
#1
Thread Starter
Hyperactive Member
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?
-
Nov 29th, 2013, 04:06 AM
#2
Re: SelStart At Mouse Position
Is there a special reason for executing the code in the GotFocus event?
-
Nov 29th, 2013, 09:09 PM
#3
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
-
Dec 3rd, 2013, 02:10 AM
#4
Thread Starter
Hyperactive Member
Re: SelStart At Mouse Position
 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")
-
Dec 3rd, 2013, 02:13 AM
#5
Thread Starter
Hyperactive Member
Re: SelStart At Mouse Position
 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.
-
Dec 4th, 2013, 01:01 PM
#6
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.
-
Dec 4th, 2013, 01:26 PM
#7
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.
-
Dec 6th, 2013, 09:52 PM
#8
Thread Starter
Hyperactive Member
Re: SelStart At Mouse Position
Sam it doesn't seem to be accurate in position the caret to where I click
-
Dec 6th, 2013, 11:08 PM
#9
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
-
Dec 7th, 2013, 08:26 AM
#10
Re: SelStart At Mouse Position
 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.
-
Dec 8th, 2013, 06:56 PM
#11
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.
-
Dec 8th, 2013, 09:40 PM
#12
Thread Starter
Hyperactive Member
Re: SelStart At Mouse Position
 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.
-
Dec 8th, 2013, 10:03 PM
#13
Thread Starter
Hyperactive Member
Re: SelStart At Mouse Position
 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 ",".
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|