sábado, 25 de maio de 2013

on

Public Class Form1

    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 TextBox1.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 TextBox1.KeyPress

        If acceptableKey = False Then       
            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
                TextBox1.Text = ""
            ElseIf strCurrency.Length = 1 Then
                TextBox1.Text = "0.0" & strCurrency
            ElseIf strCurrency.Length = 2 Then
                TextBox1.Text = "0." & strCurrency
            ElseIf strCurrency.Length > 2 Then
                TextBox1.Text = strCurrency.Substring(0, strCurrency.Length - 2) & "." & strCurrency.Substring(strCurrency.Length - 2)
            End If
            TextBox1.Select(TextBox1.Text.Length, 0)

        End If
        e.Handled = True

    End Sub

End Class



Espero ter ajudado... Abraços!

0 comentários:

Postar um comentário