2012-02-13 7 views
5

私はlstSerialというリストボックスとtxtSerialsというテキストボックスを持っています。私がしたいのは、txtSerialsに入力された文字列の検索lstSerialです。私はMicrosoft Visual Basic 6.0でVB6を使用していますが、ドキュメントを探すのに苦労しています。指定した文字列のlistBoxを検索するVB6

ありがとうございました。

答えて

7

AlexKの答え@技術的に正しいです - はい - それは動作しますが、それは行くための好ましい方法ではありません。この非常に目的のためのAPI呼び出しがあります:

Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" _ 
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As _ 
    Integer, ByVal lParam As Any) As Long 

'constants for searching the ListBox 
Private Const LB_FINDSTRINGEXACT = &H1A2 
Private Const LB_FINDSTRING = &H18F 

'function to get find an item in the Listbox 
Public Function GetListBoxIndex(hWnd As Long, SearchKey As String, Optional FindExactMatch As Boolean = True) As Long 

    If FindExactMatch Then 
     GetListBoxIndex = SendMessage(hWnd, LB_FINDSTRINGEXACT, -1, ByVal SearchKey) 
    Else 
     GetListBoxIndex = SendMessage(hWnd, LB_FINDSTRING, -1, ByVal SearchKey) 
    End If 

End Function 

ですから、これやりたい:私もこの方法を使用

lstSerial.ListIndex = GetListBoxIndex(lstSerial.hWnd, txtSerials.Text) 

Source

+0

+1これははるかに速く実行されます – MarkJ

6

文書; http://msdn.microsoft.com/en-us/library/aa267225(v=VS.60).aspx

dim find as string,i as long,found as boolean 
find=txtSerials.text 

for i=0 to lstserial.listcount - 1 
    if strcomp(find, lstSerial.list(i), vbTextcompare)=0 then 
     found = true 
     lstSerial.setfocus 
     lstSerial.listindex= i 
     exit for 
    end if 
next 

if not found then msgbox "not found ..." 
+1

+1を、私はそれがパフォーマンスだ見つけたことがありません悪いのでAPI呼び出しよりも好きです –

関連する問題