Imports System.ComponentModel
Public Class NumericTextBox
Inherits Windows.Forms.TextBox
Public Event InvalidKeyEntered(ByVal sender As NumericTextBox)
Public Event InvalidTextEntered(ByVal sender As NumericTextBox)
Public Event ValueOutOfRange(ByVal sender As NumericTextBox)
Public Event ValueChanged(ByVal sender As NumericTextBox)
Public Event KeyPressEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
#Region " Properties "
Private mAllowInvalidBeforeValidate As Boolean = False
<Browsable(True), _
Description("If true, accepts pasting none-numeric text in the NumericTextBox, before " & _
"the Validate event is fired."), _
Category("Additional Setting"), _
DefaultValue(False)> _
Public Property AllowInvalidBeforeValidate() As Boolean
Get
Return mAllowInvalidBeforeValidate
End Get
Set(ByVal Value As Boolean)
mAllowInvalidBeforeValidate = Value
End Set
End Property
Private mAutoFormat As Boolean = True
<Browsable(True), _
Description("If true, will format the entered value after the TextBox is validated. " & _
"Any extra zeros after floating point will be removed."), _
Category("Additional Setting"), _
DefaultValue(True)> _
Public Property AutoFormat() As Boolean
Get
Return mAutoFormat
End Get
Set(ByVal Value As Boolean)
mAutoFormat = Value
End Set
End Property
Private mAcceptDecimal As Boolean = False
<Browsable(True), _
Description("If true, will accept decimal numbers."), _
Category("Additional Setting"), _
DefaultValue(False)> _
Public Property AcceptDecimal() As Boolean
Get
Return mAcceptDecimal
End Get
Set(ByVal Value As Boolean)
mAcceptDecimal = Value
End Set
End Property
Private mAcceptNegative As Boolean = False
<Browsable(True), _
Description("If true, will accept negative numbers."), _
Category("Additional Setting"), _
DefaultValue(False)> _
Public Property AcceptNegative() As Boolean
Get
Return mAcceptNegative
End Get
Set(ByVal Value As Boolean)
mAcceptNegative = Value
End Set
End Property
Private mMinValue As Double = Double.MinValue
<Browsable(True), _
Description("Minimum value that the NumericTextBox accepts."), _
Category("Additional Setting"), _
DefaultValue(Double.MinValue)> _
Public Property MinValue() As Double
Get
Return mMinValue
End Get
Set(ByVal Value As Double)
mMinValue = Value
End Set
End Property
Private mMaxValue As Double = Double.MaxValue
<Browsable(True), _
Description("Maximum value that the NumericTextBox accepts."), _
Category("Additional Setting"), _
DefaultValue(Double.MaxValue)> _
Public Property MaxValue() As Double
Get
Return mMaxValue
End Get
Set(ByVal Value As Double)
mMaxValue = Value
End Set
End Property
Private mOnOutOfRangeSetToLim As Boolean = True
<Browsable(True), _
Description("If the user enters a value larger than MaxValue, it will be set to" & _
"MaxValue. If a value smaller than MinValue is entered, it will be set" & _
" to MinValue. If False, restores the last valid value,"), _
Category("Additional Setting"), _
DefaultValue(True)> _
Public Property OnOutOfRangeSetToLim() As Boolean
Get
Return mOnOutOfRangeSetToLim
End Get
Set(ByVal Value As Boolean)
mOnOutOfRangeSetToLim = Value
End Set
End Property
#End Region
Private mLastValidText As String
Private Property lastValidText() As String
Get
If mLastValidText Is Nothing Then
If MinValue > 0 Then
mLastValidText = MinValue.ToString
Else
mLastValidText = "0"
End If
End If
Return mLastValidText
End Get
Set(ByVal Value As String)
Value = Value.Trim
If Value = "" Then
Value = lastValidText
ElseIf CLng(mLastValidText) < MinValue Then
mLastValidText = MinValue.ToString
ElseIf CLng(mLastValidText) > MaxValue Then
mLastValidText = MaxValue.ToString
Else
mLastValidText = Value
End If
End Set
End Property
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
If Not AllowInvalidBeforeValidate Then
checkText()
' LastValidText = Me.Text
End If
MyBase.OnTextChanged(e)
End Sub
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim BACK_SPACE As Integer = 8
Dim ENTER As Integer = 13
Dim KeyASCII As Integer = Asc(e.KeyChar)
'Not a number
If KeyASCII <> BACK_SPACE AndAlso KeyASCII <> ENTER AndAlso _
KeyASCII < 48 OrElse KeyASCII > 57 Then
' Accept decimal character, and the negative sign if set to accept them
If Not (AcceptNegative AndAlso e.KeyChar = "-"c) AndAlso _
Not (AcceptDecimal AndAlso e.KeyChar = "."c) Then
' Cancel the entered key, not a valid key
e.Handled = True
RaiseEvent InvalidKeyEntered(Me)
End If
ElseIf KeyASCII = ENTER Then
OnValidated(Nothing)
e.Handled = True
Me.SelectAll()
End If
'RaiseEvent KeyPressEvent(Me, e)
MyBase.OnKeyPress(e)
End Sub
' Checks to see if the entered text is a number
Private Sub checkText()
Me.Text = Me.Text.Trim
If Me.Text = "" Then Exit Sub
Try
Dim tmp As Double = CDbl(Me.Text)
Catch
Me.Text = lastValidText
RaiseEvent InvalidTextEntered(Me)
End Try
End Sub
' Formats the entered number (has to be a number). For example, removes extra zeros
Private Sub formatText()
Try
Dim tmp As Double = CDbl(Me.Text)
Me.Text = tmp.ToString
Catch
MsgBox("DEBUG: WARNING! UNEXPECTED ERROR!", MsgBoxStyle.Critical, "ERROR!!!")
End Try
End Sub
' Changes to see if the entered number is in the valid range (has to be a number).
' Valid range is from MinValue to MaxValue
Private Sub checkRange()
Try
Dim tmp As Double = CDbl(Me.Text)
If tmp < MinValue Then
If mOnOutOfRangeSetToLim Then
Me.Text = MinValue.ToString
Else
Me.Text = lastValidText
RaiseEvent ValueOutOfRange(Me)
End If
ElseIf tmp > MaxValue Then
If mOnOutOfRangeSetToLim Then
Me.Text = MaxValue.ToString
Else
Me.Text = lastValidText
RaiseEvent ValueOutOfRange(Me)
End If
End If
Catch
MsgBox("DEBUG: WARNING! UNEXPECTED ERROR!", MsgBoxStyle.Critical, "ERROR!!!")
End Try
End Sub
Protected Overrides Sub OnValidated(ByVal e As System.EventArgs)
checkText()
If Me.Text = "" Then
Me.Text = lastValidText
End If
checkRange()
If AutoFormat Then formatText()
lastValidText = Me.Text
RaiseEvent ValueChanged(Me)
MyBase.OnValidated(e)
End Sub
Public Sub New()
' Since lastValidText is not set yet, the property will generate a
' default value. See the lastValidText property
Me.Text = mLastValidText
End Sub
End Class