[AHK]Listbox with incremental search

61 篇文章 6 订阅

可以根据文本框中的输入内容,实时动态从列表中搜索并定位所搜索内容。

AHK V1代码


#Requires AutoHotkey v1.0
Gui Add, Edit, w300 h20 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

AHK V2代码

#Requires AutoHotkey v2.0

; 创建GUI
MyGui := Gui()
MyGui.Add("Edit", "w300 h20 vsearchedString", "").OnEvent("Change", IncrementalSearch)
MyGui.Add("ListBox", "vchoice w300 h250 hscroll vscroll", []).OnEvent("DoubleClick", ListBoxClick)
MyGui.Add("Button", "Default", "OK").OnEvent("Click", ListBoxClick)

FillListBox()
MyGui.Show()


IncrementalSearch(*)
{
    searchedString := MyGui["searchedString"].Value
    len := StrLen(searchedString)
    itemNb := 1
    
    Loop Parse, listContent, "|"
    {
        if (SubStr(A_LoopField, 1, len) = searchedString)
        {
            itemNb := A_Index
            Break
        }
    }

    ToolTip(searchedString " (" itemNb ")")
    SetTimer () => ToolTip(), -1000
    MyGui["choice"].Choose(itemNb)
}

ListBoxClick(*)
{
    choice := MyGui["choice"].Text
    MsgBox("Choice: " choice)
}

FillListBox()
{
    global listContent := "car|bicyle|train|plane|road|railway station|track|airport|control tower|wheel|" 
        . "red|green|pink|blue|grey|silver|black|yellow|brown|white|"
        ; ... 其余列表内容 ...
        . "Gui Add|Gui Show|Gui Submit|Gui Hide|Gui Destroy"
    
    MyGui["choice"].Add(StrSplit(listContent, "|"))
}

; 设置关闭事件
MyGui.OnEvent("Close", (*) => ExitApp())
MyGui.OnEvent("Escape", (*) => ExitApp())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值