Liste sans redondance

cs_calimero93 Messages postés 29 Date d'inscription vendredi 14 mars 2003 Statut Membre Dernière intervention 10 décembre 2004 - 16 juin 2004 à 11:23
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 - 16 juin 2004 à 11:56
j'aimerai enlever les double dans une list

3 réponses

econs Messages postés 4030 Date d'inscription mardi 13 mai 2003 Statut Modérateur Dernière intervention 23 décembre 2008 23
16 juin 2004 à 11:45

Avec une liste dont la propriété Sorted est à True ...

Private Sub Command1_Click()
Dim i As Long
For i = List1.ListCount To 1 Step -1
If List1.List(i - 1) = List1.List(i) Then
List1.RemoveItem (i - 1)
i = i + 1
End If
Next i
End Sub



Manu
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 72
16 juin 2004 à 11:49
j'ai ajouté ca dans la partie declaration:

Private Const LB_FINDSTRINGEXACT = &H1A2
Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long


a toi de choisir entre ces deux fonctions :

Public Function AddDistinct(List As ListBox, ByVal Item As String) As Boolean
    If SendMessage(List.hWnd, LB_FINDSTRINGEXACT, 0, ByVal Item) = -1 Then
        List.AddItem Item
        AddDistinct = True
    End If
End Function


qui te permet de ne pas ajouter de doublons.
et

Public Sub RemoveDuplicates(List As ListBox)
    Dim i As Integer
    Dim k As Integer
    Do
        DoEvents
        For i = 0 To List1.ListCount - 1
            k = SendMessage(List.hWnd, LB_FINDSTRINGEXACT, i, ByVal List.List(i))
            If k > i Then
                List.RemoveItem k
                Exit For
            End If
        Next i
        If k = 0 Then Exit Do
    Loop While True
End Sub


qui te permet de les enlever
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 72
16 juin 2004 à 11:56
econs, a quoi sert le i = i +1 ?

Private Sub Command1_Click()
Dim i As Long
For i = List1.ListCount To 1 Step -1
If List1.List(i - 1) = List1.List(i) Then
List1.RemoveItem (i - 1)
i = i + 1
End If
Next i
End Sub
0