vb.net在表单上检测按键

问题描述 投票:0回答:1

我是vb.net的新手,正在尝试检测表单上的KeyPress我在JavaFX中通过创建一个侦听器来完成此操作,该侦听器在按下ESC键时关闭应用程序我没有在vb.net中找到任何使用侦听器的代码示例我已经找到处理TextBox的KeyPress的代码,但为Form FAILS处理相同的代码对于此功能,可以从任何表单关闭应用程序,我想知道是否需要在模块中声明它?虽然很高兴知道问题的这一部分,但称其为奖励我的问题是为什么这段代码没有在frmOne上检测到按键?检测txtBoxOne中的按键的代码按预期运行]

Public Class frmOne
Private Sub frmOne_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
    'frmOne Property FixedToolWindow
    'frmOne is the Start Up Form
    If Asc(e.KeyChar) > 1 Then
        MessageBox.Show("You Pressed " & e.KeyChar)
    End If
    'If Asc(e.KeyChar) > 1 Then txtBoxOne.Text = "You Pressed"
End Sub

Private Sub txtBoxOne_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtBoxOne.KeyPress
    If Asc(e.KeyChar) = 13 Then
        e.Handled = True
        MsgBox("Error.")
    Else
        e.Handled = False
    End If
End Sub
Private Sub btnToFormTwo_Click(sender As Object, e As EventArgs) Handles btnToFormTwo.Click
    Dim i As Integer
    i = txtBoxOne.Text.Length
    If i = 0 Then
        'txtBoxOne.Text = "Enter"
        MessageBox.Show("Enter Data")
        txtBoxOne.Select()
        Return
    End If
    Dim OBJ As New frmTwo
    OBJ.SPass = txtBoxOne.Text
    OBJ.Show()
    'MyTextBox_Enter()
    txtBoxOne.Clear()
    Me.Hide()
    'Me.Close()'R Click project PassVar Set Start Up Form
    'Best Solution is to have Splash Form as Start Up Form
End Sub
Public Sub MyTextBox_Enter()
    txtBoxOne.Clear()
End Sub
'Private Sub frmOne_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Use code below if pre populated text in txtBoxOne
'Me.ActiveControl = txtBoxOne
'txtBoxOne.Select(txtBoxOne.Text.Length, 0)
'txtBoxOne.Select()

'End Sub

结束类

vb.net keypress
1个回答
0
投票

相同的代码将适用于表单,但是如果子控件具有焦点,则默认情况下,表单不会引发键盘事件。您需要将表单的KeyPreview属性设置为True,在这种情况下,表单将在活动子控件之前引发这些键盘事件。

© www.soinside.com 2019 - 2024. All rights reserved.