Option Strict On Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents ListBox1 As System.Windows.Forms.ListBox Friend WithEvents bttnFind As System.Windows.Forms.Button Friend WithEvents bttnPopulate As System.Windows.Forms.Button 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Private Sub InitializeComponent() Me.ListBox1 = New System.Windows.Forms.ListBox() Me.bttnFind = New System.Windows.Forms.Button() Me.bttnPopulate = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'ListBox1 ' Me.ListBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.ListBox1.ItemHeight = 14 Me.ListBox1.Location = New System.Drawing.Point(8, 8) Me.ListBox1.Name = "ListBox1" Me.ListBox1.Size = New System.Drawing.Size(200, 256) Me.ListBox1.TabIndex = 0 ' 'bttnFind ' Me.bttnFind.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.bttnFind.Location = New System.Drawing.Point(216, 48) Me.bttnFind.Name = "bttnFind" Me.bttnFind.Size = New System.Drawing.Size(152, 32) Me.bttnFind.TabIndex = 1 Me.bttnFind.Text = "Find Item" ' 'bttnPopulate ' Me.bttnPopulate.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.bttnPopulate.Location = New System.Drawing.Point(216, 8) Me.bttnPopulate.Name = "bttnPopulate" Me.bttnPopulate.Size = New System.Drawing.Size(152, 32) Me.bttnPopulate.TabIndex = 2 Me.bttnPopulate.Text = "Populate List" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(392, 277) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bttnPopulate, Me.bttnFind, Me.ListBox1}) Me.Name = "Form1" Me.Text = "ListBoxFind Demo" Me.ResumeLayout(False) End Sub #End Region Private Sub bttnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPopulate.Click Dim wordLen As Integer Dim Nwords As Integer = 9999 Dim rnd As System.Random rnd = New System.Random() Dim rndChar As Char Dim thisWord As String Dim i, j As Integer ListBox1.BeginUpdate() For i = 0 To Nwords wordLen = CInt(rnd.NextDouble * 20 + 1) thisWord = "" For j = 0 To wordLen rndChar = Chr(65 + CInt(rnd.Next(0, 25))) thisWord = thisWord & rndChar Next ListBox1.Items.Add(thisWord) Next ListBox1.EndUpdate() End Sub Private Sub bttnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnFind.Click Dim srchWord As String Dim wordIndex As Integer srchWord = InputBox("Enter word to search for") wordIndex = ListBox1.FindStringExact(srchWord) If wordIndex >= 0 Then MsgBox("Index = " & wordIndex.ToString & " =" & _ (ListBox1.Items(wordIndex)).ToString, , "EXACT MATCH") ListBox1.TopIndex = wordIndex ListBox1.SelectedIndex = wordIndex Else wordIndex = ListBox1.FindString(srchWord) If wordIndex >= 0 Then MsgBox("Index = " & wordIndex.ToString & " =" & _ (ListBox1.Items(wordIndex)).ToString, , "NEAR MATCH") ListBox1.TopIndex = wordIndex ListBox1.SelectedIndex = wordIndex Else MsgBox("Item " & srchWord & " is not in the list") End If End If End Sub End Class