CbAutoComplete - Auto-completes typed values in a ComboBox
Posted: 16 Mar 2016, 15:34
I have found a few scripts out there to accomplish this task, but all of them had some bug. So I decided to make my own and share it here.
I only wish there was a better way to detect when the user is deleting content. Checking key state of bs and del keys doesn't work with Right-Click > Delete or with simulated keystrokes. But I'm not going to try OnMessage either...
Sources and examples on GitHub
Feedback and improvements are welcome.
Code: Select all
;=======================================================================================
;
; Function: CbAutoComplete
; Description: Auto-completes typed values in a ComboBox.
;
; Author: Pulover [Rodolfo U. Batista]
; Usage: Call the function from the Combobox's gLabel.
;
;=======================================================================================
CbAutoComplete()
{ ; CB_GETEDITSEL = 0x0140, CB_SETEDITSEL = 0x0142
If ((GetKeyState("Delete", "P")) || (GetKeyState("Backspace", "P")))
return
GuiControlGet, lHwnd, Hwnd, %A_GuiControl%
SendMessage, 0x0140, 0, 0,, ahk_id %lHwnd%
MakeShort(ErrorLevel, Start, End)
GuiControlGet, CurContent,, %lHwnd%
GuiControl, ChooseString, %A_GuiControl%, %CurContent%
If (ErrorLevel)
{
ControlSetText,, %CurContent%, ahk_id %lHwnd%
PostMessage, 0x0142, 0, MakeLong(Start, End),, ahk_id %lHwnd%
return
}
GuiControlGet, CurContent,, %lHwnd%
PostMessage, 0x0142, 0, MakeLong(Start, StrLen(CurContent)),, ahk_id %lHwnd%
}
MakeLong(LoWord, HiWord)
{
return (HiWord << 16) | (LoWord & 0xffff)
}
MakeShort(Long, ByRef LoWord, ByRef HiWord)
{
LoWord := Long & 0xffff
, HiWord := Long >> 16
}
Sources and examples on GitHub
Feedback and improvements are welcome.