Autocomplete a combobox in GUI

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ankitkraken
Posts: 84
Joined: 01 Jun 2019, 08:47
Location: India

Autocomplete a combobox in GUI

06 Nov 2019, 06:51

Hi,

I want to have an autocomplete combobox in AHK which would filter the list with the entered characters in the field.
Below is the code which I was able to get from the forum and tweaked it a bit to suit for ComboBox. It filters the list but doesn't show the entered chars in the text field.

Code: Select all

list_of_tasks := "Resolved"
     . "|create matrix"
     . "|show stack"
     . "|driver ver"
     . "|len"
     . "|convert"
     . "|aht"
     . "|start counting doc"
     . "|test"
     . "|temp"

Gui, Add, Text,, What to do?
Gui, Add, ComboBox, vHelp_task gAutoComplete R20 H50 W500, %list_of_tasks%
Gui,Add,Button, y+200 Default ghelp_task_check,Submit
Gui,Add,Button, x+20,Cancel

AutoComplete:

	Gui, submit, nohide

	loop, parse, list_of_tasks, | ; parse the list to see if the name is in it

	{

		if A_LoopField contains %Help_task%

			newlist .= A_LoopField . "|" ; populate the new list

	}

	if newlist =

		newlist := list_of_tasks

	GuiControl,, Help_task, |%newlist% 
	newlist := ; to clear the variable for population later on

return
garry
Posts: 3786
Joined: 22 Dec 2013, 12:50

Re: Autocomplete a combobox in GUI

06 Nov 2019, 15:27

example with EDIT / DropDownList

Code: Select all

e9=
(Ltrim join|
Resolved
create matrix
show stack
driver ver
len
convert
aht
start counting doc
test
temp
)

Gui,1: Color, ControlColor,Black           ;- background from edit
Gui,1: Color, Gray                         ;- Gui color
Gui,1: Font,s12 cYellow ,Lucida Console    ;- font , font-size and color
Gui,add,edit,gE1 x10 y10 h25 w200 vE2 -vscroll,convert           ;- preselect convert /  or type in and use ENTER
Gui,Add,DDL,gListx vLB1 h190,%e9%
Gui,Show, x100 y100 w220 h200,DDL-TEST
gosub,e1                                               ;- show preselected
;gosub,listx                                            ;- confirm
return

GuiClose:
ExitApp

e1:
Gui,Submit,NoHide
GuiControl,Choose,LB1,%e2%
return

$enter::
Listx:
Gui,Submit,NoHide
msgbox, 262208, ,You selected=%lb1%,2    ;- show selected
Return
User avatar
Delta Pythagorean
Posts: 628
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Autocomplete a combobox in GUI

07 Nov 2019, 05:55

You can use the following code to do autocomplete on a ComboBox and an Edit control respectively.
Read the function's documentation below to better understand how to use and control the functions to your liking.

Code: Select all

; ComboBox Control	----------------------------------------
Gui, Add, ComboBox, w640 gCbAutoComplete, Blue|Green|Red|Purple|Yellow|None

; Edit Control		----------------------------------------
Gui, Add, Edit, w640 HWNDhMyEdit
SHAutoComplete(hMyEdit)
Gui, Show
Return

/*
	Hotkey:
		Enter
	Description:
		Run the text entered into the Edit Control (hMyEdit) and close the script.
*/
Enter::
	ControlGetText, MyText,, ahk_id %hMyEdit%
	Run, %MyText%
GuiClose:
	ExitApp


/*
	=======================================================================================
	 Function:			SHAutoComplete
	 Description:		Auto-completes typed values in an edit with various options.
	 Usage:
		Gui, Add, Edit, w200 h21 hwndEditCtrl1
		SHAutoComplete(EditCtrl1)
	=======================================================================================
*/
SHAutoComplete(hEdit, Option := 0x20000000) {
	; https://bit.ly/335nOYt		For more info on the function.
	DllCall("ole32\CoInitialize", "Uint", 0)
	; SHACF_AUTOSUGGEST_FORCE_OFF (0x20000000)
	;	Ignore the registry default and force the AutoSuggest feature off.
	;	This flag must be used in combination with one or more of the SHACF_FILESYS* or SHACF_URL* flags.
	; AKA. It won't autocomplete anything, but it will allow functionality such as Ctrl+Backspace deleting a word.
	DllCall("shlwapi\SHAutoComplete", "Uint", hEdit, "Uint", Option)
	DllCall("ole32\CoUninitialize")
}

