Jump to content

Google's style autocomplete text field


Recommended Posts

Hi guys!

Once again I need your help. I would like to change the below Melba23's code to make the following list

$hList = GUICtrlCreateList("", 5, 40, 300, 30, BitOR(0x00100000, 0x00200000))

appearing only while I write something in the input box and disappearing after clicking on the chosen word (i.e. one of the seven dwarfs).

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

Global $hGUI, $hInput, $hList, $sPartialData, $asKeyWords[100]

; Create list full of random 5 character "words"
Keywords()

$hGUI = GUICreate("Example", 400, 500)
$hInput = GUICtrlCreateInput("", 5, 5, 300, 28)
GUICtrlSetFont(-1, 14, 400, 0, "Verdana")

$hList = GUICtrlCreateList("", 5, 40, 300, 90, BitOR(0x00100000, 0x00200000))
GUICtrlSetFont(-1, 14, 400, 0, "Verdana")

$hButton = GUICtrlCreateButton("Read", 310, 3, 80, 30)
$hUP = GUICtrlCreateDummy()
$hDOWN = GUICtrlCreateDummy()
$hENTER = GUICtrlCreateDummy()
GUISetState(@SW_SHOW, $hGUI)

; Set accelerators for Cursor up/down and Enter
Dim $AccelKeys[3][2]=[["{UP}", $hUP], ["{DOWN}", $hDOWN], ["{ENTER}", $hENTER]]
GUISetAccelerators($AccelKeys)

$sCurr_Input = ""
$iCurrIndex = -1

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hList
            $sChosen = GUICtrlRead($hList)
            If $sChosen <> "" Then GUICtrlSetData($hInput, $sChosen)
        Case $hButton
            If $sPartialData <> "" Then
                $sFinal = GUICtrlRead($hInput)
                If _ArraySearch($asKeyWords, $sFinal) >= 0 Then
                    MsgBox(0, "Chosen", $sFinal)
                EndIf
            EndIf
        Case $hUP
            If $sPartialData <> "" Then
                $iCurrIndex -= 1
                If $iCurrIndex < 0 Then $iCurrIndex = 0
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hDOWN
            If $sPartialData <> "" Then
                $iTotal = _GUICtrlListBox_GetCount($hList)
                $iCurrIndex += 1
                If $iCurrIndex > $iTotal - 1 Then $iCurrIndex = $iTotal - 1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
        Case $hENTER
            If $iCurrIndex <> -1 Then
                $sText = _GUICtrlListBox_GetText($hList, $iCurrIndex)
                GUICtrlSetData($hInput, $sText)
                $iCurrIndex = -1
                _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)
            EndIf
    EndSwitch

    ; If input has changed, refill list with matching items
    If GUICtrlRead($hInput) <> $sCurr_Input Then
        CheckInputText()
        $sCurr_Input = GUICtrlRead($hInput)
    EndIf

WEnd

Func CheckInputText()

    $sPartialData = "|" ; Start with delimiter so new data always replaces old
    Local $sInput = GUICtrlRead($hInput)
    If $sInput <> "" Then
        For $i = 0 To 99
            If StringInStr($asKeyWords[$i], $sInput) <> 0 Then $sPartialData &= $asKeyWords[$i] & "|"
        Next
        GUICtrlSetData($hList, $sPartialData)
    EndIf
EndFunc   ;==>CheckInputText

Func Keywords()

    Local $sData
    For $i = 0 To 6
        $asKeyWords[0] = "Bashful"
        $asKeyWords[1] = "Sleepy "
        $asKeyWords[2] = "Happy"
         $asKeyWords[3] = "Sneezy"
         $asKeyWords[4] = "Doc"
         $asKeyWords[5] = "Grumpy"
         $asKeyWords[6] = "Dopey"



        $sData &= $asKeyWords[$i] & "|"
    Next
    GUICtrlSetData($hList, $sData)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)

EndFunc   ;==>Keywords


Func _PopupSelector($hMainGUI, ByRef $hListGUI, $sCurr_List)
    Local $hList = -1
    If $sCurr_List = "" Then
        Return $hList
    EndIf
    $hListGUI = GUICreate("", 420, 160, 10, 120, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST, $WS_EX_MDICHILD), $hMainGUI)
    ;GUICtrlSetFont(-1, 14, 400, 0, "Arial Rounded")
    $hList = GUICtrlCreateList("", 0, 0, 420, 150, BitOR(0x00100000, 0x00200000))
    GUICtrlSetFont(-1, 13, 400, 0, "Arial Rounded")
    GUICtrlSetData($hList, $sCurr_List)
    GUISetControlsVisible($hListGUI) ; To Make Control Visible And Window Invisible.
    GUISetState(@SW_SHOWNOACTIVATE, $hListGUI)
    Return $hList
EndFunc   ;==>_PopupSelector

Thanks a lot!!!

Edited by ferradavi
Link to comment
Share on other sites

  • Moderators

ferradavi,

To keep it simple, I would just add a few lines like this in the main loop:

If GUICtrlRead($cInput) = "" And $fShowing Then
    GUICtrlSetState($cList, $GUI_HIDE)
    $fShowing = False
ElseIf GUICtrlRead($cInput) <> "" And Not $fShowing Then
    GUICtrlSetState($cList, $GUI_SHOW)
    $fShowing = True
EndIf

