vb.net e.keychar 不是 system.eventargs 的成员 - Visual Basic 2010

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15077443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:31:44  来源:igfitidea点击:

e.keychar is not a member of system.eventargs - Visual Basic 2010

vb.netvisual-studio-2010

提问by Carlos Lee

I'm trying to block my text boxes in order that they can only accept numbers. I was looking in the Internet and I found this code

我试图阻止我的文本框,以便它们只能接受数字。我在网上找,我找到了这个代码

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) HandlesTextBox1.TextChanged
  If Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
    e.Handled = True
  End If
End Sub  

The thing is that there is an error message displayed:

问题是显示了一条错误消息:

e.keychar is not a member of system.eventargs

e.keychar 不是 system.eventargs 的成员

I already imported the Imports System.EventArgs. Any idea how to fix this?

我已经导入了Imports System.EventArgs. 知道如何解决这个问题吗?

回答by ikudeljnj

I believe the problem lies in the event you're handling. It should probably not be TextChanged. e.KeyCharis usually in the EventArgsfor KeyPresshandlers. Try using something like TextBox1_KeyPress.

我相信问题在于您正在处理的事件。应该不是TextChangede.KeyChar通常在EventArgsforKeyPress处理程序中。尝试使用类似TextBox1_KeyPress.

回答by codingbiz

This should work:

这应该有效:

Public Class Form1
    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyValue < 48 OrElse e.KeyValue > 57 Then _
            e.SuppressKeyPress = True
    End Sub
End Class

回答by Taylor Fielder

It needs to have (sender As Object, e As KeyPressEventArgs) which e.KeyChar is a member of

它需要有 (sender As Object, e As KeyPressEventArgs) 其中 e.KeyChar 是