Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to establish cursor position in a textbox 1

Status
Not open for further replies.

PaulAH

Programmer
Oct 29, 2002
26
NZ
Hi all,

I have various textbox's where users can input a number.
Using one as an example of my problem...

I want to limit the input of a percentage to upto 99.99.
I know I can add validation after the users entered a value but I'm trying to be a bit smarter and not allow an invalid value to be entered in the first place.
I've restricted the length of the textbox to 5 chars and I limit entries to 0-9, '.' and backspace within the KeyPress event. However this wouldn't stop a user from entering 99999. So I've also tried to anticipate what the entry value will be once the new keystroke takes effect. All works OK unless... the user enters 99.9 then moves the cursor back and enters another 9 (so the result is 999.9). My code passes this value a valid as it adds the new keystroke to the end of the existing value so anticipating the value will be 99.99.
Is there a way I can establish where the cursor position is within the textbox so I can more accurately predict what the new value will be.?

Here's my code....

Code:
Private Sub txtCommisionValue_KeyPress(Index As Integer, KeyAscii As Integer)
    Dim sValue As String
    Dim cValue As Currency
    Dim cMaxValue As Currency
    
    'Ensure only numeric values entered...(ignore backspace)
    If KeyAscii <> 8 Then
        KeyAscii = Mask(KeyAscii, MASK_DECIMAL, txtCommisionValue(Index), False, 2)
        
        If KeyAscii > 0 Then
            If Len(Trim(txtCommisionValue(Index))) > 0 Then
                cMaxValue = 99.99
                
                'calculate new value..
                sValue = txtCommisionValue(Index) + Chr(KeyAscii)
                cValue = CCur(sValue)
                'Ensure new value is not too large..
                If cValue > cMaxValue Then
                    KeyAscii = 0
                End If
            End If
        End If
    End If

End Sub

Thanks in advance for your help.
 
Look into the Microsoft Masked Edit Control found in your Project menu underneath Controls. This should do the trick.

Swi
 
Thanks Swi for your response....I'm editing some old code thats not that flash using an array of text boxes. So I don't want to change the control type in case it causes more problems than it solves!....any other ideas? I was hoping there might be an api?
 
Just as a point of information, there is no API call called SendMessageByNum.

It is a user-created alias for the SendMessage API call, specifically one in which the lParam parameter has been defined as 'As Long' rather than the general 'As Any'

The Textbox's SelStart property will tell you the current insertion point ...
 
<The Textbox's SelStart property will tell you the current insertion point ...

And you may also set this value if you need to. I'd say the API solution is a bit of overkill. It was designed to give you the cursor position in a rich text box, which is a bit more complicated than what you're trying to do here. Here, all you need to do is access the TextBox's SelStart property, as strongm says.

Bob
 
Thanks strongm...I've used the SelStart before but didn't realise it would apply in this case....doh![sleeping2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top