smart ComboBox that updates as you type (HTML Help-style ComboBox)

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

smart ComboBox that updates as you type (HTML Help-style ComboBox)

31 May 2018, 19:02

- This is a simple script that creates a 'ComboBox' (actually 2 separate controls: an Edit control and a ListBox control).
- When you type into the Edit control it updates the selection in the ListBox control, in a manner which mimics the Index tab in HTML Help.
- (HTML Help is used for AutoHotkey Help (AutoHotkey.chm) and chm files in general for numerous programs.)
- It's quite possible that someone already came up with something similar, so do share any links. Cheers.

Code: Select all

;AHK v1.1
;smart ComboBox that updates as you type (HTML Help-style ComboBox)

Gui, New, +HwndhGui, MyWinTitle

;separate Edit control and listbox
vList := "January,February,March,April,May,June,July,August,September,October,November,December"
vList := StrReplace(vList, ",", "|")
Sort, vList, D|
Gui, Add, Edit, +gSubEdit
Gui, Add, ListBox, r12 +HwndhLbx, % vList
Gui, Show

;LB_ERR := -1
vErr := A_PtrSize=8 ? -1 : 0xFFFFFFFF
return

;==================================================

SubEdit:
ToolTip, % A_GuiEvent "`r`n" A_GuiControl
ControlGetFocus, vCtlClassNN, % "ahk_id " hGui
if !InStr(vCtlClassNN, "Edit")
	return
vText := A_GuiControl
if (vText = "")
	return
if (A_GuiEvent = "Normal")
{
	vTemp := "", vIndex := vIndexLast := 0
	Loop, Parse, % vText
	{
		vTemp .= A_LoopField
		SendMessage, 0x18F, % vIndex-1, % &vTemp,, % "ahk_id " hLbx ;LB_FINDSTRING := 0x18F
		vIndex := ErrorLevel
		if (vIndex = vErr)
			break
		vIndexLast := vIndex
	}
}
;ToolTip, % vIndexLast
Control, Choose, % vIndexLast+1,, % "ahk_id " hLbx
return

GuiClose:
ExitApp
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
just me
Posts: 8522
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: smart ComboBox that updates as you type (HTML Help-style ComboBox)

01 Jun 2018, 06:17

Something similar:

Code: Select all

; AHK v1.1
; 'Smart ComboBox' that updates as you type (HTML Help-style ComboBox) using separate Edit and ListBox controls
MonthList := "January|February|March|April|May|June|July|August|September|October|November|December"
Gui, Add, Edit, gSubEdit
Gui, Add, ListBox, xp y+0 wp r12 vComboList Sort, %MonthList%
Gui, Show, , MyWinTitle
Return
;==================================================
SubEdit:
   GuiControl, ChooseString, ComboList, %A_GuiControl%
Return
;==================================================
GuiClose:
ExitApp
But I prefer something like this:

Code: Select all

; AHK v1.1
; 'Smart ComboBox' that updates as you type (HTML Help-style ComboBox) using separate Edit and ListBox controls
MonthList := "January|February|March|April|May|June|July|August|September|October|November|December"
Gui, Add, Edit, gSubEdit vComboEdit
Gui, Add, ListBox, xp y+0 wp r12 vComboList hwndHLB Sort, %MonthList%
Gui, Show, , MyWinTitle
Return
;==================================================
SubEdit:
   GuiControlGet, ComboEdit
   GuiControl, Choose, ComboList, % LB_FindString(HLB, ComboEdit)