/*
	=======================================================================================
	 Function:			CbAutoComplete
	 Description:		Auto-completes typed values in a ComboBox.

	 Author:			Pulover [Rodolfo U. Batista]
	 Usage:
		Gui, Add, ComboBox, w200 h50 gCbAutoComplete, Billy|Joel|Samual|Jim|Max|Jackson|George
	=======================================================================================
*/
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%
}

; Required for: CbAutoComplete()
MakeLong(LoWord, HiWord) {
	Return, (HiWord << 16) | (LoWord & 0xffff)
}

; Required for: CbAutoComplete()
MakeShort(Long, ByRef LoWord, ByRef HiWord) {
	LoWord := Long & 0xffff, HiWord := Long >> 16
}
Hopefully this helps.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: Autocomplete a combobox in GUI

10 Dec 2019, 16:17

Delta Pythagorean wrote:
07 Nov 2019, 05:55
You can use the following code to do autocomplete on a ComboBox and an Edit control respectively.
Read the function's documentation below to better understand how to use and control the functions to your liking.

Code: Select all

; ComboBox Control	----------------------------------------
Gui, Add, ComboBox, w640 gCbAutoComplete, Blue|Green|Red|Purple|Yellow|None

; Edit Control		----------------------------------------
Gui, Add, Edit, w640 HWNDhMyEdit
SHAutoComplete(hMyEdit)
Gui, Show
Return

/*
	Hotkey:
		Enter
	Description:
		Run the text entered into the Edit Control (hMyEdit) and close the script.
*/
Enter::
	ControlGetText, MyText,, ahk_id %hMyEdit%
	Run, %MyText%
GuiClose:
	ExitApp


/*
	=======================================================================================
	 Function:			SHAutoComplete
	 Description:		Auto-completes typed values in an edit with various options.
	 Usage:
		Gui, Add, Edit, w200 h21 hwndEditCtrl1
		SHAutoComplete(EditCtrl1)
	=======================================================================================
*/
SHAutoComplete(hEdit, Option := 0x20000000) {
	; https://bit.ly/335nOYt		For more info on the function.
	DllCall("ole32\CoInitialize", "Uint", 0)
	; SHACF_AUTOSUGGEST_FORCE_OFF (0x20000000)
	;	Ignore the registry default and force the AutoSuggest feature off.
	;	This flag must be used in combination with one or more of the SHACF_FILESYS* or SHACF_URL* flags.
	; AKA. It won't autocomplete anything, but it will allow functionality such as Ctrl+Backspace deleting a word.
	DllCall("shlwapi\SHAutoComplete", "Uint", hEdit, "Uint", Option)
	DllCall("ole32\CoUninitialize")
}

/*
	=======================================================================================
	 Function:			CbAutoComplete
	 Description:		Auto-completes typed values in a ComboBox.

	 Author:			Pulover [Rodolfo U. Batista]
	 Usage:
		Gui, Add, ComboBox, w200 h50 gCbAutoComplete, Billy|Joel|Samual|Jim|Max|Jackson|George
	=======================================================================================
*/
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%
}

; Required for: CbAutoComplete()
MakeLong(LoWord, HiWord) {
	Return, (HiWord << 16) | (LoWord & 0xffff)
}

; Required for: CbAutoComplete()
MakeShort(Long, ByRef LoWord, ByRef HiWord) {
	LoWord := Long & 0xffff, HiWord := Long >> 16
}
Hopefully this helps.
Hello, There are some special letters in my language. "iİ", "ıI" can detect these characters, but will not be able to detect if there is a difference in uppercase and lowercase letters. Could a simple solution be possible?
User avatar
Delta Pythagorean
Posts: 628
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Autocomplete a combobox in GUI

12 Dec 2019, 02:07

