Sign in to follow this  
Followers 0
Spiff59

Ideas for a better _GUICtrlComboBox_AutoComplete?

2 posts in this topic

#1 ·  Posted (edited)

I've never liked the way the UDF function _GUICtrlComboBox_AutoComplete behaves.

It ignores the delete and backspace keys.

It is unresponsive to characters being inserted or overwritten.

In one case, I needed it to restrict users to only entries in the ComboBox list; to not allow freeform typing.

So, a while back I whipped up my own version, which I think behaves quite nicely, but it does not highlight the auto-filled portion to the right of the string as many similar functions do. I thought if I could get that working that this might be a good replacement for the existing function.

If I enable highlighting the "optional" text to the right of what has been typed, then I run into issues with the behavior of the backspace key. If text is selected, backspavce deletes that, otherwise it deletes the last significant character. The called function has no way of knowing which took place. It results in troublesome logic to ensure the proper number of chars are selected, particularily when transitioning (via backspace) from a string not in the combo list to a shorter string that now matches a list entry. After a couple hours my head is scrambled. I can't say if the third version in the demo below is the best I've had going, having tried a few options. Triggering the function from the $CBN_EDITUPDATE message didn't seem to offer a fix. I thought about trying to only simulate selected text, by painting the background of the right portion of the string, but couldn't figure that out.

The demo below has the production version, my working non-highlighted version, and then a combobox at the bottom where I was trying to work out a final version with highlighting.

Has anyone put together a more functional _GUICtrlComboBox_AutoComplete already?

I couldn't fnd one...

Any ideas?

#include <WindowsConstants.au3>
#include <GuiConstants.au3>
#include <GuiComboBox.au3>

Global $lock2, $lock3
$list = "Aaron|Ada|Adams|Adamsville|Addams|Adell|Adelman|Adenson|Akron|Appleton|Bannock|Bear Lake|Benewah|Bingham|Blaine|Boise|Bonner|Bonneville|" & _
"Boundary|Butte|Camas|Canyon|Caribou|Cassia|Clark|Clearwater|Custer|Elmore|Franklin|Fremont|Gem|Gooding|Idaho|Jefferson|Jerome|" & _
"Kootenai|Latah|Lemhi|Lewis|Lincoln|Madison|Minidoka|Oneida|Owyhee|Payette|Power|Shoshone|Test11|Test22|Testing|Teton|Twin Falls|Valley|Washington"

GUICreate("_GUICtrlComboBox_AutoComplete comparison",430,320)
GUICtrlCreateLabel(StringReplace($list,"|"," "), 10, 10, 400, 100)

GUICtrlCreateLabel("PRODUCTION VERSION:", 20, 110, 130)
$Ctrl_Combo1 = GUICtrlCreateCombo("", 160, 107, 180)
GUICtrlSetData($Ctrl_Combo1, $list)

GUICtrlCreateLabel("MODIFIED VERSION:", 20, 145, 130)
$Ctrl_Combo2 = GUICtrlCreateCombo("", 160, 142, 180)
GUICtrlSetData($Ctrl_Combo2, $list)
$hCombo2 = GUICtrlGetHandle($Ctrl_Combo2)
$Ctrl_Check2 = GUICtrlCreateCheckbox("LOCK", 350, 142, 50, 20)

GUICtrlCreateLabel("TROUBLED VERSION:", 20, 180, 130)
$Ctrl_Combo3 = GUICtrlCreateCombo("", 160, 177, 180)
GUICtrlSetData($Ctrl_Combo3, $list)
$hCombo3 = GUICtrlGetHandle($Ctrl_Combo3)
$Ctrl_Check3 = GUICtrlCreateCheckbox("LOCK", 350, 177, 50, 20)

GUICtrlCreateLabel("Behaviors to test:", 20, 220, 400)
GUICtrlCreateLabel("> Backspace and delete keys", 20, 240, 400)
GUICtrlCreateLabel("> Inserting and ovewriting characters", 20, 260, 400)
GUICtrlCreateLabel("> Arrow up and arrow down keys", 20, 280, 400)
GUISetState()
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $Ctrl_Check2
            $lock2 = (Not $lock2)
            _GUICtrlComboBox_SetEditText($hCombo2, "")
            GUICtrlSetState($Ctrl_Combo2, $GUI_FOCUS)
        Case $Ctrl_Check3
            $lock3 = (Not $lock3)
            _GUICtrlComboBox_SetEditText($hCombo3, "")
         GUICtrlSetState($Ctrl_Combo3, $GUI_FOCUS)
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

