做了个数值输入文本框(支持类型、最大值、最小值),尚不支持拷贝、剪切、粘帖,请指教。
工作需要,制作了一个在Winform下的数值文本输入框,可以限制输入的内容符合数值要求,原本一直用正则表达式做这个,包括IP地址、手机号码等,但发现在数值输入时用正则比较耗费资源,固改成这个。不过在IP地址、手机号码等还是得继续用正则,而且必须要一个一个的字符进行判断,有时间再贴出来。如有改进的请务必回复,供我和大家参考学习,多谢多谢!
尤其是对剪切、粘帖、拷贝的支持,本人目前还毫无头绪。
Public Class MyTextBox Inherits TextBox Private _InputNumericType As NumericType Private _MMaxValue As Double Private _MMinValue As Double Private _MaxValue As Double Private _MinValue As Double Private _TryParse As StringValidate Sub New(Optional ByVal inputformat As NumericType = NumericType.Integer) Me.InputNumericType = inputformat Me.ShortcutsEnabled = False End Sub Private Overloads Sub MyBase_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress Try If Char.IsControl(e.KeyChar) Then e.Handled = False ElseIf Char.IsDigit(e.KeyChar) OrElse e.KeyChar = "." OrElse e.KeyChar = "+" OrElse e.KeyChar = "-" Then Dim ValidString = Strings.Left(Me.Text, MyBase.SelectionStart) & e.KeyChar & Strings.Right(Me.Text, MyBase.Text.Length - Me.SelectionStart - Me.SelectionLength) If ValidString = "+" AndAlso Me._MaxValue > 0 Then e.Handled = False Exit Sub End If If ValidString = "-" AndAlso Me._MinValue < 0 Then e.Handled = False Exit Sub End If If ValidString.Length > 1 AndAlso _InputNumericType < NumericType.Single AndAlso (Strings.Left(ValidString, 1) = "0" OrElse Strings.Left(ValidString.Trim, 2) = "+0" OrElse Strings.Left(ValidString.Trim, 2) = "-0") Then e.Handled = True Exit Sub End If If ValidString.Length > 1 AndAlso _InputNumericType > NumericType.Int64 AndAlso (ValidString.Chars(0) = "0" AndAlso ValidString.Chars(1) <> ".") Then e.Handled = True Exit Sub End If If ValidString.Length > 2 AndAlso _InputNumericType > NumericType.Int64 AndAlso ((Strings.Left(ValidString, 2) = "+0" OrElse Strings.Left(ValidString.Trim, 2) = "-0") AndAlso ValidString.Chars(2) <> ".") Then e.Handled = True Exit Sub End If Dim DataValue As Double If _TryParse(ValidString, DataValue) Then If Not IsNothing(DataValue) AndAlso DataValue <= _MaxValue AndAlso DataValue >= _MinValue Then e.Handled = False Else e.Handled = True End If Else e.Handled = True End If Else e.Handled = True End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub Public Property InputNumericType As NumericType Set(ByVal value As NumericType) Try Select Case value Case NumericType.Byte _TryParse = New StringValidate(AddressOf Byte.TryParse) _MMaxValue = Byte.MaxValue _MMinValue = Byte.MinValue Case NumericType.SByte _TryParse = New StringValidate(AddressOf SByte.TryParse) _MMaxValue = SByte.MaxValue _MMinValue = SByte.MinValue Case NumericType.Short, NumericType.Int16 _TryParse = New StringValidate(AddressOf Short.TryParse) _MMaxValue = Short.MaxValue _MMinValue = Short.MinValue Case NumericType.UShort, NumericType.UInt16 _TryParse = New StringValidate(AddressOf UShort.TryParse) _MMaxValue = UShort.MaxValue _MMinValue = UShort.MinValue Case NumericType.Integer, NumericType.Int32 _TryParse = New StringValidate(AddressOf Integer.TryParse) _MMaxValue = Integer.MaxValue _MMinValue = Integer.MinValue Case NumericType.Uinteger, NumericType.UInt32 _TryParse = New StringValidate(AddressOf UInteger.TryParse) _MMaxValue = UInteger.MaxValue _MMinValue = UInteger.MinValue Case NumericType.Long, NumericType.Int64 _TryParse = New StringValidate(AddressOf Long.TryParse) _MMaxValue = Long.MaxValue _MMinValue = Long.MinValue Case NumericType.Ulong, NumericType.UInt64 _TryParse = New StringValidate(AddressOf ULong.TryParse) _MMaxValue = ULong.MaxValue _MMinValue = ULong.MinValue Case NumericType.Single _TryParse = New StringValidate(AddressOf Single.TryParse) _MMaxValue = Single.MaxValue _MMinValue = Single.MinValue Case NumericType.Double _TryParse = New StringValidate(AddressOf Double.TryParse) _MMaxValue = Double.MaxValue _MMinValue = Double.MinValue Case NumericType.Decimal _TryParse = New StringValidate(AddressOf Decimal.TryParse) _MMaxValue = Decimal.MaxValue _MMinValue = Decimal.MinValue End Select _MaxValue = _MMaxValue _MinValue = _MMinValue Dim DataValue As Double If _TryParse(Me.Text, DataValue) Then If DataValue >= _MinValue AndAlso DataValue <= _MaxValue Then Me.Text = Me.Text Else Me.Text = "" End If Else Me.Text = "" End If _InputNumericType = value Catch ex As Exception MsgBox(ex.Message) End Try End Set Get Return _InputNumericType End Get End Property Public Property MaxValue As Double Set(ByVal value As Double) If value <= _MMaxValue AndAlso value >= _MMinValue AndAlso value >= _MinValue Then _MaxValue = value Else _MaxValue = _MMaxValue End If End Set Get Return _MaxValue End Get End Property Public Property MinValue As Double Set(ByVal value As Double) If value >= _MMinValue AndAlso value <= _MMaxValue AndAlso value <= _MaxValue Then _MinValue = value Else _MinValue = _MMinValue End If End Set Get Return _MinValue End Get End Property Public Overrides Property [Text] As String Set(ByVal value As String) If String.IsNullOrWhiteSpace(value) Then MyBase.Text = value Exit Property End If Dim DataValue As Double If _TryParse(value, DataValue) Then If DataValue >= _MinValue AndAlso DataValue <= _MaxValue Then MyBase.Text = value End If End If End Set Get Return MyBase.Text End Get End Property Delegate Function StringValidate(ByVal Str As String, ByRef result As Object) As BooleanEnd ClassPublic Enum NumericType [SByte] [Byte] [Short] [Int16] [UShort] [UInt16] [Integer] [Int32] [Uinteger] [UInt32] [Long] [Int64] [Ulong] [UInt64] [Single] [Double] [Decimal]End Enum