Solved

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

Posted on 2007-11-29
9
1,181 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:PBLack
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 7

Accepted Solution

by:
prosh0t earned 200 total points
ID: 20379150
It is actually fairly difficult to do something so simple because there are a lot of possibilities.  What if  someone pastes some text into the box that is invalid?  Then your code will never get called. What if the cursor is not all the way at the end of the text?  What if they try to enter two decimals into the box?

Basically the easiest way to implement this is to derive your own textbox control from the winforms text box, and store the data currently in the text box into a private variable.  Then, override an event that will trigger AFTER the text is already changed - for example, KeyUp or TextChanged.  Validate the text, and if it is an invalid decimal, set the text of your textbox back to what is in that private variable.  Otherwise, update your private variable with the new text.  

It sounds complicated but the code is actually fairly simple, and this is the best way to go about it because then you will be sure that you'll never have an invalid decimal in your textbox.  If you try to validate it one keystroke at a time, believe me the code gets messy.
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 150 total points
ID: 20379190
you better validate on lostfocus with a regular expression to be sure that the user does not paste a value. see http://www.emoreau.com/Entries/Articles/2003/03/Creating-your-own-Windows-Custom-Control.aspx
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 150 total points
ID: 20379197
also consider to use the MaskedTextBox control.
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

Author Comment

by:PBLack
ID: 20379277
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
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 20379325
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
0
 

Author Comment

by:PBLack
ID: 20379377
emoreau

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

PBLack
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 20379474
I thought that this feature was built-in in the textbox control!
0
 

Author Comment

by:PBLack
ID: 20379521
not that I can see
0
 

Author Comment

by:PBLack
ID: 20388030
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!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question