Incremental search in listbox without sort

Put simple Tips and Tricks that are not entire Tutorials in this forum
jkeks
Posts: 60
Joined: 20 Oct 2019, 00:24
Contact:

Incremental search in listbox without sort

Post by jkeks » 12 Apr 2020, 09:08

this hack use edit.
Edit component let you do incremental search by any char in lines, not only first char. And thats cool !
you just need this little function:

Code: Select all

Gui Add, Edit,    ge1 x1   y1  w180 h20 r1 vSearch
Gui Add, ListBox,     x1   y21 w180 h400   vSite   hwndSite, %sites%

...

; on edit change
e1:
	Gui,Submit,NoHide
	sitesArr := StrSplit(sites, "|")
	newArr   := []
	newStr   := ""
	for k,v in sitesArr
	{
		if InStr(v, Search, false)>0
		{
			newArr.push(v)
		}
	}
	for k,v in newArr
		newStr .= "|" v
	GuiControl, , Site, % newStr
	GuiControl, Choose, site, 1
Return
I like it little hack :wave:

burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Incremental search in listbox without sort

Post by burque505 » 13 Apr 2020, 08:13

@jkeks, thank you. Working great here!
Regards,
burque505

burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Incremental search in listbox without sort

Post by burque505 » 14 Nov 2020, 18:19

@jkeks, I had forgotten how much I liked your hack. Here is a mod that clears the search box when the string isn't found. May not be necessary, but it's useful for me.

Code: Select all

sites := "cnn.com|autohotkey.com|google.com|robin-language.org"
sitesArr := StrSplit(sites, "|")
Gui Add, Edit,    ge1 x5   y5  w185 h20 r1 vSearch
Gui Add, ListBox,     x5   y+5 w185 h400   vSite   hwndSite, %sites%
Gui, Show, w195, jkeks

e1:
	Gui,Submit,NoHide
	if !(InStr(sites, Search, false)) {
	#Persistent
	ToolTip, Search string '%Search%' not found.
	SetTimer, RemoveToolTip, 2000
	GuiControl, , Search
	return
	}
	sitesArr := StrSplit(sites, "|")
	newArr   := []
	newStr   := ""
	for k,v in sitesArr
	{
		if InStr(v, Search, false)>0
		{
			newArr.push(v)
		}
	}
	for k,v in newArr
		newStr .= "|" v
	GuiControl, , Site, % newStr
	GuiControl, Choose, site, 1
Return

GuiClose:
Escape::
ExitApp


RemoveToolTip:
ToolTip
return
Regards,
burque505

Greast
Posts: 71
Joined: 24 Oct 2020, 19:01

Re: Incremental search in listbox without sort

Post by Greast » 14 Nov 2020, 19:22

@burque505
Hi! Sorry to ask that, what does this script do exactly?
“You see things; you say, 'Why?' But I dream things that never were; and I say 'Why not?”
― George Bernard Shaw

burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Incremental search in listbox without sort

Post by burque505 » 14 Nov 2020, 20:00

@Greast, at the moment it doesn't do anything except demonstrate a fuzzy search. You'll have to handle a GuiEvent to actually see anything, here's a simple MsgBox example.

Code: Select all

sites := "cnn.com|autohotkey.com|google.com|robin-language.org"
sitesArr := StrSplit(sites, "|")
Gui Add, Edit,    ge1 x5   y5  w185 h20 r1 vSearch
Gui Add, ListBox, ge2    x5   y+5 w185 h400   vSite   hwndSite, %sites%
Gui, Show, w195, jkeks

e1:
	Gui,Submit,NoHide
	if !(InStr(sites, Search, false)) {
	#Persistent
	ToolTip, Search string '%Search%' not found.
	SetTimer, RemoveToolTip, 2000
	GuiControl, , Search
	return
	}
	sitesArr := StrSplit(sites, "|")
	newArr   := []
	newStr   := ""
	for k,v in sitesArr
	{
		if InStr(v, Search, false)>0
		{
			newArr.push(v)
		}
	}
	for k,v in newArr
		newStr .= "|" v
	GuiControl, , Site, % newStr
	GuiControl, Choose, site, %A_%
Return

e2:
	if (A_GuiEvent = "DoubleClick")
	{
			ControlGet, listText, Choice, ,ListBox1, jkeks
			msgbox %listText%
	}

GuiClose:
Escape::
ExitApp


RemoveToolTip:
ToolTip
return
Regards,
burque505

Greast
Posts: 71
Joined: 24 Oct 2020, 19:01

Re: Incremental search in listbox without sort

Post by Greast » 14 Nov 2020, 20:06

@burque505
Actually, it is a good code.
“You see things; you say, 'Why?' But I dream things that never were; and I say 'Why not?”
― George Bernard Shaw

burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Incremental search in listbox without sort

Post by burque505 » 15 Nov 2020, 11:10

@Greast, :thumbup: Thanks! Credit goes to @jkeks, though.
Regards,
burque505

Post Reply

Return to “Tips and Tricks (v1)”