为什么e.Keychar不在keypress事件中工作?

时间:2014-05-25 06:29:32

标签: vb.net

我对KeyChar e.KeyChar有疑问。它不起作用。我希望从中获得e的价值,请看一下,我们非常感谢您的评论。

Private Sub txtTax_KeyPress(ByVal sender As System.Object, 
             ByVal e As System.EventArgs) Handles txtTax.KeyPress
        If (Strings.Asc(e.KeyChar) > 0) Then
            Try
                If (Me.conn.State = ConnectionState.Closed) Then
                    Me.conn.Open()
                End If
                Dim adapter As New SqlDataAdapter(("Select * from TaxGroup where TaxName='" & Me.txtTax.Text & "'"), Me.conn)
                Dim dataSet As New DataSet
                adapter.Fill(dataSet, "TaxGroup")
                Me.dgrdTax.DataSource = dataSet.Tables.Item(0)
                Me.dgrdTax.Visible = True
            Catch exception1 As Exception
                ProjectData.SetProjectError(exception1)
                Dim exception As Exception = exception1
                Interaction.MsgBox(exception.Message, MsgBoxStyle.ApplicationModal, Nothing)
                ProjectData.ClearProjectError()
            End Try
        End If
    End Sub

2 个答案:

答案 0 :(得分:2)

您的活动声明有误。正确的签名是:

 Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) 
                                Handles TextBox1.KeyPress

KeyChar不是EventArgs的一部分,它基本上只是一个基类。

我猜也许你的想法你需要自己打字。只需从左下方选择控件,然后从右侧选择事件,IDE将为您创建控件。我不确定如何发生这种情况。

答案 1 :(得分:0)

你也可以试试这个:

If Not (Asc(e.KeyChar) = 8) Then
        Dim allowedChars As String = "1234567890" ' allowed In textbox
        If Not allowedChars.Contains(e.KeyChar.ToString.ToLower) Then
            e.KeyChar = ChrW(0)
            e.Handled = True
        End If
    End If