检测何时显示TextBox自动完成列表 [英] Detect when TextBox autocomplete list is showing

查看:79
本文介绍了检测何时显示TextBox自动完成列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有TextBox控件和关联的计时器,它们在每个KeyDownMouseClick事件上重新启动,并在3秒钟后基于键入的文本执行查询,而没有这些事件.到目前为止一切顺利.

I have TextBox controls and associated Timers that restarts on each KeyDown or MouseClick event, and performs queries based on typed text after 3 seconds without those events. So far so good.

但是我的某些文本框也具有用户可以浏览的自动完成"列表,但是即使使用键盘的箭头键,也不会暂停计时器,并且在用户浏览列表的过程中会触发意外查询.

But some of my TextBoxes also have AutoComplete lists which user can browse in, but even if they do using keyboard arrow keys, timer is not halted and an unexpected query is fired in the middle of user browsing the list.

问题:有没有一种方法可以检测到何时显示自动完成列表,以便我可以暂停计时器或忽略其勾号?

Question: is there a way of detecting when autocomplete list is showing, so that I can suspend timer or ignoring its tick?

非常感谢!

推荐答案

您可以使用Auto-Suggest Dropdown.您可以使用 GetClassName 方法来获取枚举窗口的类名,然后使用 IsWindowVisible 方法检查窗口是否可见.

You can use EnumThreadWindows to find all auto complete dropdown windows and check if any of them is visible. The Auto-complete dropdown window class name is Auto-Suggest Dropdown. You can use GetClassName method to get class name of enumerated windows and then using IsWindowVisible method check if the window is visible.

示例

在下面的示例中,我使用了与问题代码中类似的计时器,并且在计时器的计时事件中,我检查了是否有自动完成的窗口打开,我在标题中显示了打开"表格,否则显示为关闭":

In below example, I used a timer like you have in your code in the question, and in tick event of the timer, I've checked if there is an auto-complete window open I showed "Open" in title of form, otherwise showed "close":

Delegate Function EnumThreadDelegate(hWnd As IntPtr, lParam As IntPtr) As Boolean

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function EnumThreadWindows(dwThreadId As Integer, _
    lpfn As EnumThreadDelegate, lParam As IntPtr) As Boolean
End Function

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function GetClassName(ByVal hWnd As System.IntPtr,
    lpClassName As System.Text.StringBuilder, _
    nMaxCount As Integer) As Integer
End Function

<System.Runtime.InteropServices.DllImport("kernel32.dll")> _
Shared Function GetCurrentThreadId() As Integer
End Function

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Shared Function IsWindowVisible(hWnd As IntPtr) As Boolean
End Function

Const AutoCompleteClassName As String = "Auto-Suggest Dropdown"
Function EnumThreadCallback(hWnd As IntPtr, lParam As IntPtr) As Boolean
    Dim className As New System.Text.StringBuilder("", 256)
    GetClassName(hWnd, className, 256)
    If className.ToString() = AutoCompleteClassName AndAlso IsWindowVisible(hWnd) Then
        AnAutoCOmpleteIsOpen = True
    End If
    Return True
End Function
Dim AnAutoCOmpleteIsOpen As Boolean = False
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    AnAutoCOmpleteIsOpen = False
    EnumThreadWindows(GetCurrentThreadId(), _
        New EnumThreadDelegate(AddressOf Me.EnumThreadCallback), IntPtr.Zero)
    If (AnAutoCOmpleteIsOpen) Then
        Me.Text = "Open"
    Else
        Me.Text = "Close"
    End If
End Sub

这篇关于检测何时显示TextBox自动完成列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