-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextBoxNumeric.vb
More file actions
73 lines (64 loc) · 2.76 KB
/
TextBoxNumeric.vb
File metadata and controls
73 lines (64 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Public Class TextBoxNumeric
Inherits TextBox
Private _MinValue As Single = Single.MinValue, _MaxValue As Single = Single.MaxValue, _Mask As String = ""
Public Sub New()
TextAlign = HorizontalAlignment.Right
End Sub
<ComponentModel.Category("A")>
Public Property Mask() As String
Get
Return _Mask
End Get
Set(ByVal value As String)
_Mask = value
End Set
End Property
<ComponentModel.Category("A")>
Public Property MinValue() As Single
Get
Return _MinValue
End Get
Set(ByVal value As Single)
_MinValue = value
End Set
End Property
<ComponentModel.Category("A")>
Public Property MaxValue() As Single
Get
Return _MaxValue
End Get
Set(ByVal value As Single)
_MaxValue = value
End Set
End Property
Public Function TextSet(pValue As String, provider As IFormatProvider) As String
Dim tmpSingle As Single, tmpError As String
tmpError = ValidateNumber(pValue, tmpSingle, provider)
TextSet(tmpSingle)
Return tmpError
End Function
Public Sub TextSet(pValue As Single)
MyBase.Text = pValue.ToString(Mask)
End Sub
Public Function TextGet(Optional ByRef pErrors As String = "") As Single
Dim pResult As Single = 0
Dim res As String = $"Instead of «{MyBase.Text}», write a number between {MinValue.ToString(Mask)} and {MaxValue.ToString(Mask)}" & vbCrLf
If Not Single.TryParse(MyBase.Text, Globalization.NumberStyles.Any, provider:=Nothing, result:=pResult) _
OrElse pResult < MinValue OrElse pResult > MaxValue Then
pErrors &= vbCrLf & res
End If
Return pResult
End Function
Public Function Validate(Optional ByRef pResult As Single = 0) As String
Dim res As String = $"Instead of «{MyBase.Text}», write a number between {MinValue.ToString(Mask)} and {MaxValue.ToString(Mask)}" & vbCrLf
If Not Single.TryParse(MyBase.Text, Globalization.NumberStyles.Any, provider:=Nothing, result:=pResult) Then Return res
If pResult < MinValue OrElse pResult > MaxValue Then Return res
Return ""
End Function
Public Function ValidateNumber(pText As String, Optional ByRef pResult As Single = 0, Optional provider As IFormatProvider = Nothing) As String
Dim res As String = $"Instead of «{pText}», write a number between {MinValue.ToString(Mask)} and {MaxValue.ToString(Mask)}" & vbCrLf
If Not Single.TryParse(pText, Globalization.NumberStyles.Any, provider:=provider, result:=pResult) Then Return res
If pResult < MinValue OrElse pResult > MaxValue Then Return res
Return ""
End Function
End Class