RaiseEvent中的KeyPress,KeyCode事件定义。

zerg2001 2003-09-30 09:32:16
自定义控件中有一个文本框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


...全文
110 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zerg2001 2003-11-19
  • 打赏
  • 举报
回复
很神,过了这么久,终于知道是怎么回事了。
是我自定义的Validate(Cancel as Boolean)事件引发了这次错误。
可是Validate(Cancel as Boolean)事件本身触发时不会出错,
其他的自定义事件Keypress却会引起VB的非法错误。
horsefly 2003-09-30
  • 打赏
  • 举报
回复
单步跟进去走过吗?
RaiseEvent TextKeyPress(k)放到KeyAscii = k前呢?
你在Public Event TextKeyPress(Key as Integer)这各事件中有什么处理吗?
如果没有什么要求的话这些控制代码是否可以考虑放到Public Event TextKeyPress(Key as Integer)这各事件中呢?


banni2003 2003-09-30
  • 打赏
  • 举报
回复
up up!
gzqg 2003-09-30
  • 打赏
  • 举报
回复
up
zerg2001 2003-09-30
  • 打赏
  • 举报
回复
1.单步跟进去走过吗?
可以确定是RaiseEvent引起的错误。

2.RaiseEvent TextKeyPress(k)放到KeyAscii = k前呢?
这个试过了,不行。

3.你在Public Event TextKeyPress(Key as Integer)这各事件中有什么处理吗?

没有处理。

4。如果没有什么要求的话这些控制代码是否可以考虑放到Public Event TextKeyPress(Key as Integer)这各事件中呢?
放到Public Event TextKeyPress(Key as Integer)这各事件中????????
不知道要怎么作。这个只是一个事件定义吧。

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