在任何鼠标点击之后,将代码放入循环中以退出循环

在用户窗体上,我正在通过切换其可见性来closures/closures帧。 它闪烁不定次数然后停止,但是在闪烁之间它检查用户活动。 如果鼠标在窗体的任何地方或任何包含的控件上单击,则闪烁将立即停止。

这是我的blinker看起来像。

For i = 1 To numberOfBlinks <blink twice> DoEvents If <click detected> Then Exit Sub Next i 

一切工作正常,除了<click detected>部分。 我如何从循环内部做到这一点?

您是否尝试将mouseclick事件中的全局布尔variables更改为true(默认为false)?

然后尝试检查<click detected>全局布尔variables是否为true。

这似乎工作正常,但它看起来像很多代码只是检测到鼠标点击。 例如,我认为应该可以创build一个包含所有表单控件的类,这样我就可以一次检测到它们中的任何一个,而不必单独检查每种控件。 我无法做到这一点,我希望有人能改善这一点。

只是为了重申这一点:在一个用户窗体中,一个名为mapFrame的大型框架可以包含任意数量的其他框架和标签,并且所有包含框架的框架可以包含任意数量的其他框架和标签,但是这与嵌套一样深。 我想开始一个循环(在这种情况下,循环闪烁一个控制closures和打开,但它可以是任何其他循环),并等待用户点击任何包含的框架或标签来表示从循环退出。 我也想获得被点击的控件的名称。

我通过therealmarv采取了build议,并使用点击设置一个公共布尔值在循环内部进行testing。

在一个新的类模块中:

 Option Explicit Public WithEvents classLabels As msForms.Label Private Sub classLabels_Click() clickedControlName = "" '<== Public String With classLabels If .Parent.Name = "mapFrame" Or _ .Parent.Parent.Name = "mapFrame" Then isClickDetected = True '<== Public Boolean clickedControlName = .Name End If End With End Sub 

在另一个新的类模块中:

 Option Explicit Public WithEvents classFrames As msForms.Frame Private Sub classFrames_Click() clickedControlName = "" '<== Public String With classFrames If .Name = "mapFrame" Or _ .Parent.Name = "mapFrame" Or _ .Parent.Parent.Name = "mapFrame" Then isClickDetected = True '<== Public Boolean clickedControlName = .Name End If End With End Sub 

在表单模块中:

 Option Explicit Dim frames() As New clsFrames Dim labels() As New clsLabels Private Sub createFrameListeners() Dim ctl As msForms.Control Dim frameCount as Long For Each ctl In Me.Controls ' Debug.Print TypeName(ctl): Stop If TypeName(ctl) = "Frame" Then frameCount = frameCount + 1 ReDim Preserve frames(1 To frameCount) 'Create the Frame Listener objects Set frames(frameCount).classFrames = ctl End If Next ctl End Sub Private Sub createLabelListeners() Dim ctl As msForms.Control Dim LabelCount as Long For Each ctl In Me.Controls ' Debug.Print TypeName(ctl): Stop If TypeName(ctl) = "Label" Then LabelCount = LabelCount + 1 ReDim Preserve labels(1 To LabelCount) 'Create the Label Listener objects Set labels(LabelCount).classLabels = ctl End If Next ctl End Sub Function blinkThisControl(ctrl As Control, ByVal blinkCount As Long) isClickDetected = False Dim i As Integer For i = 1 To blinkCount ' <blink ctrl twice> DoEvents If isClickDetected Then Exit Function 'name of clicked control will be in clickedControlName Next i End Function Private Sub userform_initialize() Call createFrameListeners Call createLabelListeners ' do other stuff End Sub