Solved

Textbox -- what is the best way to limit the text box to one decimal in currency format?

Posted on 2010-08-15
9
403 Views
Last Modified: 2013-11-26
I would like to limit my end user to one decimal in currency format.

When the user submits the data, it should be in the format of 0.00 where the amount can be any number up to decimal(18, 2)
0
Comment
Question by:jdraggi
9 Comments
 

Expert Comment

by:abrajoe
Comment Utility
you can use Math.Round() before you submit the value to the database
0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
Try this:


        If e.KeyCode = Keys.Back OrElse e.KeyCode = Keys.Delete Then    'allow Backspace and Delete Keys

            Exit Sub

        End If

        If e.KeyCode = "-" Then 'allow negative sign as the first character

            If TextBox1.Text.Trim <> "" Then

                e.SuppressKeyPress = True

            End If

        End If

        If TextBox1.Text.Contains(".") Then

            If e.KeyCode = Keys.Decimal Then

                e.SuppressKeyPress = True

                Exit Sub

            End If

            If TextBox1.Text Like "*.##" Then

                e.SuppressKeyPress = True

            End If

        End If

Open in new window

0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
Oh, the code should be in the KeyDown event:

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Back OrElse e.KeyCode = Keys.Delete Then    'allow Backspace and Delete Keys
            Exit Sub
        End If
        If e.KeyCode = "-" Then 'allow negative sign as the first character
            If TextBox1.Text.Trim <> "" Then
                e.SuppressKeyPress = True
            End If
        End If
        If TextBox1.Text.Contains(".") Then
            If e.KeyCode = Keys.Decimal Then
                e.SuppressKeyPress = True
                Exit Sub
            End If
            If TextBox1.Text Like "*.##" Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub
0
 
LVL 8

Expert Comment

by:PagodNaUtak
Comment Utility
When submitting the value try this code:
textbox1.text.tostring("0.00")
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 100 total points
Comment Utility
Ooops, there is a syntax error in my previous posts.
Sorry for the mistakes.

1. Here is the correct code:

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Back OrElse e.KeyCode = Keys.Delete Then    'allow Backspace and Delete Keys
            Exit Sub
        End If
        If e.KeyCode = Keys.Space Then 'do not allow spaces
            e.SuppressKeyPress = True
            Exit Sub
        End If
        If e.KeyCode = Keys.Subtract OrElse e.KeyCode = Keys.OemMinus Then 'allow negative sign as the first character
            If TextBox1.Text <> "" AndAlso TextBox1.Text.StartsWith("-") Then
                e.SuppressKeyPress = True
            ElseIf TextBox1.SelectionStart > 0 Then
                e.SuppressKeyPress = True
            End If
            Exit Sub
        End If
        If TextBox1.Text.Contains(".") Then
            If e.KeyCode = Keys.Decimal Then
                e.SuppressKeyPress = True
                Exit Sub
            End If
            If TextBox1.Text Like "*.##" Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub


2. To submit the value, do this:

Format(Val(TextBox1.Text), "0.00")
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
Use the MaskedTextbox control, with a mask of "#.0"...

http://msdn.microsoft.com/en-us/library/kkx4h3az.aspx

Wayne
0
 

Expert Comment

by:haroonali361
Comment Utility
abcvalue = FormatNumber$(Val(abc.text, 2)
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
Try also this ...working on me ....
Public Function Currency(ByVal sender As System.Object, ByVal eChar As Char) As Boolean

        Dim CheckString As String = "$0123456789."

        If CheckString.IndexOf(eChar) > -1 OrElse eChar = vbBack Then

            If CType(sender, TextBox).Text.IndexOf(".") > -1 Then

                If CType(sender, TextBox).TextLength - CType(sender, TextBox).Text.IndexOf(".") > 2 And eChar <> vbBack Then

                    Return True

                End If

            End If

            If eChar = "$" Then

                If CType(sender, TextBox).TextLength > 0 Then

                    Return True

                Else

                    Return False

                End If

            End If

            If eChar = "." Then

                If CType(sender, TextBox).Text.IndexOf(eChar) > -1 Then

                    Return True

                Else

                    Return False

                End If

            End If

            Return False

        Else

            Return True

        End If

    End Function



Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        e.Handled = Currency(sender, e.KeyChar)

    End Sub

Open in new window

0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 400 total points
Comment Utility

       Dim CheckString As String = "$0123456789."

        If CheckString.IndexOf(eChar) > -1 OrElse eChar = vbBack Then

            If CType(sender, TextBox).Text.IndexOf(".") > -1 Then

                If CType(sender, TextBox).TextLength - CType(sender, TextBox).Text.IndexOf(".") > 1 And eChar <> vbBack Then   '------>For One Digit After decimal'

                    Return True

                End If

            End If

            If eChar = "$" Then

                If CType(sender, TextBox).TextLength > 0 Then

                    Return True

                Else

                    Return False

                End If

            End If

            If eChar = "." Then

                If CType(sender, TextBox).Text.IndexOf(eChar) > -1 Then

                    Return True

                Else

                    Return False

                End If

            End If

            Return False

        Else

            Return True

        End If

    End Function



Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        e.Handled = Currency(sender, e.KeyChar)

    End Sub

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now