Find, FindExact, FindNext Method

Purpose

Searches a string in a ListBox or ComboBox Ocx control.

Syntax

i = object.Find(search$)

i = object.FindNext(start, search$)

i = object.FindExact(search$)

I, start:iexp
object:ListBox or ComboBox

Description

Find searches for a string and FindNext the next match of that string, as part of a list item in a ListBox or ComboBox Ocx control. With FindNext, if the string is not found in the remainder of the list, it will start again from the beginning of the list until a match is made or the start item is reached.

FindExact searches for an exact (but non-case-sensitive) match of a string with the whole of a list item in a ListBox or ComboBox Ocx control.

The return value is the index of the item.

Example

Dim list As Variant, n As Int32

list = Array("Matthew", "Mark", "Luke", "John", "Paul")

Ocx ListBox lbx = "", 10, 10, 150, 400 : lbx.Sorted = False

For n = 1 To 40 : lbx.AddItem list(Random(5)) & Iif(Random(2) = 0, " 2", "") : Next n

Ocx Label lbl = "String to Find:", 220, 10, 150, 15 : lbl.BackColor = RGB(255, 255, 255)

Ocx ComboBox cmb = "", 220, 30, 150, 22 : cmb.Style = 2

For n = 0 To 4 : cmb.AddItem list(n) : Next n : cmb.ListIndex = 0

Ocx Command cmd1 = "Find", 190, 60, 60, 22

Ocx Command cmd2 = "Find Next", 260, 60, 60, 22

Ocx Command cmd3 = "Find Exact", 330, 60, 60, 22

Do : Sleep  : Until Me Is Nothing

 

Sub cmd1_Click

// lbx.ListIndex = lbx.Find(cmb.List(cmb.ListIndex)) does not seem to work

Local a$ = cmb.List(cmb.ListIndex)

lbx.ListIndex = lbx.Find(a$)

EndSub

 

Sub cmd2_Click

Local a$ = cmb.List(cmb.ListIndex)

lbx.ListIndex = lbx.FindNext(lbx.ListIndex, a$)

EndSub

 

Sub cmd3_Click

Local a$ = cmb.List(cmb.ListIndex)

lbx.ListIndex = lbx.FindExact(a$)

EndSub

Remarks

See Also

ListBox, ComboBox

{Created by Sjouke Hamstra; Last updated: 17/11/2017 by James Gaite}