带有检索功能的列表框示例

;~ http://www.autohotkey.com/board/topic/2391-listbox-with-incremental-search/ Gui Add, Edit, w300 h30 vsearchedString g

带有检索功能的列表框示例

;~ http://www.autohotkey.com/board/topic/2391-listbox-with-incremental-search/
Gui Add, Edit, w300 h30 vsearchedString gIncrementalSearch
Gui Add, ListBox, vchoice gListBoxClick w300 h250 hscroll vscroll
Gui Add, Button, gListBoxClick Default, OK
Gosub FillListBox
Gui Show
Return
 
IncrementalSearch:
	Gui Submit, NoHide
	len := StrLen(searchedString)
	itemNb := 1
	Loop Parse, listContent, |
	{
		StringLeft part, A_LoopField, len
;~ 		Tooltip %part% (%A_LoopField%)
;~ 		Sleep 500
		If (part = searchedString)
		{
			itemNb := A_Index
			Break
		}
	}
 
	Tooltip %searchedString% (%itemNb%)
	SetTimer HideTooltip, 1000
	GuiControl Choose, choice, %itemNb%
Return
 
HideTooltip:
	SetTimer HideTooltip, Off
	Tooltip
Return
 
ListBoxClick:
	Gui Submit, NoHide
	MsgBox Choice: %choice%
Return
 
GuiClose:
GuiEscape:
ExitApp
 
FillListBox:
	listContent =
( Join|
car|bicyle|train|plane|road|railway station|track|airport|control tower|wheel
red|green|pink|blue|grey|silver|black|yellow|brown|white
hair|nose|eye|ear|face|mustache|neck|collar|arm|hand|forearm|forehead
finger|thumb|palm|back|stomach|leg|thigh|foot|toe
shoe|sock|stocking|trousers|jumpsuit|skirt|blouse|dress|shirt|tie|necklace|earring
Sun|Moon|Mercure|Venus|Earth|Mars|Jupiter|Saturn|Neptun|Pluto
Africa|America|Antarctica|Asia|Australia|Europe
Canada|USA|Mexico|Guatemala|Honduras|Cuba
Gui Add|Gui Show|Gui Submit|Gui Hide|Gui Destroy
)
	GuiControl, , choice, %listContent%
Return
 

说明

这个示例演示了带增量检索(incremental search)功能的 ListBox 列表框:界面上方是一个编辑框(Edit),下方是一个列表框(ListBox),当你在编辑框中输入文字时,脚本会根据输入实时高亮/选中列表中匹配的项,实现类似搜索引擎的即时过滤效果。它的核心原理并不复杂:在 IncrementalSearch 子程序里取得编辑框当前输入,用 StrLen 得到长度,然后Loop Parse逐项解析列表内容,用 StringLeft 取每项开头与输入比较,命中后通过 GuiControl Choose 选中对应项,并用 Tooltip 显示当前命中位置。

适用场景:需要在一大串候选项中快速定位数据的工具(比如城市选择、单词表、设备列表等),比从头滚动查找高效得多。

几点补充:

  • 该脚本为 AutoHotkey v1 写法;在 v2 中对应命令改为函数式,例如 StrLen()、GuiControl()、StringLeft() 用 SubStr() 取代,GUI 用 MyGui := Gui() 对象化创建。
  • 列表数据用管道符 | 分隔,通过 GuiControl 一次性填入列表框。
  • 若输入在列表中不存在,可不做处理或给出提示,避免误选中。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA捐赠
共{{data.count}}人
人已捐赠
教程

后台发送按键和操作鼠标 By FeiYue

2020-4-1 12:59:38

案例

md5pass自己的密码生成方案

2020-4-2 6:09:15

❯
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索