如何在vb .net中按下大写锁定键时选中复选框

时间:2016-06-24 09:44:47

标签: vb.net keypress

我尝试使用vb .net开发一个程序,当按下大写锁定键时,该程序可以选中或取消选中复选框。我使用下面的代码来做同样的事情,但它没有用。

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.CapsLock) Then
        checkbutton_caps.Checked = True
    End If

上面的代码有什么问题?

3 个答案:

答案 0 :(得分:2)

这两个代码都可以使用。

➤设置 KeyPreview = True

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.CapsLock Then
        checkbutton_caps.Checked = True            
    End If
End Sub

➤不使用 KeyPreview 属性(只需将此代码添加到您的程序中)

Protected Overrides Function ProcessCmdKey(ByRef Msg As Message, _
                                           ByVal Key As Keys) _
                                           As Boolean
    If Msg.WParam = Keys.CapsLock Then
       checkbutton_caps.Checked = True           
       Return True
    End If
       Return Me.ProcessCmdKey(Msg, Key)
End Function

提示:再次按下该键时,请使用此代码取消选中checkbox

checkbutton_caps.Checked = Not checkbutton_caps.Checked

'Instead of...

checkbutton_caps.Checked = True 

答案 1 :(得分:0)

Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic

Public Function GetCapsLockState() As Boolean
    If Control.IsKeyLocked(Keys.CapsLock) Then
        Return True
    Else
        Return False
    End If
End Function

If GetCapsLockState Then
    checkbutton_caps.Checked = True
End If

答案 2 :(得分:0)

Capital

CapsLockMe.KeyPreview = True都可以使用。

还要记住在代码或属性中设置RewriteEngine On RewriteBase /kurumsal/ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^index/sayfa/([^/]*)/page/([^/]*)/$ /kurumsal/index.php?sayfa=$1&page=$2 [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^index/sayfa/([^/]*)/$ /kurumsal/index.php?sayfa=$1 [L]

这是关于keydown vs keypress

的有用研究