Public Class numericTextbox

    Inherits TextBox

    Const WM_PASTE As Integer = &H302

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Dim strText As String = Me.Text
        strText = strText.Remove(Me.SelectionStart, Me.SelectionLength)
        strText = strText.Insert(Me.SelectionStart, e.KeyChar)
        e.Handled = CBool(strText.LastIndexOf("-") > 0) Or Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar) OrElse (e.KeyChar = "."c And Not Me.Text.Contains(".") Or e.KeyChar = "."c And Me.SelectedText.Contains(".")) OrElse (e.KeyChar = "-"c And Me.SelectionStart = 0))
        MyBase.OnKeyPress(e)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_PASTE Then
            Dim strText As String = Me.Text
            strText = strText.Remove(Me.SelectionStart, Me.SelectionLength)
            strText = strText.Insert(Me.SelectionStart, Clipboard.GetText)
            Dim result As Decimal = 0
            If Not Decimal.TryParse(strText, result) Then
                Return
            End If
        End If
        MyBase.WndProc(m)
    End Sub

End Class
