设置窗体的keypreview属性为TRUEDim flag As BooleanPrivate Sub Command1_Click() Dim i As Long flag = True Do i = i + 1 DoEvents Loop While flag = True End SubPrivate Sub Form_KeyPress(KeyAscii As Integer) If KeyAscii = 97 Then ' 按下A键停止循环 flag = False End IfEnd Sub
在循环中检查这个变量的值
如果是某一特定值时
退出循环在键盘事件中检查
如果按下的是某个键
把变量值改变成退出循环的值别忘了在循环中加
doevents语句
Dim i As Long
flag = True
Do
i = i + 1
DoEvents
Loop While flag = True
End SubPrivate Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 97 Then ' 按下A键停止循环
flag = False
End IfEnd Sub
do循环用用exit do
前提是有doevents这句