使用函数时OnKeyPress事件无法正常工作 [英] OnKeyPress event is not working properly when using a function

查看:115
本文介绍了使用函数时OnKeyPress事件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在尝试创建一个常用函数来检查小数值。如果我把代码保持在内部甚至调用,它工作正常,但如果我移动到一个函数并从事件过程调用该函数它不工作。如果我键入任何字母,它将不会在第一种情况下在文本
字段中添加,但在第二种情况下添加。你能帮忙纠正这个错误吗?请参阅下面的代码。

I am trying to make a common function to check for decimal values. If I keep the code inside even call, it works fine but if I move to a function and call the function from event procedure its not working. If I type any letter it wont be added in the text field in first case but adding in second case. Could you please help to rectify this error? Please see the code as below.

Private Sub txtFdxStd_KeyPress(KeyAscii As Integer)

    CheckForDecimalsOnKeyPress(KeyAscii)

End Sub

Private Sub txtFdxStd_KeyPress(KeyAscii As Integer)
    CheckForDecimalsOnKeyPress (KeyAscii)
End Sub

******

非常感谢您提前

Jose

推荐答案


全部

Hi All

我正在努力建立一个共同的功能检查小数值。如果我把代码保持在内部甚至调用,它工作正常,但如果我移动到一个函数并从事件过程调用该函数它不工作。如果我键入任何字母,它将不会在第一种情况下在文本
字段中添加,但在第二种情况下添加。你能帮忙纠正这个错误吗?请参阅下面的代码。

I am trying to make a common function to check for decimal values. If I keep the code inside even call, it works fine but if I move to a function and call the function from event procedure its not working. If I type any letter it wont be added in the text field in first case but adding in second case. Could you please help to rectify this error? Please see the code as below.

Private Sub txtFdxStd_KeyPress(KeyAscii As Integer)

    CheckForDecimalsOnKeyPress(KeyAscii)

End Sub

Private Sub txtFdxStd_KeyPress(KeyAscii As Integer)
    CheckForDecimalsOnKeyPress (KeyAscii)
End Sub

******

在CheckForDecimalsOnKeyPress调用中删除KeyAscii周围的括号。 取而代之的是:

Remove the parenthese around KeyAscii in the call to CheckForDecimalsOnKeyPress.  Instead of this:

    CheckForDecimalsOnKeyPress(KeyAscii)

    CheckForDecimalsOnKeyPress (KeyAscii)

写下这个:

    CheckForDecimalsOnKeyPress KeyAscii

    CheckForDecimalsOnKeyPress KeyAscii

如果在不需要变量名时将括号括起来(如本例所示),则会导致变量名称被评估,并且该评估的结果为传递到该功能。

If you put parentheses around the variable name when they aren't required (as in this case), you cause the variable name to be evaluated and the result of that evaluation to be passed to the function in its place.

顺便说一下,我没有看到该行的重点

By the way, I don't see the point of the line

    CheckAsDecimalsOnKeyPress中的KeyAscii = KeyAscii

    KeyAscii = KeyAscii

。 你为什么不写:

in CheckForDecimalsOnKeyPress.  Why don't you just write:

Public Function CheckForDecimalsOnKeyPress(ByRef KeyAscii As Integer) As Integer

    If (KeyAscii > 47 And KeyAscii < 58) Or (KeyAscii = 8) Or (KeyAscii = 46) Then
        ' The key is okay, so leave it alone.
    Else
        KeyAscii = 0
        MsgBox ("You Must Enter Numbers Only!")
    End If
    
End Function





这篇关于使用函数时OnKeyPress事件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