Link to home
Start Free TrialLog in
Avatar of Todd MacPherson
Todd MacPhersonFlag for Canada

asked on

I need KeyPress code to limit textbox entry to 2 places of decimal

Hi

I am using vb.net 2005 compact framework. I have the following keypress code that limits values entered into a textbox to numeric entry and back space. I need to take it one step further and limit entry to 2 places of decimals.

Code thus far:

Private Sub txtStandArea_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStandArea.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = "." And Not e.KeyChar = ChrW(8) Then
            Beep()
            e.Handled = True
        End If
    End Sub

I was trying to incorporate txtStandArea.Text.Length >= txtStandArea.Text.IndexOf(".") + 2 into it but I just cant seem to get the proper way. Please help.

Thanks

PBLack
ASKER CERTIFIED SOLUTION
Avatar of prosh0t
prosh0t

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Todd MacPherson

ASKER

I do not know if your concerns apply to compact framework. I came up with a bit of code that seems to work just fine. I have tried using ctrl c to copy and ctrl v to paste but in compact framework it does not work...at least not on my device. Dell Axim X3.

The code I am using is working 100% as I wanted it to. Now if someone does know how to effect a copy-paste on this textbox I will have to change my ways as per your suggestions.

PBLack

    Private Sub txtStandArea_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStandArea.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = "." And Not e.KeyChar = ChrW(8) Then
            Beep()
            e.Handled = True
        Else
            If e.KeyChar = "." And txtStandArea.Text.IndexOf(".") <> -1 Then
                Beep()
                e.Handled = True
            ElseIf e.KeyChar = "." Then
                e.Handled = False
            ElseIf Char.IsDigit(e.KeyChar) Then
                If txtStandArea.Text.IndexOf(".") <> -1 Then
                    If txtStandArea.Text.Length >= txtStandArea.Text.IndexOf(".") + 3 Then
                        Beep()
                        e.Handled = True
                    End If
                End If
            End If
        End If
    End Sub
it is not ctrl-c ctrl-v but if you click and hold the textbox, a menu will appear and let you copy and paste content
emoreau

that is true if you only program that functionality into it.

PBLack
I thought that this feature was built-in in the textbox control!
not that I can see
Hi again

Since I think that I reached a solution that works outside of the expert suggestions I will divide the points up among those who took the time to share their ideas.

Their solutions are great 'food for thought' outside of the compact framework schema.

Regards,

PBLack

ps - experts-exchange should have a compact framework section!