求个vb程序写法

来源:百度知道 编辑:UC知道 时间:2024/06/06 16:38:36
本人vb菜鸟
我想写一个摁住一个键,图片就会一直移动的程序
先把timer的interval设成50,在这样写:
Dim vx As Integer

Private Sub Picture1_KeyPress(KeyAscii As Integer)
If KeyCode = 39 Then vx = 100

End Sub

Private Sub Timer1_Timer()
Picture1.Left = Picture1.Left + vx
End Sub
自己感觉很对但是它运行时没动啊,那该怎么写,我的又怎么不对呢,高人来解答下,谢谢。

KeyPress事件用到的参数是KeyAscii 你用Keycode=39当然没用啦!! Keycode是keydown和keyup得参数额
修改:
if keyascii=39 then vx=100'具体要用哪个键控制自己设定键的keyascii值
另外建议你不要用Vx=100来控制 因为这样即使你不按这个键了Vx还是等于100
图片还是移动
这样改:(用Keydown和KeyUp事件)
Dim Move_picture As Boolean

Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 39 Then
Move_picture = True
End If
End Sub

Private Sub Picture1_KeyUp(KeyCode As Integer, Shift As Integer)
Move_picture = False
End Sub

Private Sub Timer1_Timer()
If Move_picture = True Then
Picture1.Left = Picture1.Left + 100
End If
End Sub

Private Sub Picture1_KeyPress(KeyAscii As Integer)
If KeyCode = 39 Then vx = 100
Timer1.Enabled = TRUE '加上这个
End Sub

'这样就可以了,用不到Timer1

Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = 37 Then Picture1.Left = Picture1.Left - 100 '左