Jump to content

OnKeyPress-Event for Input boxes


Recommended Posts

Hey,

I'm trying to create a searchable listview. I've got an input field where you can enter a keyword and the listview will show only matching results. The clue: i want a live search. Everytime you enter / remove a letter in that input box the listiew should update.

Any idea?

Link to comment
Share on other sites

  • Moderators

Sn0opy,

I wrote this some time ago - it uses a List rather than a ListView, but the principles would be the same: :)

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

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

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

$hGUI = GUICreate("Example", 200, 400)
$hInput = GUICtrlCreateInput("", 5, 5, 190, 20)
$hList = GUICtrlCreateList("", 5, 30, 190, 325, BitOR(0x00100000, 0x00200000))
$hButton = GUICtrlCreateButton("Read", 60, 360, 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 99
        For $j = 1 To 12
            $asKeyWords[$i] &= Chr(Random(65, 90, 1))
        Next
        $sData &= $asKeyWords[$i] & "|"
    Next
    GUICtrlSetData($hList, $sData)
    $iCurrIndex = -1
    _GUICtrlListBox_SetCurSel($hList, $iCurrIndex)

EndFunc   ;==>Keywords

The list is filled with random 12-letter "words" - just start typing random letters in the input and only items in the list containing the string in the input will appear.

I hope it helps - if not let me know and we can work on tailoring it to your precise needs. :)

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...