hasantr wrote:
10 Dec 2019, 16:17
Delta Pythagorean wrote:
07 Nov 2019, 05:55
You can use the following code to do autocomplete on a ComboBox and an Edit control respectively.
Read the function's documentation below to better understand how to use and control the functions to your liking.

Code: Select all

; ComboBox Control	----------------------------------------
Gui, Add, ComboBox, w640 gCbAutoComplete, Blue|Green|Red|Purple|Yellow|None

; Edit Control		----------------------------------------
Gui, Add, Edit, w640 HWNDhMyEdit
SHAutoComplete(hMyEdit)
Gui, Show
Return

/*
	Hotkey:
		Enter
	Description:
		Run the text entered into the Edit Control (hMyEdit) and close the script.
*/
Enter::
	ControlGetText, MyText,, ahk_id %hMyEdit%
	Run, %MyText%
GuiClose:
	ExitApp


/*
	=======================================================================================
	 Function:			SHAutoComplete
	 Description:		Auto-completes typed values in an edit with various options.
	 Usage:
		Gui, Add, Edit, w200 h21 hwndEditCtrl1
		SHAutoComplete(EditCtrl1)
	=======================================================================================
*/
SHAutoComplete(hEdit, Option := 0x20000000) {
	; https://bit.ly/335nOYt		For more info on the function.
	DllCall("ole32\CoInitialize", "Uint", 0)
	; SHACF_AUTOSUGGEST_FORCE_OFF (0x20000000)
	;	Ignore the registry default and force the AutoSuggest feature off.
	;	This flag must be used in combination with one or more of the SHACF_FILESYS* or SHACF_URL* flags.
	; AKA. It won't autocomplete anything, but it will allow functionality such as Ctrl+Backspace deleting a word.
	DllCall("shlwapi\SHAutoComplete", "Uint", hEdit, "Uint", Option)
	DllCall("ole32\CoUninitialize")
}

/*
	=======================================================================================
	 Function:			CbAutoComplete
	 Description:		Auto-completes typed values in a ComboBox.

	 Author:			Pulover [Rodolfo U. Batista]
	 Usage:
		Gui, Add, ComboBox, w200 h50 gCbAutoComplete, Billy|Joel|Samual|Jim|Max|Jackson|George
	=======================================================================================
*/
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%
}

; Required for: CbAutoComplete()
MakeLong(LoWord, HiWord) {
	Return, (HiWord << 16) | (LoWord & 0xffff)
}

; Required for: CbAutoComplete()
MakeShort(Long, ByRef LoWord, ByRef HiWord) {
	LoWord := Long & 0xffff, HiWord := Long >> 16
}
Hopefully this helps.
Hello, There are some special letters in my language. "iİ", "ıI" can detect these characters, but will not be able to detect if there is a difference in uppercase and lowercase letters. Could a simple solution be possible?
Possibly, but I don't think it'd be worth to tink around with the function until you get what you want. I'm not the one to ask for this kind of thing seeing as I just grabbed the functions and their examples from the forums.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Autocomplete a combobox in GUI

12 Dec 2019, 02:42

I can't speak on detecting the differences between characters in your language, but IMHO this is a better approach then using a normal auto-complete.

https://www.autohotkey.com/boards/viewtopic.php?f=76&t=69121&p=298058&hilit=typing+date#p298058

Try typing something like "date" into the second gui. (launched by pressing "Numpad1")

Code: Select all

#SingleInstance,Force
SetBatchLines,-1
;===============================================================================================================
;===================================== For The Demo Only =======================================================
;===============================================================================================================
 