You will need to set $fShowing to False and hide the list initially as well.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

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

 

Link to comment
Share on other sites

  • Moderators

ferradavi,

Of course:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#Include <GuiListBox.au3>

Global $hGUI, $cInput, $cList, $sPartialData, $asKeyWords[100], $fShowing = True

; Create list full of random 5 character "words"
Keywords()

$hGUI = GUICreate("Example", 400, 500)
$cInput = GUICtrlCreateInput("", 5, 5, 300, 28)
GUICtrlSetFont(-1, 14, 400, 0, "Verdana")

$cList = GUICtrlCreateList("", 5, 40, 300, 90, BitOR(0x00100000, 0x00200000))
GUICtrlSetFont($cList, 14, 400, 0, "Verdana")
GUICtrlSetBkColor($cList, 0xFFCCCC)
GUISetState($cList, $GUI_HIDE)

$cButton = GUICtrlCreateButton("Read", 310, 3, 80, 30)
$cUP = GUICtrlCreateDummy()
$cDOWN = GUICtrlCreateDummy()
$cENTER = GUICtrlCreateDummy()
GUISetState(@SW_SHOW, $hGUI)

; Set accelerators for Cursor up/down and Enter
Dim $AccelKeys[3][2]=[["{UP}", $cUP], ["{DOWN}", $cDOWN], ["{ENTER}", $cENTER]]
GUISetAccelerators($AccelKeys)

$sCurr_Input = ""
$iCurrIndex = -1

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cList
            $sChosen = GUICtrlRead($cList)
            If $sChosen <> "" Then GUICtrlSetData($cInput, $sChosen)
        Case $cButton
            If $sPartialData <> "" Then
                $sFinal = GUICtrlRead($cInput)
                If $sFinal And _ArraySearch($asKeyWords, $sFinal) >= 0 Then
                    MsgBox($MB_SYSTEMMODAL, "Chosen", $sFinal)
                    GUICtrlSetData($cInput, "")
                EndIf
            EndIf
        Case $cUP
            If $sPartialData <> "" Then
                $iCurrIndex -= 1
                If $iCurrIndex < 0 Then $iCurrIndex = 0
                _GUICtrlListBox_SetCurSel($cList, $iCurrIndex)
            EndIf
        Case $cDOWN
            If $sPartialData <> "" Then
                $iTotal = _GUICtrlListBox_GetCount($cList)
                $iCurrIndex += 1
                If $iCurrIndex > $iTotal - 1 Then $iCurrIndex = $iTotal - 1
                _GUICtrlListBox_SetCurSel($cList, $iCurrIndex)
            EndIf
        Case $cENTER
            If $iCurrIndex <> -1 Then
                $sText = _GUICtrlListBox_GetText($cList, $iCurrIndex)
                GUICtrlSetData($cInput, $sText)
                $iCurrIndex = -1
                _GUICtrlListBox_SetCurSel($cList, $iCurrIndex)
            EndIf
    EndSwitch

    ; If input has changed, refill list with matching items
    If GUICtrlRead($cInput) <> $sCurr_Input Then
        CheckInputText()
        $sCurr_Input = GUICtrlRead($cInput)
    EndIf

    If GUICtrlRead($cInput) = "" And $fShowing Then
        GUICtrlSetState($cList, $GUI_HIDE)
        $fShowing = False
    ElseIf GUICtrlRead($cInput) <> "" And Not $fShowing Then
        GUICtrlSetState($cList, $GUI_SHOW)
        $fShowing = True
    EndIf

WEnd

Func CheckInputText()

    $sPartialData = "|" ; Start with delimiter so new data always replaces old
    Local $sInput = GUICtrlRead($cInput)
    If $sInput <> "" Then
        For $i = 0 To 99
            If StringInStr($asKeyWords[$i], $sInput) <> 0 Then $sPartialData &= $asKeyWords[$i] & "|"
        Next
        GUICtrlSetData($cList, $sPartialData)
    EndIf
EndFunc   ;==>CheckInputText

Func Keywords()

    Local $sData
    For $i = 0 To 6
        $asKeyWords[0] = "Bashful"
        $asKeyWords[1] = "Sleepy "
        $asKeyWords[2] = "Happy"
         $asKeyWords[3] = "Sneezy"
         $asKeyWords[4] = "Doc"
         $asKeyWords[5] = "Grumpy"
         $asKeyWords[6] = "Dopey"



        $sData &= $asKeyWords[$i] & "|"
    Next
    GUICtrlSetData($cList, $sData)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($cList, $iCurrIndex)

EndFunc   ;==>Keywords


Func _PopupSelector($hMainGUI, ByRef $cListGUI, $sCurr_List)
    Local $cList = -1
    If $sCurr_List = "" Then
        Return $cList
    EndIf
    $cListGUI = GUICreate("", 420, 160, 10, 120, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST, $WS_EX_MDICHILD), $hMainGUI)
    ;GUICtrlSetFont(-1, 14, 400, 0, "Arial Rounded")
    $cList = GUICtrlCreateList("", 0, 0, 420, 150, BitOR(0x00100000, 0x00200000))
    GUICtrlSetFont(-1, 13, 400, 0, "Arial Rounded")
    GUICtrlSetData($cList, $sCurr_List)
    GUISetState(@SW_SHOWNOACTIVATE, $cListGUI)
    Return $cList
EndFunc   ;==>_PopupSelector

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...