If searchstring <> String.Empty Then ' Find the item in the list and store the index to the item. Dim index As Integer = ListBox1.FindString(searchstring) ' Determine if a valid index is returned. Select the item if it is valid. If index <> -1 Then ListBox1.SetSelected(index, True) MsgBox("Name : " & ListBox1.SelectedItem) Else MessageBox.Show("The search string did not match any names") End If End If
Private Sub testlistbox(ByVal searchstring As String)
Dim num As Integer
If Integer.TryParse(searchstring, num) Then
For iter As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(iter).ToString.StartsWith("allo") AndAlso ListBox1.Items(iter).ToString.Contains(searchstring) Then
ListBox1.SetSelected(iter, True)
MessageBox.Show("Name : " & ListBox1.SelectedItem.ToString)
End If
Next
Else
For iter As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(iter).ToString.StartsWith(searchstring) Then
ListBox1.SetSelected(iter, True)
MessageBox.Show("Name : " & ListBox1.SelectedItem.ToString)
End If
Next
End If
End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim listLength As Integer = (ListBox1.Items.Count - 1)
Dim i As Integer
Dim listString As String
If TextBox1.TextLength <= 2 Then
MessageBox.Show("Vous devez saisir 3 caractères minimum!", "Sélection", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
For i = 0 To listLength
listString = CStr(ListBox1.Items.Item(i))
If CBool(InStr(listString.ToLower, TextBox1.Text.ToLower)) Then
ListBox1.SelectedItems.Add(listString)
MessageBox.Show("Nom : " & ListBox1.SelectedItem.ToString, "Recherche", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Next
listString = Nothing
End Sub
Private Sub testlistbox(Optional ByVal prenom As String = Nothing, Optional ByVal nom As String = Nothing, Optional ByVal num As String = Nothing)
Dim found As Boolean = False
Dim searchname As String = String.Empty
Dim foundstr As String = String.Empty
Dim numero As Integer
If prenom <> String.Empty Then
searchname = prenom
ElseIf nom <> String.Empty Then
searchname = nom
End If
'recherche le nom ou prenom selon le numéro
If num <> String.Empty AndAlso searchname <> String.Empty Then
If Integer.TryParse(num, numero) Then
For iter As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(iter).ToString.StartsWith(searchname) AndAlso ListBox1.Items(iter).ToString.Contains(num) Then
ListBox1.SetSelected(iter, True)
MessageBox.Show("Name : " & ListBox1.SelectedItem.ToString)
Exit Sub
End If
Next
End If
ElseIf searchname <> String.Empty Then
For iter As Integer = 0 To ListBox1.Items.Count - 1
If prenom <> String.Empty Then
If ListBox1.Items(iter).ToString.StartsWith(searchname) Then
ListBox1.SetSelected(iter, True)
foundstr &= ListBox1.SelectedItem.ToString & Environment.NewLine
found = True
End If
Else
If ListBox1.Items(iter).ToString.Contains(searchname) Then
ListBox1.SetSelected(iter, True)
foundstr &= ListBox1.SelectedItem.ToString & Environment.NewLine
found = True
End If
End If
Next
End If
If found Then
MessageBox.Show("Name : " & Environment.NewLine & foundstr)
Else
MessageBox.Show("Aucun resultat", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub
testlistbox("prenom")'pour chercher selon un numero
testlistbox("prenom", ,"4")'pour chercher selon le nom de famille
testlistbox( , "familyname")