Mark Nartea
Mark Nartea

Reputation: 67

Formatting a number with commas and cents while typing in textbox

Pro devs, I want to ask questions on you. I expect you could help me. My question is: I have a Textbox named "txtUnitPrice" what I need is when I type a number on it it will auto-generate the commas and decimals of that number, example: when I type a number "4" it will display "4.00" and when I type "1000" it will display "1,000.00" how can I do that, please help me.

I tried many questions here that are related to my problem but none of them can help me.

Here's my code:

Private Sub txtUnitPrice_TextChanged(sender As Object, e As EventArgs) Handles txtUnitPrice.TextChanged
    Dim input As Double = Double.Parse(txtUnitPrice.Text)
    Dim inputNumWithCommas As String = input.ToString("N0", CultureInfo.InvariantCulture)
    txtUnitPrice.Text = inputNumWithCommas
End Sub

The code above is not my desired output. Please help me thanks.

Upvotes: 2

Views: 1329

Answers (1)

K.Madden
K.Madden

Reputation: 210

For now a easy way would be to add a button under the textbox and then use this for when the button is pressed and after you input a number to txtUnitPrice

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        txtUnitPrice.Text = FormatCurrency(txtUnitPrice.Text)
End Sub

Another way which will update in real time without the need of a button would be something similar to this

Dim strCurrency As String = ""
Dim acceptableKey As Boolean = False

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUnitPrice.KeyDown
        If (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9) OrElse (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) OrElse e.KeyCode = Keys.Back Then
            acceptableKey = True
        Else
            acceptableKey = False
        End If
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUnitPrice.KeyPress
        ' Check for the flag being set in the KeyDown event.
        If acceptableKey = False Then
            ' Stop the character from being entered into the control since it is non-numerical.
            e.Handled = True
            Return
        Else
            If e.KeyChar = Convert.ToChar(Keys.Back) Then
                If strCurrency.Length > 0 Then
                    strCurrency = strCurrency.Substring(0, strCurrency.Length - 1)
                End If
            Else
                strCurrency = strCurrency & e.KeyChar
            End If

            If strCurrency.Length = 0 Then
                txtUnitPrice.Text = ""
            ElseIf strCurrency.Length = 1 Then
                txtUnitPrice.Text = "0.0" & strCurrency
            ElseIf strCurrency.Length = 2 Then
                txtUnitPrice.Text = "0." & strCurrency
            ElseIf strCurrency.Length > 2 Then
                txtUnitPrice.Text = strCurrency.Substring(0, strCurrency.Length - 2) & "." & strCurrency.Substring(strCurrency.Length - 2)
            End If
            txtUnitPrice.Select(TextBox1.Text.Length, 0)

        End If
e.Handled = True
    End Sub

May want to change a few things but ill leave that up to you to play with.

Upvotes: 1

Related Questions