Jump to content
Iczer

Cancel ComboBox selection when clicking outside control

Recommended Posts

#include <GuiConstantsEx.au3>
#include <GuiComboBox.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiComboBoxEx.au3>
#include <Array.au3>

Opt("GUIOnEventMode", 1)

Global $aRecentSearchStrings[8] = [7,"aaa","bbb","ccc","ddd","eee","fff","ggg"]
Global $sRecentSearchStrings

For $i = 1 To $aRecentSearchStrings[0]
    $sRecentSearchStrings &= "|" & $aRecentSearchStrings[$i]
Next

#Region ### START Koda GUI section ### Form=
Local $hGUI = GUICreate("Form1", 548, 294, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
Local $Group1 = GUICtrlCreateGroup("Group1", 8, 8, 529, 65)
Local $Button1 = GUICtrlCreateButton("Search", 456, 32, 75, 24)
GUICtrlSetOnEvent(-1, "Button1Click")
Local $hwndCombo = _GUICtrlComboBox_Create($hGUI,"", 16, 32, 433, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL, $WS_VSCROLL,$WS_HSCROLL,$CBS_DROPDOWN), $WS_EX_STATICEDGE)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

_GUICtrlComboBox_BeginUpdate ( $hwndCombo )
For $i = 1 To $aRecentSearchStrings[0]
    _GUICtrlComboBox_AddString ( $hwndCombo, $aRecentSearchStrings[$i] )
Next
_GUICtrlComboBox_EndUpdate ( $hwndCombo )

GUISetState()
GUIRegisterMsg($WM_COMMAND, "Proc_WM_COMMAND")

While 1
    Sleep(10)
WEnd

Func Form1Close()
    GUIDelete()
    Exit
EndFunc

Func Button1Click()
    Local $sSearchStringNew = _GUICtrlComboBox_GetEditText ( $hwndCombo )
    
    If StringLen($sSearchStringNew) < 3 Then Return
    
    _ArrayAdd($aRecentSearchStrings,$sSearchStringNew)
    $aRecentSearchStrings = _ArrayUnique($aRecentSearchStrings,0,1)
    
    $sRecentSearchStrings = ""
    For $i = 1 To $aRecentSearchStrings[0]
        $sRecentSearchStrings &= "|" & $aRecentSearchStrings[$i]
    Next
    
    _GUICtrlComboBox_BeginUpdate ( $hwndCombo )
    _GUICtrlComboBox_ShowDropDown ( $hwndCombo , False )
    _GUICtrlComboBox_ResetContent ( $hwndCombo )
    For $i = 1 To $aRecentSearchStrings[0]
        _GUICtrlComboBox_AddString ( $hwndCombo, $aRecentSearchStrings[$i] )
    Next
    _GUICtrlComboBox_SetEditText ( $hwndCombo, $sSearchStringNew )
    _GUICtrlComboBox_EndUpdate ( $hwndCombo )
EndFunc

Func ComboEdit_Changed()
    AdlibUnRegister("ComboEdit_Changed")
    GUIRegisterMsg($WM_COMMAND, "")
    
    Local $sCurrentStr = _GUICtrlComboBox_GetEditText ($hwndCombo)
    
    If StringLen($sCurrentStr) = 0 Then
        
        _GUICtrlComboBox_BeginUpdate ( $hwndCombo )
        _GUICtrlComboBox_ShowDropDown ( $hwndCombo , False )
        _GUICtrlComboBox_ResetContent ( $hwndCombo )
        
        For $i = 1 To $aRecentSearchStrings[0]
            _GUICtrlComboBox_AddString ( $hwndCombo, $aRecentSearchStrings[$i] )
        Next

        _GUICtrlComboBox_EndUpdate ( $hwndCombo )
        
        GUIRegisterMsg($WM_COMMAND, "Proc_WM_COMMAND")
        
        Return
    EndIf
    
    Local $aSearchSugestions = StringRegExp($sRecentSearchStrings,"\|([^\|]*?" & $sCurrentStr & "[^\|]++)",3)
    Local $iError = @error
    
    _GUICtrlComboBox_BeginUpdate ( $hwndCombo )
    _GUICtrlComboBox_ShowDropDown ( $hwndCombo , False )
    _GUICtrlComboBox_ResetContent ( $hwndCombo )
    
    If $iError Then

        For $i = 1 To $aRecentSearchStrings[0]
            _GUICtrlComboBox_AddString ( $hwndCombo, $aRecentSearchStrings[$i] )
        Next
        
        _GUICtrlComboBox_SetEditText ( $hwndCombo, $sCurrentStr )
        _GUICtrlComboBox_EndUpdate ( $hwndCombo )
        
        GUIRegisterMsg($WM_COMMAND, "Proc_WM_COMMAND")
        
        Return
    EndIf
    
    For $i = 0 To UBound($aSearchSugestions)-1
        _GUICtrlComboBox_AddString ( $hwndCombo, $aSearchSugestions[$i] )
    Next
    _GUICtrlComboBox_ShowDropDown ( $hwndCombo , True )
    _GUICtrlComboBox_SetEditText ( $hwndCombo, $sCurrentStr )
    _GUICtrlComboBox_EndUpdate ( $hwndCombo )
    
    GUIRegisterMsg($WM_COMMAND, "Proc_WM_COMMAND")
EndFunc

Func CancelSelection()
    AdlibUnRegister("CancelSelection")
    AdlibUnRegister("ComboEdit_Changed")
EndFunc

Func Proc_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16), $hID = BitAND($wParam, 0xFFFF), $hCtrl = $lParam

    Switch $hCtrl
        Case $hwndCombo
            Switch $nNotifyCode
                Case $CBN_EDITCHANGE, $CBN_EDITUPDATE
                    AdlibRegister("ComboEdit_Changed",333)
                    
                Case $CBN_SELCHANGE
                    
                Case $CBN_SELENDCANCEL
                    AdlibRegister("CancelSelection",188)
                    
                Case $CBN_KILLFOCUS
                    AdlibRegister("CancelSelection",188)
                   
                Case $CBN_SETFOCUS
                   
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Is there a way to cancel ComboBox Edit control being overwritten when drop-down list is closed by clicking outside combo-control? I want it retain manually typed string unless I specifically click on some string in drop-down list:

 

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

×
×
  • Create New...