Solved

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

Posted on 2010-08-15
9
435 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
[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
 

Expert Comment

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

Expert Comment

by:Zhaolai
ID: 33442711
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
ID: 33442715
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 33442933
When submitting the value try this code:
textbox1.text.tostring("0.00")
0
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 100 total points
ID: 33443054
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)
ID: 33443089
Use the MaskedTextbox control, with a mask of "#.0"...

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

Wayne
0
 

Expert Comment

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

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33444272
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
ID: 33444275

       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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

735 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