Jump to content
Sign in to follow this  

Detect Tab into ListBox

Recommended Posts


I can't seem to find how to detect when a ListBox gets the focus by tabbing into it. What's happening is I have an input line, when I press Tab the focus shifts to the ListBox but the top item isn't selected. This results in the user having to play around moving the arrow keys to get the first item selected.

Ideally I'd like to know that the ListBox was entered due to Tabbing from the input line. Then I can just set the top item as selected. I don't want to interfere with mouse action so I only want to take non-default action if entry was from the input line and pressing Tab.. not via mouse click or backtab from another control.

Share this post

Link to post
Share on other sites


Take a look at this:

#include <GUIConstantsEx.au3>
#Include <GuiListBox.au3>
#include <WinAPI.au3>

$sData = "aaa|bbb|ccc"

$hGUI = GUICreate("Test", 500, 500)

$hInput = GUICtrlCreateInput("", 10, 10, 480, 20)
$hListBox = GUICtrlCreateList("", 10, 50, 480, 300)
GUICtrlSetData(-1 , $sData)


$fListFocus = False
$hLB_Handle = ControlGetHandle($hGUI, "", $hListBox)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE

    If _WinAPI_GetFocus() = $hLB_Handle And $fListFocus = False Then
        $iIndex = _GUICtrlListBox_GetTopIndex($hListBox)
        $sText  = _GUICtrlListBox_GetText($hListBox, $iIndex)
        MsgBox(0, "Selected List Item", $sText)
        $fListFocus = True
    ElseIf _WinAPI_GetFocus() <> $hLB_Handle Then
        $fListFocus = False


I think it does what you want.


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Share this post

Link to post
Share on other sites


Take a look at this:


Thanks for the reply. I had a WM_COMMAND handler already to catch mouse double click but it took me awhile to find the $LBN_SETFOCUS. Instead of worrying about the previous control in the Tab Stop chain, I just test if no items are selected when I get that notification. If none selected, set top item as selected.

Seems to work pretty well. I can't believe I've been diddling with arrow keys for months instead of fixing it!!! Takes a jolt to unzombify my brain sometimes. Thanks for posting your code. I appreciate the help. >_<

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this