问题描述
我有 TextBox
控件和关联的计时器,它们在每个 KeyDown
或 MouseClick
事件上重新启动,并在 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?
非常感谢!
推荐答案
你可以使用EnumThreadWindows
查找所有自动完成的下拉窗口并检查它们是否可见.自动完成下拉窗口类名称为 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 自动完成列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!