-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTextBoxHelper.cls
117 lines (88 loc) · 2.35 KB
/
TextBoxHelper.cls
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "TextBoxHelper"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'【成员变量】
Private WithEvents m_TextBox As TextBox
Attribute m_TextBox.VB_VarHelpID = -1
Private m_NumberOnly As Boolean
Private m_CancelKey As Boolean
'枚举变量
Public Enum vbKeyType
vbF5 = 116
End Enum
'【公开属性】
Public Property Get NumberOnly() As Boolean
NumberOnly = m_NumberOnly
End Property
Public Property Let NumberOnly(ByVal vNewValue As Boolean)
m_NumberOnly = vNewValue
End Property
Public Property Get Length() As Variant
Length = Len(m_TextBox.Text)
End Property
'【公开方法】
'读取文本
Public Function GetText() As String
GetText = m_TextBox.Text
End Function
'绑定文本框
Public Sub InitTextBox(ByRef txtCtl As TextBox)
Set m_TextBox = txtCtl
End Sub
'选定所有内容
Public Sub SelAll()
With m_TextBox
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
End Sub
'光标移动到末尾
Public Sub PointerToEnd()
LocPointer Length
End Sub
'清空文本框
Public Sub Clear()
m_TextBox.Text = ""
End Sub
'获取选定的文本
Public Function GetSelected()
GetSelected = m_TextBox.SelText
End Function
'【私有方法】
'定位光标
Private Sub LocPointer(ByVal offsetValue As Long)
m_TextBox.SelStart = offsetValue
m_TextBox.SetFocus
End Sub
'【事件绑定】
Private Sub m_TextBox_KeyDown(KeyCode As Integer, Shift As Integer)
Dim txtContent As String
Select Case True
Case NumberOnly
txtContent = m_TextBox.Text
If IsNumeric(txtContent) = False And NumberOnly Then
m_TextBox.Text = Mid(txtContent, 1, Len(txtContent) - 1)
PointerToEnd
End If
Case KeyCode = vbKeyA And Shift = vbCtrlMask
Call SelAll
m_CancelKey = True
End Select
End Sub
Private Sub m_TextBox_KeyPress(KeyAscii As Integer)
If m_CancelKey = True Then
KeyAscii = 0
m_CancelKey = False
End If
End Sub