PipeList:="UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255)|BitBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, Raster="")|StretchBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, sw, sh, Raster="")|SetStretchBltMode(hdc, iStretchMode=4)|SetImage(hwnd, hBitmap)|SetSysColorToControl(hwnd, SysColor=15)|Gdip_BitmapFromScreen(Screen=0, Raster="")|Gdip_BitmapFromHWND(hwnd)|CreateRectF(ByRef RectF, x, y, w, h)|CreateRect(ByRef Rect, x, y, w, h)|CreateSizeF(ByRef SizeF, w, h)|CreatePointF(ByRef PointF, x, y)|CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)|PrintWindow(hwnd, hdc, Flags=0)|DestroyIcon(hIcon)|PaintDesktop(hdc)|CreateCompatibleBitmap(hdc, w, h)|CreateCompatibleDC(hdc=0)|SelectObject(hdc, hgdiobj)|DeleteObject(hObject)|GetDC(hwnd=0)|GetDCEx(hwnd, flags=0, hrgnClip=0)|ReleaseDC(hdc, hwnd=0)|DeleteDC(hdc)|Gdip_LibraryVersion()|Gdip_LibrarySubVersion()|Gdip_BitmapFromBRA(ByRef BRAFromMemIn, File, Alternate=0)|Gdip_DrawRectangle(pGraphics, pPen, x, y, w, h)|Gdip_DrawRoundedRectangle(pGraphics, pPen, x, y, w, h, r)|Gdip_DrawEllipse(pGraphics, pPen, x, y, w, h)|Gdip_DrawBezier(pGraphics, pPen, x1, y1, x2, y2, x3, y3, x4, y4)|Gdip_DrawArc(pGraphics, pPen, x, y, w, h, StartAngle, SweepAngle)|Gdip_DrawPie(pGraphics, pPen, x, y, w, h, StartAngle, SweepAngle)|Gdip_DrawLine(pGraphics, pPen, x1, y1, x2, y2)|Gdip_DrawLines(pGraphics, pPen, Points)|Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h)|Gdip_FillRoundedRectangle(pGraphics, pBrush, x, y, w, h, r)|Gdip_FillPolygon(pGraphics, pBrush, Points, FillMode=0)|Gdip_FillPie(pGraphics, pBrush, x, y, w, h, StartAngle, SweepAngle)|Gdip_FillEllipse(pGraphics, pBrush, x, y, w, h)|Gdip_FillRegion(pGraphics, pBrush, Region)|Gdip_FillPath(pGraphics, pBrush, Path)|Gdip_DrawImagePointsRect(pGraphics, pBitmap, Points, sx="", sy="", sw="", sh="", Matrix=1)|Gdip_DrawImage(pGraphics, pBitmap, dx="", dy="", dw="", dh="", sx="", sy="", sw="", sh="", Matrix=1)|Gdip_SetImageAttributesColorMatrix(Matrix)|Gdip_GraphicsFromImage(pBitmap)|Gdip_GraphicsFromHDC(hdc)|Gdip_GetDC(pGraphics)|Gdip_ReleaseDC(pGraphics, hdc)|Gdip_GraphicsClear(pGraphics, ARGB=0x00ffffff)|Gdip_BlurBitmap(pBitmap, Blur)|Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality=75)|Gdip_GetPixel(pBitmap, x, y)|Gdip_SetPixel(pBitmap, x, y, ARGB)|Gdip_GetImageWidth(pBitmap)|Gdip_GetImageHeight(pBitmap)|Gdip_GetImageDimensions(pBitmap, ByRef Width, ByRef Height)|Gdip_GetDimensions(pBitmap, ByRef Width, ByRef Height)|Gdip_GetImagePixelFormat(pBitmap)|Gdip_GetDpiX(pGraphics)|Gdip_GetDpiY(pGraphics)|Gdip_GetImageHorizontalResolution(pBitmap)|Gdip_GetImageVerticalResolution(pBitmap)|Gdip_BitmapSetResolution(pBitmap, dpix, dpiy)|Gdip_CreateBitmapFromFile(sFile, IconNumber=1, IconSize="")|Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0)|Gdip_CreateHBITMAPFromBitmap(pBitmap, Background=0xffffffff)|Gdip_CreateBitmapFromHICON(hIcon)|Gdip_CreateHICONFromBitmap(pBitmap)|Gdip_CreateBitmap(Width, Height, Format=0x26200A)|Gdip_CreateBitmapFromClipboard()|Gdip_SetBitmapToClipboard(pBitmap)|Gdip_CloneBitmapArea(pBitmap, x, y, w, h, Format=0x26200A)|Gdip_CreatePen(ARGB, w)|Gdip_CreatePenFromBrush(pBrush, w)|Gdip_BrushCreateSolid(ARGB=0xff000000)|Gdip_BrushCreateHatch(ARGBfront, ARGBback, HatchStyle=0)|Gdip_CreateTextureBrush(pBitmap, WrapMode=1, x=0, y=0, w="", h="")|Gdip_CreateLineBrush(x1, y1, x2, y2, ARGB1, ARGB2, WrapMode=1)|Gdip_CreateLineBrushFromRect(x, y, w, h, ARGB1, ARGB2, LinearGradientMode=1, WrapMode=1)|Gdip_CloneBrush(pBrush)|Gdip_DeletePen(pPen)|Gdip_DeleteBrush(pBrush)|Gdip_DisposeImage(pBitmap)|Gdip_DeleteGraphics(pGraphics)|Gdip_DisposeImageAttributes(ImageAttr)|Gdip_DeleteFont(hFont)|Gdip_DeleteStringFormat(hFormat)|Gdip_DeleteFontFamily(hFamily)|Gdip_DeleteMatrix(Matrix)|Gdip_TextToGraphics(pGraphics, Text, Options, Font="", Width="", Height="", Measure=0)|Gdip_DrawString(pGraphics, sString, hFont, hFormat, pBrush, ByRef RectF)|Gdip_MeasureString(pGraphics, sString, hFont, hFormat, ByRef RectF)|Gdip_SetStringFormatAlign(hFormat, Align)|Gdip_StringFormatCreate(Format=0, Lang=0)|Gdip_FontCreate(hFamily, Size, Style=0)|Gdip_FontFamilyCreate(Font)|Gdip_CreateAffineMatrix(m11, m12, m21, m22, x, y)|Gdip_CreateMatrix()|Gdip_CreatePath(BrushMode=0)|Gdip_AddPathEllipse(Path, x, y, w, h)|Gdip_AddPathPolygon(Path, Points)|Gdip_DeletePath(Path)|Gdip_SetTextRenderingHint(pGraphics, RenderingHint)|Gdip_SetInterpolationMode(pGraphics, InterpolationMode)|Gdip_SetSmoothingMode(pGraphics, SmoothingMode)|Gdip_SetCompositingMode(pGraphics, CompositingMode=0)|Gdip_Startup()|Gdip_Shutdown(pToken)|Gdip_RotateWorldTransform(pGraphics, Angle, MatrixOrder=0)|Gdip_ScaleWorldTransform(pGraphics, x, y, MatrixOrder=0)|Gdip_TranslateWorldTransform(pGraphics, x, y, MatrixOrder=0)|Gdip_ResetWorldTransform(pGraphics)|Gdip_GetRotatedTranslation(Width, Height, Angle, ByRef xTranslation, ByRef yTranslation)|Gdip_GetRotatedDimensions(Width, Height, Angle, ByRef RWidth, ByRef RHeight)|Gdip_ImageRotateFlip(pBitmap, RotateFlipType=1)|Gdip_SetClipRect(pGraphics, x, y, w, h, CombineMode=0)|Gdip_SetClipPath(pGraphics, Path, CombineMode=0)|Gdip_ResetClip(pGraphics)|Gdip_GetClipRegion(pGraphics)|Gdip_SetClipRegion(pGraphics, Region, CombineMode=0)|Gdip_CreateRegion()|Gdip_DeleteRegion(Region)|Gdip_LockBits(pBitmap, x, y, w, h, ByRef Stride, ByRef Scan0, ByRef BitmapData, LockMode = 3, PixelFormat = 0x26200a)|Gdip_UnlockBits(pBitmap, ByRef BitmapData)|Gdip_SetLockBitPixel(ARGB, Scan0, x, y, Stride)|Gdip_GetLockBitPixel(Scan0, x, y, Stride)|Gdip_PixelateBitmap(pBitmap, ByRef pBitmapOut, BlockSize)|Gdip_ToARGB(A, R, G, B)|Gdip_FromARGB(ARGB, ByRef A, ByRef R, ByRef G, ByRef B)|Gdip_AFromARGB(ARGB)|Gdip_RFromARGB(ARGB)|Gdip_GFromARGB(ARGB)|Gdip_BFromARGB(ARGB)|StrGetB(Address, Length=-1, Encoding=0)|"
;~ ToolTip,% A_ScreenDPI / 96
Gui,1:+AlwaysOnTop -Theme +HwndGui1Hwnd
Gui,1:Color,222222,333333
Gui,1:Font,s10,Segoe UI
Gui,2:+Parent1 -Caption
Gui,2:Color,004444
Gui,2:Font,csilver s12 Bold,Segoe UI
Gui,2:Add,Text,x0 y0 w380 h40 Center 0x200,This Is The Target Window For The Demo
Gui,2:Show,x10 y10 w380 h40
Gui,1:Add,ComboBox,x50 y70 w300 r20,% PipeList
Gui,3:+Parent1 -Caption
Gui,3:Color,004444
Gui,3:Font,cYellow s10 Bold,Segoe UI
Gui,3:Add,Text,x0 y0 w380 h40 Center 0x200,***Press Numpad1 To Bring Up The Sorting Tool***
Gui,3:Show,% "x10 y" 120*(A_ScreenDPI / 96) " w380 h40"
Gui,1:Show,x200 y100 w400 h170,Demo
return
GuiClose:
GuiContextMenu:
*ESC::
	ExitApp
