?
Solved

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

Posted on 2007-11-29
9
Medium Priority
?
1,188 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 800 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 600 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 600 total points
ID: 20379197
also consider to use the MaskedTextBox control.
0
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

719 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