2017-10-19 38 views
0

我有这样的代码:如何转换变量翻一番(键值)中的KeyDown在Visual Basic

Private Sub key(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown 
    If e.KeyValue = Keys.Enter Then 
     If FuSt = False Then 
      FuSt = True 
      My.Computer.Audio.Play(My.Settings.Full) 
     Else 
      FuSt = False 
      My.Computer.Audio.Play(DIRECOTRY) 
     End If 
    End If 
    If e.KeyValue = b1 Then 
     My.Computer.Audio.Play(SoundsDir & "1.wav") 
    End If 
End Sub 

但是,当我想玩B1(Z键在键盘上),我有这样的错误:

Conversation from "z" to Double is incorrect (Somethink like that)

你能帮我吗?

+0

在比较中混合字符串和枚举不是一个好主意。而是使用KeyPress事件来检测类似“z”的键。 –

+1

用'Keys.Z'替换'b1'? – PerpetualStudent

回答

0

将键分配给变量b1尝试如下所述。

Keys是一个枚举。

Dim b1 As Keys 
b1 = Keys.Z 

汉斯帕桑特评论,如果你想使用KeyPress事件(推荐),试试下面的代码。

Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress 
     If e.KeyChar = Char.ToLower(ChrW(Keys.Z)) Then 
      MsgBox("Z selecetd") 
     End If 
    End Sub 

,或者如果你想要去的KeyDown事件,试试下面的代码。

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown 
    If e.KeyCode.Equals(Keys.Z) Then 
     MsgBox("Z selecetd") 
    End If 
End Sub 
+0

雅,我知道,但我想Keys.z是在变量b1 – Metexu

+0

键是一个枚举....所以你可以尝试.....昏暗b1作为键..... – Naidu

+0

然后,而分配。 ... b1 = Keys.Z – Naidu