RaiseEvent中的KeyPress,KeyCode事件定义。
自定义控件中有一个文本框T1,将控件的KeyPress事件和T1的KeyPress
事件关联起来。
结果是只能在按下Enter键时会触发TextKeyPress事件,而且Key=0。
而KeyDown事件用同样的方法触发,会出现内存不能write的错误,VB会被
非法关闭。
Public Event TextKeyPress(Key as Integer)
Private Sub T1_KeyPress(KeyAscii As Integer)
On Err GoTo Err1
Dim k As Integer
k = KeyAscii
RaiseEvent TextKeyPress(k)
'回车移动
If ShowListState = True Then
With Vf1
If k = 13 Then
If .Visible = True And .Row >= 1 Then
T1 = Trim(.TextMatrix(.Row, 1))
.Visible = False
k = 0
End If
End If
End With
End If
'限制提示输入文本框,只能用提示的内容
If ShowListState = True And OptAlwaysList = True Then
If k <> 13 And k <> 40 And k <> 38 Then
k = 0
End If
End If
'回车
If k = 13 Then
SendKeys "{tab}"
k = 0
End If
'退格键
If k = 8 Then
Exit Sub
End If
If k <> 0 Then
Select Case vDataType
Case "Str"
If DataLen(T1.Text & Chr(k)) > StrMaxLenth Then
k = 0
End If
Case "Int"
If Not (k <= Asc("9") And k >= Asc("0")) Then
k = 0
Else
If Not (IsNumeric(T1.Text & Chr(k))) Then
k = 0
End If
End If
Case "Num"
If Not (k <= Asc("9") And k >= Asc("0") Or k = Asc(".") Or k = Asc("-")) Then
k = 0
Else
If Not (IsNumeric(T1.Text & Chr(k))) Then
k = 0
End If
End If
Case "Data"
If Not (k <= Asc("9") And k >= Asc("0")) Then
k = 0
End If
End Select
End If
'RaiseEvent TextKeyPress(KeyAscii)
KeyAscii = k
Exit Sub
Err1:
MsgBox "KeyPress事件:" & KeyAscii & "键入出错"
End Sub