, stackoverflow , , : - , , . Ctrl + C .
, , KeyDown/Up; TextChanged : , ; else: .
- - : o |... TextBox, ApplyValue, TextChanged Validated, , , .
'nuff , , stackoverflow - ; o) , - !
Imports System.ComponentModel
Public Class RestrictedTextbox
Inherits Windows.Forms.TextBox
Public Sub New()
MyBase.New()
End Sub
'use enum instead of flags because of the overhead for creating a UiTypeEditor
Public Enum StyleEnum
[Integer]
IntegerZero
[Double]
DoubleZero
TextOnChange
TextOnValidate
End Enum
Public Event ApplyValue(sender As Object, e As System.EventArgs)
#End Region
#Region "New Properties"
Private _style As StyleEnum = StyleEnum.TextOnValidate
<Category("Restrictions"), _
Description("Determines input validation, alignment and when the ApplyValue event is raised"), _
Browsable(True), DefaultValue(StyleEnum.TextOnValidate)> _
Public Shadows Property Style As StyleEnum
Get
Return _style
End Get
Set(value As StyleEnum)
_style = value
initializeText()
End Set
End Property
Private _min As Integer = 0
<Category("Restrictions"), _
Description("Minimum value (for numeric styles)"), _
Browsable(True), DefaultValue(0)> _
Public Property Minimum As Integer
Get
Return _min
End Get
Set(value As Integer)
_min = value
initializeText()
End Set
End Property
Private _max As Integer = 2147483647
<Category("Restrictions"), _
Description("Maximum value (for numeric styles)"), _
Browsable(True), DefaultValue(2147483647)> _
Public Property Maximum As Integer
Get
Return _max
End Get
Set(value As Integer)
_max = value
initializeText()
End Set
End Property
#End Region
#Region "Shadow properties"
'hide and do not allow changing
<Browsable(False), EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Shadows Property TextAlign As Windows.Forms.HorizontalAlignment
Get
Return MyBase.TextAlign
End Get
Set(value As Windows.Forms.HorizontalAlignment)
'do nothing
End Set
End Property
'hide and always use false
<Browsable(False), EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Shadows Property Multiline As Boolean
Get
Return False
End Get
Set(value As Boolean)
MyBase.Multiline = False
End Set
End Property
'hide and always use true
<Browsable(False), EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Shadows Property CausesValidation As Boolean
Get
Return True
End Get
Set(value As Boolean)
MyBase.CausesValidation = True
End Set
End Property
'hide, but emulate default
<Browsable(False), EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Shadows Property Lines As String()
Get
Return MyBase.Lines
End Get
Set(value As String())
MyBase.Lines = value
End Set
End Property
Private _oldText As String = ""
Private nfi As System.Globalization.NumberFormatInfo = New System.Globalization.NumberFormatInfo With {.CurrencyDecimalSeparator = "."}
Private Sub initializeText()
Dim def As String = ""
Select Case _style
Case StyleEnum.TextOnChange, StyleEnum.TextOnValidate
MyBase.TextAlign = HorizontalAlignment.Left
def = ""
Case StyleEnum.Double, StyleEnum.Integer
MyBase.TextAlign = HorizontalAlignment.Right
def = ""
Case StyleEnum.DoubleZero, StyleEnum.IntegerZero
MyBase.TextAlign = HorizontalAlignment.Right
If _min < 0 And _max > 0 Then
def = "0"
Else
def = _min.ToString
End If
End Select
If Me.Text = "" Or Me.Text = "0" Or Not Me.validateText Then
Me.Text = def
End If
End Sub
Private Function validateText() As Boolean
Dim negativeOk As Boolean = False
Dim checkDouble As Boolean = False
Dim checkInteger As Boolean = False
Dim valueOk As Boolean = False
Select Case _style
Case StyleEnum.Double, StyleEnum.DoubleZero
checkDouble = True
Case StyleEnum.Integer, StyleEnum.IntegerZero
checkInteger = True
Case StyleEnum.TextOnChange, StyleEnum.TextOnValidate
valueOk = True
End Select
If Not valueOk Then
Dim txt As String = Me.Text
If String.IsNullOrEmpty(txt) Then
valueOk = True
ElseIf _min < 0 And txt = "-" Then
valueOk = True
Else
Dim tmp As Double = 0
If checkDouble Then
valueOk = Double.TryParse(txt, Globalization.NumberStyles.Float, nfi, tmp)
ElseIf checkInteger Then
valueOk = Integer.TryParse(txt, Globalization.NumberStyles.Float, nfi, tmp)
End If
If valueOk And ((tmp > _max) Or ((tmp < _min) And (tmp.ToString.Length >= _min.ToString.Length))) Then
'if value could be parsed, but
'value is too large or
'value is too small, even though the length is sufficient (to allow entering incomplete numbers, e.g. "2", if _min=10)
'NOTE: too small numbers will be caught in validation event
valueOk = False
End If
End If
End If
Return valueOk
End Function
Private Sub WdTextbox_TextChanged(sender As Object, e As EventArgs) Handles Me.TextChanged
Dim valueOk As Boolean = Me.validateText
If Not valueOk Then
Dim oldPos As Integer = Me.SelectionStart
Me.Text = _oldText
If (oldPos > 0) Then
Me.SelectionStart = oldPos - 1
End If
Else
Me._oldText = Text
If Me._style = StyleEnum.TextOnChange Then
RaiseEvent ApplyValue(Me, New System.EventArgs)
End If
End If
End Sub
Private Sub WdTextbox_Validating(sender As Object, e As CancelEventArgs) Handles Me.Validating
Dim txt As String = Me.Text
Dim raise As Boolean
Select Case _style
Case StyleEnum.Double, StyleEnum.Integer
Dim tmp As Double = 0
If Double.TryParse(txt, Globalization.NumberStyles.Float, nfi, tmp) Then
If tmp < _min Then
tmp = _min
ElseIf tmp > _max Then
tmp = _max
End If
Me.Text = IIf(tmp <> 0, tmp.ToString, "")
Else
Me.Text = ""
End If
raise = True
Case StyleEnum.DoubleZero, StyleEnum.IntegerZero
Dim tmp As Double = 0
If Double.TryParse(txt, Globalization.NumberStyles.Float, nfi, tmp) Then
If tmp < _min Then
tmp = _min
ElseIf tmp > _max Then
tmp = _max
End If
Me.Text = tmp.ToString
Else
Me.Text = "0"
End If
raise = True
Case StyleEnum.TextOnChange
raise = False
Case StyleEnum.TextOnValidate
raise = True
End Select
If raise Then
RaiseEvent ApplyValue(Me, New System.EventArgs)
End If
End Sub
End Class