Return
;==================================================
GuiClose:
ExitApp
;==================================================
LB_FindString(HLB, Str) {
   Return (DllCall("SendMessage", "Ptr", HLB, "UInt", 0x018F, "Ptr", -1, "Ptr", &Str, "Int") + 1)
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: smart ComboBox that updates as you type (HTML Help-style ComboBox)

01 Jun 2018, 07:51

- Thanks just me. Your first example seems to have exactly the same functionality as my original example. Is there any difference?
- Your second example is interesting. There are 2 differences that I can see with HTML Help (although since the behaviour is stricter, it is good for some situations, to avoid selecting a false match):
- (1) In HTML Help if you set the Edit control to blank, whatever was last focused, remains focused. In this example, it removes the selection (nothing is selected).
- (2) In HTML Help if you set the Edit control to 'jum' that would select 'June' ('jum' failed, but 'ju' didn't), in this example it removes the selection (nothing is selected) since nothing actually begins with 'jum'.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
just me
Posts: 8522
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: smart ComboBox that updates as you type (HTML Help-style ComboBox)

01 Jun 2018, 08:21

jeeswg wrote:Your first example seems to have exactly the same functionality as my original example. Is there any difference?
I didn't notice any difference as yet.
... (although since the behaviour is stricter, it is good for some situations, to avoid selecting a false match) ...
Yes, that's why I prefer it.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: smart ComboBox that updates as you type (HTML Help-style ComboBox)

01 Jun 2018, 22:09

- I realised that there's one issue versus HTML Help. HTML Help makes the focused item, the top item.
- I'm not clear on how this can be done via ListBox messages.
- Possibly there's a direct approach, otherwise perhaps something like: to make item n the top item: count v visible items, and show item n+v-1.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: smart ComboBox that updates as you type (HTML Help-style ComboBox)

08 Jun 2018, 03:06

- It turns out that LB_SETTOPINDEX can set an item to appear at the top.
- I had misread the documentation, I thought it just made the item visible.
- In fact it will make the item the top item, unless you've reached the final page, in which case it makes the item as high as possible. The behaviour matches HTML Help.
- Here's a new example, this time using the chemical elements as the example list.
- It's a little bit jerky, in case there's a way to improve that.

Code: Select all

;AHK v1.1
;smart ComboBox that updates as you type (HTML Help-style ComboBox)

Gui, New, +HwndhGui, MyWinTitle
Gui, Font, s18

;separate Edit control and listbox

;List of chemical elements - Wikipedia
;https://en.wikipedia.org/wiki/List_of_chemical_elements
;As of August 2017, 118 chemical elements are identified.
vList := "
(Join|
hydrogen
helium
lithium
beryllium
boron
carbon
nitrogen
oxygen
fluorine
neon
sodium
magnesium
aluminium
silicon
phosphorus
sulfur
chlorine
argon
potassium
calcium
scandium
titanium
vanadium
chromium
manganese
iron
cobalt
nickel
copper
zinc
gallium
germanium
arsenic
selenium
bromine
krypton
rubidium
strontium
yttrium
zirconium
niobium
molybdenum
technetium
ruthenium
rhodium
palladium
silver
cadmium
indium
tin
antimony
tellurium
iodine
xenon
caesium
barium
lanthanum
cerium
praseodymium
neodymium
promethium
samarium
europium
gadolinium
terbium
dysprosium
holmium
erbium
thulium
ytterbium
lutetium
hafnium
tantalum
tungsten
rhenium
osmium
iridium
platinum
gold
mercury
thallium
lead
bismuth
polonium
astatine
radon
francium
radium
actinium
thorium
protactinium
uranium
neptunium
plutonium
americium
curium
berkelium
californium
einsteinium
fermium
mendelevium
nobelium
lawrencium
rutherfordium
dubnium
seaborgium
bohrium
hassium
meitnerium
darmstadtium
roentgenium
copernicium
nihonium
flerovium
moscovium
livermorium
tennessine
oganesson
)"
vList := StrReplace(vList, "sulfur", "sulfur [sulphur]")

Sort, vList, D|
Gui, Add, Edit, +gSubEdit
Gui, Add, ListBox, r12 +HwndhLbx, % vList
Gui, Show

;LB_ERR := -1
vErr := A_PtrSize=8 ? -1 : 0xFFFFFFFF
return

;==================================================

SubEdit:
;ToolTip, % A_GuiEvent "`r`n" A_GuiControl
ControlGetFocus, vCtlClassNN, % "ahk_id " hGui
if !InStr(vCtlClassNN, "Edit")
	return
vText := A_GuiControl
if (vText = "")
	return
if (A_GuiEvent = "Normal")
{
	vTemp := "", vIndex := vIndexLast := 0
	Loop, Parse, % vText
	{
		vTemp .= A_LoopField
		SendMessage, 0x18F, % vIndex-1, % &vTemp,, % "ahk_id " hLbx ;LB_FINDSTRING := 0x18F
		vIndex := ErrorLevel
		if (vIndex = vErr)
			break
		vIndexLast := vIndex
	}
}
;ToolTip, % vIndexLast
if (vIndexLastLast = vIndexLast)
	return
SendMessage, 0x197, % vIndexLast,,, % "ahk_id " hLbx ;LB_SETTOPINDEX := 0x197
Control, Choose, % vIndexLast+1,, % "ahk_id " hLbx
vIndexLastLast := vIndexLast
return

GuiClose:
ExitApp
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions”

Who is online

Users browsing this forum: crocodile, hitman, hotcheesesoup and 24 guests