;-------------------------------------------------------------------------
Func _GUICtrlComboBox_AutoComplete2($hWnd, $iLock = 0)
    Local $ret, $sEditText, $sEditSel
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    $sEditSel = _GUICtrlComboBox_GetEditSel($hWnd)
    If IsArray($sEditSel) Then
        $sEditSel = $sEditSel[0] ; characters left of cursor
    EndIf
    $sEditText = StringLeft(_GUICtrlComboBox_GetEditText($hWnd), $sEditSel)
    _GUICtrlComboBox_SetEditText($hWnd, $sEditText)
    If StringLen($sEditText) Then
        $ret = _GUICtrlComboBox_FindString($hWnd, $sEditText)
     If ($ret <> $CB_ERR) Then ; found
            _GUICtrlComboBox_SetCurSel($hWnd, $ret)
        Else
            If $iLock Then
                $sEditSel -= 1
                $sEditText = StringLeft($sEditText, $sEditSel)
             $ret = _GUICtrlComboBox_FindString($hWnd, $sEditText)
               _GUICtrlComboBox_SetCurSel($hWnd, $ret)
            EndIf
        EndIf
        _GUICtrlComboBox_SetEditSel($hWnd, $sEditSel, $sEditSel) ; place cursor
    EndIf
EndFunc ;==>_GUICtrlComboBox_AutoComplete2

;-------------------------------------------------------------------------
Func _GUICtrlComboBox_AutoComplete3($hWnd, $iLock = 0)
    Local $ret, $sEditText, $sEditSel
    If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd)
    $sEditSel = _GUICtrlComboBox_GetEditSel($hWnd)
    If IsArray($sEditSel) Then
        $sEditSel = $sEditSel[0] ; characters left of cursor
    EndIf
    $sEditText = StringLeft(_GUICtrlComboBox_GetEditText($hWnd), $sEditSel)
    _GUICtrlComboBox_SetEditText($hWnd, $sEditText)
    If StringLen($sEditText) Then
        $ret = _GUICtrlComboBox_FindString($hWnd, $sEditText)
        If ($ret <> $CB_ERR) Then ; found
            _GUICtrlComboBox_SetCurSel($hWnd, $ret)
            If __GUICtrlComboBox_IsPressed('08') Then
                $sEditSel -= 1
                $sEditText = StringLeft($sEditText, $sEditSel)
            EndIf
        Else
            If $iLock Then
                $sEditSel -= 1
             $sEditText = StringLeft($sEditText, $sEditSel)
             $ret = _GUICtrlComboBox_FindString($hWnd, $sEditText)
             _GUICtrlComboBox_SetCurSel($hWnd, $ret)
            EndIf
        EndIf
        _GUICtrlComboBox_SetEditSel($hWnd, $sEditSel, -1) ; place cursor
    EndIf
EndFunc ;==>_GUICtrlComboBox_AutoComplete3

;-------------------------------------------------------------------------
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    $nNotifyCode = BitShift($wParam, 16) ; HiWord
    $nID = BitAnd($wParam, 0x0000FFFF) ; LoWord
    Switch $nID
        Case $Ctrl_Combo1
            Switch $nNotifyCode
                Case $CBN_EDITCHANGE
                 _GUICtrlComboBox_AutoComplete($Ctrl_Combo1)
            EndSwitch
        Case $Ctrl_Combo2
         Switch $nNotifyCode
;              Case $CBN_EDITUPDATE
                Case $CBN_EDITCHANGE
                   _GUICtrlComboBox_AutoComplete2($Ctrl_Combo2, $lock2)
            EndSwitch
        Case $Ctrl_Combo3
            Switch $nNotifyCode
;              Case $CBN_EDITUPDATE
                Case $CBN_EDITCHANGE
                    _GUICtrlComboBox_AutoComplete3($Ctrl_Combo3, $lock3)
            EndSwitch
    EndSwitch
EndFunc

typo

Edited by Spiff59

Share this post


Link to post
Share on other sites



Has anyone put together a more functional _GUICtrlComboBox_AutoComplete already?

I haven't as I've always used the UDF version. I will have a look at this but make no promises that it can be resolved.

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

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  
Followers 0