Private Sub ListBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ListBox1.KeyPress ListBox1.Sorted = True For i As Integer = 0 To ListBox1.Items.Count - 1 ListBox1.SetSelected(i, True) If ListBox1.SelectedItem.ToString.StartsWith(e.KeyChar) Then Exit For Next End Sub
Pourquoi ne pas gérer l'évènement KeyPress?
ListBox1.Sorted = True, cependant lorsque l'on fait ça, ça tri bien les displaymember mais pas les valuemember.... et on se retrouve avec des incohérences car si on clique sur Roger Rabbit (valuemember 1), on va obtenir un valuemember qui n'est pas le bon... du fait qu'ils ne sont pas triés en même temps.
Try y = txtSearch.Text x = ListBox1.FindString(y) 'retourne le numéro du premier élément commençant par "x". ListBox1.SetSelected(x, True) 'la ligne contenant x devient bleue Catch ' l'ajout d'un try/catch permet d'éviter les plantages avec les extensions inexistantes MessageBox.Show("Extension non valide!", "Recherche extension", MessageBoxButtons.OK, MessageBoxIcon.Information) Exit Sub End Try
displaymember mais pas les valuemember
x = ListBox1.FindString(y)
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionAvec cette listbox j'obtiens donc tous les noms de ma BDD, mais j'aimerais créer des boutons pour que l'utilisateur puisse trier les noms et n'obtenir que ceux qu'il rechercher
Public Class Form1 Dim Bouton(4) As Button Dim Liste As ListBox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'pour le test: ListBox1.Items.Add("alkjlkj") ListBox1.Items.Add("boiuoiu") ListBox1.Items.Add("cytrurt") ListBox1.Items.Add("diyoiuy") ListBox1.Items.Add("ehfuyiy") ListBox1.Items.Add("fknknpn") ListBox1.Items.Add("gokjpojopi") ListBox1.Items.Add("hezareza") ListBox1.Items.Add("ihbihbkj") ListBox1.Items.Add("abvcnc") ListBox1.Items.Add("btreytre") ListBox1.Items.Add("ckpnpl") ListBox1.Items.Add("dezaeza") ListBox1.Items.Add("ejnojini") ListBox1.Items.Add("feztretre") ListBox1.Items.Add("gytreyte") ListBox1.Items.Add("hezare") ListBox1.Items.Add("idwgw") For Rt As Integer = 0 To 3 Bouton(Rt) = New Button Bouton(Rt).Top = Rt * Bouton(0).Height If Rt 0 Then Bouton(Rt).Text "all" If Rt 1 Then Bouton(Rt).Text "abc" If Rt 2 Then Bouton(Rt).Text "def" If Rt 3 Then Bouton(Rt).Text "ghi" Me.Controls.Add(Bouton(Rt)) AddHandler Bouton(Rt).Click, AddressOf Bouton_Click Next Liste = New ListBox Liste.Items.AddRange(ListBox1.Items) End Sub Private Sub Bouton_Click(ByVal Sender As Object, ByVal e As System.EventArgs) Dim St As String = Sender.Text If St = "all" Then ListBox1.Items.Clear() ListBox1.Items.AddRange(Liste.Items) Else ListBox1.Items.Clear() For i As Integer = 0 To Liste.Items.Count - 1 For Each Caract As Char In St If Liste.Items(i).ToString.StartsWith(Caract) Then ListBox1.Items.Add(Liste.Items(i)) Next Caract Next i End If ListBox1.Sorted = True End Sub End Class
On a déjà le tri alphabétique, maintenant on va réfléchir au tri numérique
VALUEMEMBER | DISPLAYMEMBER 17 | Dupont 569 | Valin 40 | Boubet