001.
Imports
System.Drawing
002.
Imports
System.Windows.Forms
003.
004.
<ToolboxBitmap(
GetType
(TextBox))> _
005.
Public
Class
usrCtrlMaskTextBox :
Inherits
TextBox
006.
007.
#Region " Private Variables "
008.
Private
WithEvents
_DTP
As
New
DateTimePicker
009.
Private
_Mask
As
String
010.
Private
_Button
As
New
Button()
With
{.Text =
String
.Empty}
011.
#End Region
012.
013.
#Region " Properties "
014.
<System.ComponentModel.DefaultValue(
GetType
(
String
),
"Text"
), System.ComponentModel.Category(
"Appearance"
)> _
015.
Public
Property
Mask()
As
String
016.
Get
017.
Return
_Mask
018.
End
Get
019.
Set
(
ByVal
value
As
String
)
020.
_Mask = value
021.
End
Set
022.
End
Property
023.
024.
Private
mKey
As
String
025.
<System.ComponentModel.DefaultValue(
GetType
(
String
),
"Text"
), System.ComponentModel.Category(
"Appearance"
)> _
026.
Public
Property
_Key()
As
String
027.
Get
028.
Return
mKey
029.
End
Get
030.
Set
(
ByVal
value
As
String
)
031.
mKey = value
032.
End
Set
033.
End
Property
034.
035.
#End Region
036.
037.
#Region " Constructor "
038.
039.
Public
Sub
New
()
040.
AddHandler
_Button.Click,
Sub
(หอย, งาม)
041.
042.
043.
044.
End
Sub
045.
End
Sub
046.
047.
#End Region
048.
049.
Protected
Overrides
Sub
OnKeyDown(e
As
KeyEventArgs)
050.
051.
052.
End
Sub
053.
054.
Private
Function
findDMY(
ByVal
txt
As
String
,
Optional
ByVal
dmy
As
String
=
"m"
)
As
String
055.
Dim
strRet
As
String
=
""
056.
For
i
As
Integer
= 0
To
txt.Length - 1
057.
If
(_Mask(i).ToString()).ToLower() = dmy.ToLower()
Then
058.
strRet &= txt.Substring(i, 1)
059.
End
If
060.
Next
061.
Return
If
(strRet =
""
,
"0"
, strRet)
062.
End
Function
063.
064.
065.
066.
067.
068.
069.
070.
071.
Private
Function
isLeapyear(
ByVal
shrYear
As
Short
)
As
Boolean
072.
Return
Date
.IsLeapYear(shrYear)
073.
End
Function
074.
075.
Protected
Overrides
Sub
OnKeyPress(
ByVal
e
As
System.Windows.Forms.KeyPressEventArgs)
076.
If
AscW(e.KeyChar) = Keys.Back
OrElse
AscW(e.KeyChar) = Keys.Delete
OrElse
AscW(e.KeyChar) = Keys.Left
OrElse
AscW(e.KeyChar) = Keys.Right
Then
077.
e.Handled =
False
078.
Return
079.
End
If
080.
If
Mask <>
String
.Empty
Then
081.
e.Handled =
True
082.
Dim
newText
As
String
=
Me
.Text
083.
Dim
Finished
As
Boolean
=
False
084.
Dim
daysInMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
085.
Dim
mask1
As
String
=
String
.Empty
086.
For
i
As
Integer
=
Me
.SelectionStart
To
_Mask.Length - 1
087.
mask1 = _Mask(i).ToString().ToLower()
088.
If
Not
(
Char
.IsDigit(e.KeyChar))
Then
089.
Return
090.
Else
091.
If
"dmy"
.Contains(mask1)
Then
092.
newText &= e.KeyChar.ToString()
093.
Finished =
True
094.
Else
095.
newText &= mask1
096.
Finished =
False
097.
End
If
098.
End
If
099.
Select
Case
mask1
100.
Case
"d"
101.
Dim
getDay
As
String
= findDMY(newText,
"d"
)
102.
103.
If
(
CByte
(getDay) > 31)
OrElse
(getDay.Length = 1
AndAlso
e.KeyChar >
"3"
)
OrElse
(getDay.Length > 1
AndAlso
getDay.Substring(0, 2) =
"00"
)
Then
104.
Return
105.
End
If
106.
Case
"m"
107.
Dim
getDay
As
String
= findDMY(newText,
"d"
)
108.
Dim
getMonth
As
String
= findDMY(newText,
"m"
)
109.
110.
If
CByte
(getMonth) > 12
OrElse
(getMonth.Length > 1
AndAlso
getMonth.Substring(0, 2) =
"00"
)
Then
111.
Return
112.
End
If
113.
114.
If
CByte
(getMonth) > 0
Then
115.
Dim
lastDayOfMonth
As
Byte
= daysInMonth(getMonth - 1)
116.
If
CByte
(getDay) > lastDayOfMonth
Then
117.
Return
118.
End
If
119.
If
getMonth.Length = 1
AndAlso
CByte
(getMonth) <> 1
Then
120.
getMonth =
"0"
& getMonth
121.
122.
End
If
123.
ElseIf
getMonth.Length > 1
AndAlso
getMonth.Substring(0, 2) =
"00"
Then
124.
Return
125.
End
If
126.
Case
"y"
127.
Dim
getDay
As
String
= findDMY(newText,
"d"
)
128.
Dim
getMonth
As
String
= findDMY(newText,
"m"
)
129.
Dim
getYear
As
String
= findDMY(newText,
"y"
)
130.
If
Not
isLeapyear(
CShort
(getYear))
Then
131.
daysInMonth(1) = 28
132.
End
If
133.
134.
135.
136.
137.
138.
End
Select
139.
If
Finished
Then
140.
Exit
For
141.
End
If
142.
Next
143.
144.
Me
.Text = newText
145.
Me
.SelectionStart =
Me
.Text.Length
146.
End
If
147.
End
Sub
148.
149.
Protected
Overrides
Sub
OnValidating(
ByVal
e
As
System.ComponentModel.CancelEventArgs)
150.
151.
End
Sub
152.
153.
Public
Sub
ResetValue()
154.
MyBase
.Text =
String
.Empty
155.
End
Sub
156.
157.
Protected
Overrides
Sub
OnCausesValidationChanged(e
As
EventArgs)
158.
MyBase
.OnCausesValidationChanged(e)
159.
End
Sub
160.
161.
End
Class