Správný postup: ---------------- Do deklarační části vložit
Dim foundAtIndex As Integer
Private Function FindItemWithText(ByVal lv As ListView, ByVal text As String, ByVal startIndex As Integer, Optional ByVal subItemsText As Boolean = True) As Integer
Dim foundItem As ListViewItem = lv.FindItemWithText(text.Trim, subItemsText, startIndex)
If foundItem IsNot Nothing Then
For Each item As ListViewItem In lv.SelectedItems
item.Selected = False
Next
foundItem.Selected = True
foundItem.EnsureVisible()
Return foundItem.Index
Else
MessageBox.Show("Vyhledávané slovo není v seznamu.")
End If
End Function
Potom do událostí Textboxu a Tlačítka(Buttonu) vložit
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
On Error Resume Next
Dim startFrom As Integer = IIf(foundAtIndex > 0, foundAtIndex + 1, 0)
foundAtIndex = FindItemWithText(ListView1, TextBox1.Text, startFrom)
btnFindAndFindNext.Text = IIf(foundAtIndex = 0, "Další", "Hledat další")
End If
End Sub
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
foundAtIndex = 0
btnFindAndFindNext.Text = "Hledat"
End Sub
Private Sub btnFindAndFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindAndFindNext.Click
On Error Resume Next
Dim startFrom As Integer = IIf(foundAtIndex > 0, foundAtIndex + 1, 0)
foundAtIndex = FindItemWithText(ListView1, TextBox1.Text, startFrom)
btnFindAndFindNext.Text = IIf(foundAtIndex = 0, "Další", "Hledat další")
End Sub
Doufám, že to někomu dalšímu taky pomůže v řešení tohoto problému. :)
|