home *** CD-ROM | disk | FTP | other *** search
- VERSION 5.00
- Begin VB.Form ch02
- Caption = "ListBox Search Example"
- ClientHeight = 6030
- ClientLeft = 1095
- ClientTop = 1515
- ClientWidth = 6720
- LinkTopic = "Form1"
- PaletteMode = 1 'UseZOrder
- ScaleHeight = 6030
- ScaleWidth = 6720
- Begin VB.CommandButton Command3
- Caption = "Performance"
- Height = 495
- Left = 3360
- TabIndex = 3
- Top = 1920
- Width = 1575
- End
- Begin VB.CommandButton Command2
- Caption = "Find String"
- Height = 495
- Left = 3360
- TabIndex = 2
- Top = 1320
- Width = 1575
- End
- Begin VB.CommandButton Command1
- Caption = "Load List"
- Height = 495
- Left = 3360
- TabIndex = 1
- Top = 720
- Width = 1575
- End
- Begin VB.ListBox List1
- Height = 2565
- Left = 360
- TabIndex = 0
- Top = 720
- Width = 2175
- End
- Attribute VB_Name = "ch02"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- Option Explicit
- Const RNDSEARCH% = 50
- ' Fill the listbox with some sample text
- Sub Command1_Click()
- Dim x%
- For x% = 1 To 500
- List1.AddItem "Listbox entry #" + Str$(x%)
- Next x%
- End Sub
- ' This code demonstrates a fast search for a string in
- ' the list box
- Sub Command2_Click()
- Dim hw&, t&
- hw& = List1.hWnd ' Get the window handle for the list box
- t& = SendMessageByString&(hw&, LB_FINDSTRINGEXACT, -1, "Listbox entry # 200")
- MsgBox "Listbox entry found at " + Str$(t&)
- End Sub
- Private Sub Command3_Click()
- Dim sequence(RNDSEARCH) As String
- Dim si As SYSTEMTIME
- Dim starttime As FILETIME
- Dim endtime As FILETIME
- Dim search1time&
- Dim search2time&
- Dim x%, searchcount%
- Dim maxlistentry%
- Dim dl&
- Screen.MousePointer = 11
- ' Load the array with a sequence
- maxlistentry% = List1.ListCount ' It's faster to use a variable than a property
- For x% = 1 To RNDSEARCH
- sequence(x%) = "Listbox entry #" & Str$(Int(Rnd * maxlistentry%))
- Next x%
- ' Now search using code
- GetSystemTime si
- dl& = SystemTimeToFileTime(si, starttime)
- For searchcount% = 1 To RNDSEARCH
- For x% = 1 To maxlistentry%
- If List1.List(x%) = sequence(searchcount%) Then Exit For
- Next x%
- Next searchcount%
- GetSystemTime si
- dl& = SystemTimeToFileTime(si, endtime)
- ' Find the difference in time
- search1time = FileTimeDifference(endtime, starttime)
- GetSystemTime si
- dl& = SystemTimeToFileTime(si, starttime)
- For searchcount% = 1 To RNDSEARCH
- dl& = SendMessageByString&(List1.hWnd, LB_FINDSTRINGEXACT, -1, sequence(searchcount%))
- Next searchcount%
- GetSystemTime si
- dl& = SystemTimeToFileTime(si, endtime)
- ' Find the difference in time
- search2time = FileTimeDifference(endtime, starttime)
- Screen.MousePointer = 0
- MsgBox "VB search: " & search1time & "ms API search: " & search2time & "ms"
- End Sub
-