;=====================================================================================
;=========================== Main Functionality ======================================
;=====================================================================================

;~ #IfWinActive, You know what to do here

*Numpad1::
	Gui,4:New,+AlwaysOnTop -Theme
	Gui,4:Color,333333,444444
	Gui,4:Font,cWhite s10,Segoe UI
	Gui,4:Add,Edit,x10 y10 w280 r1 vInputValue gReviseList, 
	Gui,4:Add,ListBox,x10 y+10 w280 r10 vComboSorter,
	Gui,4:Add,Button,x+10 y10 gInjectSelection,Set Target
	Gui,4:Add,Button,xp y+10 gStartOver,Start Over
	gosub,GetListFromTarget
	Gui,4:Show,,Sorter
	return

;~ #If


4GuiClose:
	Gui,4:Destroy
	ComboListArray:="",ComboContentsList:="",SortedList:="",Choice:="",ComboSorter:="",InputValue:="",ComboList:=""
	return

ReviseList:
	GuiControlGet,InputValue
	if(!InputValue){
		GuiControl,4:,ComboSorter,% "|" ComboContentsList
		return
	}
	;================================================
	;================================================
	SortedList:="",Count:=0,last:=""
	Loop,% ComboListArray.Length()	
		if(InStr(ComboListArray[A_Index],InPutValue)){
			SortedList.=ComboListArray[A_Index] "|"
			Count++,last:=ComboListArray[A_Index]
		}
	;if(SortedList)  ;<------- The Derp!!!
		GuiControl,4:,ComboSorter,% "|" SortedList
	if(Count=1){
		GuiControl,4:Choose,ComboSorter,% last
		goto,InjectSelection
	}
	;================================================
	;================================================
	return

