查找字符串
#include <GuiListBox.au3>
_GUICtrlListBox_FindString($hWnd, $sText [, $fExact = False])
$hWnd | 控件的控件ID/句柄 |
$sText | 要搜索的字符串 |
$fExact | [可选参数] 是否精确匹配 |
成功: | 返回项目的 0 基索引 |
失败: | 返回 -1 |
#include <GuiListBox.au3>
#include <GUIConstantsEx.au3>
$Debug_LB = False ;检查传递给 ListBox 函数的类名, 设置为True并输出到一个控件的句柄,用于检查它是否工作
Example()
Example2()
Func Example()
Local $iIndex, $hListBox
; 创建 GUI
GUICreate("List Box Find String", 400, 296)
$hListBox = GUICtrlCreateList("", 2, 2, 396, 296)
GUISetState()
; 添加字符串
_GUICtrlListBox_BeginUpdate($hListBox)
For $iI = 1 To 9
_GUICtrlListBox_AddString($hListBox, StringFormat("%03d : Random string", Random(1, 100, 1)))
Next
_GUICtrlListBox_InsertString($hListBox, "eXaCt tExT", 3)
_GUICtrlListBox_EndUpdate($hListBox)
; Find an item
$iIndex = _GUICtrlListBox_FindString($hListBox, "exa")
_GUICtrlListBox_SetCurSel($hListBox, $iIndex)
; 循环直到用户退出
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc ;==>Example
Func Example2()
Local $iIndex, $hListBox
; 创建 GUI
GUICreate("List Box Find String Exact", 400, 296)
$hListBox = GUICtrlCreateList("", 2, 2, 396, 296)
GUISetState()
; 添加字符串
_GUICtrlListBox_BeginUpdate($hListBox)
For $iI = 1 To 9
_GUICtrlListBox_AddString($hListBox, StringFormat("%03d : Random string", Random(1, 100, 1)))
Next
_GUICtrlListBox_InsertString($hListBox, "eXa", 2)
_GUICtrlListBox_InsertString($hListBox, "eXaCt tExT", 3)
_GUICtrlListBox_EndUpdate($hListBox)
; Find an item
$iIndex = _GUICtrlListBox_FindString($hListBox, "exact text", True)
_GUICtrlListBox_SetCurSel($hListBox, $iIndex)
; 循环直到用户退出
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
EndFunc ;==>Example2