我在command_click中有一个循环,希望按键盘上的任何一个键都可以使该循环中断,我该怎么编程?

解决方案 »

  1.   

    在通用部分设一个变量
    private exitCL as bool...在窗何的keypress事件中加入
    exitCL=true在循环中加入下列代码:
    doevents
    if exitCL=true then
       exitCL=false
       exit for
    end if
      

  2.   

    DO EVENTS吧!好像是!具体代码要找了!
      

  3.   

    Dim bln As BooleanPrivate Sub Command1_Click()
        Do While bln
            Cls
            For i = 1 To 20
                Print i
            Next
            DoEvents
        Loop
    End SubPrivate Sub Command1_KeyPress(KeyAscii As Integer)
        bln = False
    End SubPrivate Sub Form_KeyPress(KeyAscii As Integer)
        bln = False
    End SubPrivate Sub Form_Load()
        bln = True
    End Sub
      

  4.   

    真正的标准答案dim bStop as booleanPrivate Sub Form_Load()
    me.keypreview = true    '截获所有控件上的键盘动作
    End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)  '相应所有的键
    bStop = true
    End SubPrivate Sub Command1_Click()
    do 
    bStop = false    '使得按过键后能够重新开始循环
    Doevents         '允许其他事件运行
    '......
    loop until bStop  '检测到停止标志结束循环
    End Sub
      

  5.   

    我写的,但我发现我的代码和楼上的代码只能显示K,L等键件,对回车好象无效。
    再改一改,也请高手参与。
    Option Explicit
    Dim keyp As BooleanPrivate Sub Command1_Click()keyp = TrueDo While True
         
         
         DoEvents     If Not keyp Then
         
             MsgBox "成功退出!"
             
             Exit Do
         
         End IfLoopEnd SubPrivate Sub Form_KeyPress(KeyAscii As Integer)    keyp = False
        
    End SubPrivate Sub Form_Load() Form1.keypPreview = true  '重要End Sub
      

  6.   

    这才是真正的正解。Option Explicit
    Dim keyp As BooleanPrivate Sub Command1_Click()keyp = TrueCommand1.Enabled = FalseDo While True
              
         DoEvents     If Not keyp Then
         
             MsgBox "成功退出!"
             
             Exit Do
         
         End IfLoopCommand1.Enabled = TrueEnd SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)'用KeyDown事件可以响应所有键盘  If Command1.Enabled = False Then '避免对其它按键程序干扰
      
        keyp = False
        
      End If
      
    End Sub