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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    '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.
    Friend WithEvents lvwFiles As System.Windows.Forms.ListView
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.lvwFiles = New System.Windows.Forms.ListView
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'lvwFiles
        '
        Me.lvwFiles.FullRowSelect = True
        Me.lvwFiles.HideSelection = False
        Me.lvwFiles.Location = New System.Drawing.Point(32, 32)
        Me.lvwFiles.Name = "lvwFiles"
        Me.lvwFiles.Size = New System.Drawing.Size(408, 344)
        Me.lvwFiles.TabIndex = 0
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(88, 400)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = "TextBox1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(480, 477)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.lvwFiles)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
	' Add a few items to the listview
        For l As Integer = 0 To 4
            Dim s As String = "Folder " & l.ToString.PadRight(5, "0")
            Me.lvwFiles.Items.Add(s)
        Next

        For l As Integer = 0 To 4
            Dim s As String = "A File " & l.ToString.PadRight(5, "0")
            Me.lvwFiles.Items.Add(s)
        Next

        For l As Integer = 0 To 4
            Dim s As String = "File " & l.ToString.PadRight(5, "0")
            Me.lvwFiles.Items.Add(s)
        Next

        For l As Integer = 0 To 4
            Dim s As String = "Old File " & l.ToString.PadRight(5, "0")
            Me.lvwFiles.Items.Add(s)
        Next

    End Sub

    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
        ' Convert the key pressed to lower case 
        Dim KeyPressed As String = e.KeyData.ToString.ToLower

        ' If an item is selected then start the search from the next item in the 
        ' list.  If no item is selected start the search from the beginning.
        Dim CurrentIndex As Integer = 0

        If Me.lvwFiles.SelectedItems.Count > 0 Then
            CurrentIndex = Me.lvwFiles.SelectedItems(0).Index()
        End If

        ' Set up a NewIndex with an invalid (negative) value.
        Dim NewIndex As Integer = -1

        ' Item name
        Dim FirstLetterOfItemName As String

        ' Start searching from the next item until we wither get a match or 
        ' we reach the last item in the list.
        For i As Integer = (CurrentIndex + 1) To Me.lvwFiles.Items.Count - 1
            ' Convert the first character of current item name to lower case
            FirstLetterOfItemName = Me.lvwFiles.Items(i).Text.Substring(0, 1).ToLower

            If FirstLetterOfItemName.Equals(KeyPressed) Then
                ' Match found.  Note the index and exit the loop
                NewIndex = i

                Exit For

            End If

        Next

        ' If new index is still -1 then we haven't found an item.  Start 
        ' the search from the beginning of the list view.
        If NewIndex = -1 Then
            For i As Integer = 0 To CurrentIndex
                ' Convert the first character of current item name to lower case
                FirstLetterOfItemName = Me.lvwFiles.Items(i).Text.Substring(0, 1).ToLower

                If FirstLetterOfItemName.Equals(KeyPressed) Then
                    ' Match found.  Note the index and exit the loop
                    NewIndex = i

                    Exit For

                End If

            Next

        End If

        ' If NewIndex is set select the item
        If NewIndex > -1 Then
            ' Unselect all currently selected items
            For Each Item As ListViewItem In Me.lvwFiles.SelectedItems
                Item.Selected = False
            Next

            ' Select required item
            Me.lvwFiles.Items(NewIndex).Selected = True

        End If

    End Sub

End Class