GetListFromTarget:
	ComboListArray:="",ComboListArray:=[],ComboContentsList:="",Index:=1
	ControlGet, ComboList, List,,ComboBox1,ahk_Id %Gui1Hwnd%
	Loop,Parse,ComboList,`n
      if(A_LoopField)
         ComboListArray[Index++]:=A_LoopField,ComboContentsList.=A_LoopField "|"
	GuiControl,4:,ComboSorter,% "|" ComboContentsList
	return

StartOver:
	GuiControl,4:,ComboSorter,% "|" ComboContentsList	
	GuiControl,4:,InputValue,
	return
	
InjectSelection:
	GuiControlGet,Choice,4:,ComboSorter
	Control,ChooseString,%Choice%,ComboBox1,ahk_Id %Gui1Hwnd%
	Gui,4:Destroy
	;================================================
	;================================================
	send,{Right}
	;================================================
	;================================================
	ComboListArray:="",ComboContentsList:="",SortedList:="",Choice:="",ComboSorter:="",InputValue:="",ComboList:=""
	return
	
;=====================================================================================
;=========================== Main Functionality End ==================================
;=====================================================================================	

That is more or less just a proof of concept script and would need to be adapted to your needs and the code refined (That was written in 15-20 mins so not a lot of time was spend on refinement).


***Edit***
I should perhaps point out that the script above was written as a demo for using two windows, that of course doesn't have to be the case. Simple adaptations can be made to have the same functionality in one control, on one window.
likethevegetable
Posts: 81
Joined: 05 May 2021, 08:54

Re: Autocomplete a combobox in GUI

08 Nov 2022, 20:50

Delta Pythagorean wrote:
07 Nov 2019, 05:55
You can use the following code to do autocomplete on a ComboBox and an Edit control respectively.
Read the function's documentation below to better understand how to use and control the functions to your liking.

Code: Select all

; ComboBox Control	----------------------------------------
Gui, Add, ComboBox, w640 gCbAutoComplete, Blue|Green|Red|Purple|Yellow|None

; Edit Control		----------------------------------------
Gui, Add, Edit, w640 HWNDhMyEdit
SHAutoComplete(hMyEdit)
Gui, Show
Return

/*
	Hotkey:
		Enter
	Description:
		Run the text entered into the Edit Control (hMyEdit) and close the script.
*/
Enter::
	ControlGetText, MyText,, ahk_id %hMyEdit%
	Run, %MyText%
GuiClose:
	ExitApp


/*
	=======================================================================================
	 Function:			SHAutoComplete
	 Description:		Auto-completes typed values in an edit with various options.
	 Usage:
		Gui, Add, Edit, w200 h21 hwndEditCtrl1
		SHAutoComplete(EditCtrl1)
	=======================================================================================
*/
SHAutoComplete(hEdit, Option := 0x20000000) {
	; https://bit.ly/335nOYt		For more info on the function.
	DllCall("ole32\CoInitialize", "Uint", 0)
	; SHACF_AUTOSUGGEST_FORCE_OFF (0x20000000)
	;	Ignore the registry default and force the AutoSuggest feature off.
	;	This flag must be used in combination with one or more of the SHACF_FILESYS* or SHACF_URL* flags.
	; AKA. It won't autocomplete anything, but it will allow functionality such as Ctrl+Backspace deleting a word.
	DllCall("shlwapi\SHAutoComplete", "Uint", hEdit, "Uint", Option)
	DllCall("ole32\CoUninitialize")
}

/*
	=======================================================================================
	 Function:			CbAutoComplete
	 Description:		Auto-completes typed values in a ComboBox.

	 Author:			Pulover [Rodolfo U. Batista]
	 Usage:
		Gui, Add, ComboBox, w200 h50 gCbAutoComplete, Billy|Joel|Samual|Jim|Max|Jackson|George
	=======================================================================================
*/
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%
}

; Required for: CbAutoComplete()
MakeLong(LoWord, HiWord) {
	Return, (HiWord << 16) | (LoWord & 0xffff)
}

; Required for: CbAutoComplete()
MakeShort(Long, ByRef LoWord, ByRef HiWord) {
	LoWord := Long & 0xffff, HiWord := Long >> 16
}
Hopefully this helps.


Long time but reviving this.. I'm slightly confused as to how this works for an Edit control--there are no provided words to choose from.
User avatar
Delta Pythagorean
Posts: 628
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Autocomplete a combobox in GUI

09 Nov 2022, 03:27

likethevegetable wrote:
08 Nov 2022, 20:50
Long time but reviving this.. I'm slightly confused as to how this works for an Edit control--there are no provided words to choose from.
The thing is with an edit control, the control doesn't have a set list of words to choose from. The ComboBox, however, does.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

likethevegetable
Posts: 81
Joined: 05 May 2021, 08:54

Re: Autocomplete a combobox in GUI

09 Nov 2022, 07:40

So how does it select which words to provide? Are they stored in memory somehow?
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Autocomplete a combobox in GUI

09 Nov 2022, 09:27

One more variant with IAutoComplete interface
viewtopic.php?p=103105#p103105

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CoffeeChaton, PAT06, Ragnar, RickC, RussF and 116 guests