【问题标题】:how to test if a particular control has focus?如何测试特定控件是否具有焦点?
【发布时间】:2013-12-20 19:48:00
【问题描述】:

我可以访问 2007 表单,我想测试特定控件(切换按钮)是否具有焦点,

类似:

if gotfocus(mytoggle) then
dosomething
endif

或者可能像:

if me.mytoggle.setfocus = true then
dosomething
endif

我已经搜索并找不到这个,有人可以告诉我这样做的正确方法是什么吗?

【问题讨论】:

  • Application.Caller 是否在 Access 中工作?

标签: vba ms-access ms-access-2007


【解决方案1】:

这是当前表单:

If (mytoggle Is Me.ActiveControl) Then

这适用于当前的 Access.Application:

If (mytoggle Is Screen.ActiveControl) Then

注意,如果没有控件有焦点,*.ActiveControl 可能不存在。

【讨论】:

    【解决方案2】:

    不幸的是,在某些情况下 .ActiveControl 临时不存在!当记录在表单中滚动时,程序 Form_Current() 开始运行。一开始就没有焦点了——只有 Form_Current() 终止后,焦点才会重置到前一个字段。

    【讨论】:

    • 这种情况有解决方案或变通方法吗?
    • 对不起,我不知道。
    【解决方案3】:

    试试这个代码 - 我试图说明 .ActiveControl 不存在。

    Private Function isCurrentControl(thisControl As Control) As Boolean
        On Error GoTo err_handler
        
            If Not Me.ActiveControl Is Nothing Then
                If (Me.ActiveControl Is thisControl) Then
                    isCurrentControl = True
                Else
                    isCurrentControl = False
                End If
            Else
                GoTo err_handler
            End If
      
    close_function:
        On Error GoTo 0
        Exit Function
            
    err_handler:
            isCurrentControl = False
            Resume close_function
    
    End Function
    

    你只需要调用函数并将控件设置为参数

    ''EXAMPLE: isCurrentControl(mytoggle)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      相关资源
      最近更新 更多