Option Explicit Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Const LB_FINDSTRING = &H18F Private Const LB_SETSEL = &H185 '用于单选ListBox Private Const LB_SETTOPINDEX = &H197 Private Const LB_SETCURSEL = &H186 '用于多选ListBoxPrivate Sub Command1_Click() Dim nIndex As Long nIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal "ttee") If nIndex < 0 Then Exit Sub
List1.listindex=i
List1.TopIndex = i
end function
LB_FINDSTRING
LB_SETSEL
LB_SETTOPINDEX
先用LB_FINDSTRING找到"ttee"索引,再用LB_SETSEL选中,再用LB_SETTOPINDEX移到顶端。
看你五角,还需要我写代码吗?
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Const LB_FINDSTRING = &H18F
Private Const LB_SETSEL = &H185 '用于单选ListBox
Private Const LB_SETTOPINDEX = &H197
Private Const LB_SETCURSEL = &H186 '用于多选ListBoxPrivate Sub Command1_Click()
Dim nIndex As Long
nIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal "ttee")
If nIndex < 0 Then Exit Sub
SendMessage List1.hwnd, LB_SETCURSEL, nIndex, 0 '再此以单选ListBox为例
SendMessage List1.hwnd, LB_SETTOPINDEX, nIndex, 0
End SubPrivate Sub Form_Load()
List1.AddItem "a1"
List1.AddItem "a2"
List1.AddItem "a3"
List1.AddItem "a4"
List1.AddItem "a5"
List1.AddItem "a6"
List1.AddItem "a7"
List1.AddItem "a8"
List1.AddItem "ttee"
List1.AddItem "a9"
End Sub
Private Const LB_FINDSTRING = &H18F
Private Const LB_SETSEL = &H185 '用于单选ListBox
Private Const LB_SETTOPINDEX = &H197
Private Const LB_SETCURSEL = &H186 '用于多选ListBox你是怎么知道的 你怎么知道 用LB_SETEL这些东西呢????虚心求教////.....谢谢了先
————————————————————
MSDN中搜List Box Messages吧你怎么知道 用LB_SETEL这些东西呢????
——————————————————————————————
VB插件里有个API Viewer,打开载入WIN32API.TXT后,API Type框中选择Constants后,键入
LB_... ,出来一堆让你选,选择后复制到VB IDE中就行了。