monter et descendre une liste

C

Chezmoi

Guest
Bonjour forum,

J'ai une liste box, avec des elements dedans. J'ai créer des boutons pour monter ou descendre l'éléement selectionné dans la liste. Mais problème je sais pas comment faire vous pouvez maider ?

merci davance
 
R

Rai

Guest
Bonjour,

Si on considère ton UserForm avec 3 objets :
- une liste nommée Liste
- un bouton nommé btn_descendre
- un bouton nommé btn-remonter

Tu peux gèrer le déplacement au sein de ta liste avec les évennements CLIC de chacun des 2 boutons. Ca donnera en code :

Private Sub btn_descendre_Click()
Pos = Liste.ListIndex
If Pos = Liste.ListCount - 1 Then Pos = Liste.ListCount - 2
Liste.ListIndex = Pos + 1
End Sub

Private Sub btn_remonter_Click()
Pos = Liste.ListIndex
If Pos < 1 Then Pos = 1
Liste.ListIndex = Pos - 1
End Sub

Et en plus ça s'adapte automatiquement à ta liste et son nombre d'éléments ... Sympa, non ? ;o))


Bonne continuation

Rai
 
H

Hervé

Guest
Bonjour Chezmoi et rai
Salut au forum

Si j'ai bien compris, une proposition en pièce jointe (un peu barbatruc).

Salut
Hervé
 

Pièces jointes

  • Classeur4.zip
    11.4 KB · Affichages: 91
  • Classeur4.zip
    11.4 KB · Affichages: 92
  • Classeur4.zip
    11.4 KB · Affichages: 89
L

Lord Nelson

Guest
Bonsoir tout le monde,

Voici une une autre proposition (sans USF) :

Private Sub CommandButton1_Click()
'Reculer un élément de la liste
Dim Temp As String
If ListBox1.ListCount = 0 Then Exit Sub
Temp = ListBox1.Text
If ListBox1.ListIndex <= 0 Then
ListBox1.List(0) = ListBox1.List(ListBox1.ListCount - 1)
ListBox1.List(ListBox1.ListCount - 1) = Temp
ListBox1.ListIndex = ListBox1.ListCount - 1
Else
ListBox1.List(ListBox1.ListIndex) = ListBox1.List(ListBox1.ListIndex - 1)
ListBox1.List(ListBox1.ListIndex - 1) = Temp
ListBox1.ListIndex = ListBox1.ListIndex - 1
End If
End Sub

Private Sub CommandButton2_Click()
'Avancer un élément de la liste
Dim Temp As String
If ListBox1.ListCount = 0 Then Exit Sub
Temp = ListBox1.Text
If ListBox1.ListIndex = ListBox1.ListCount - 1 Then
ListBox1.List(ListBox1.ListIndex) = ListBox1.List(0)
ListBox1.List(0) = Temp
ListBox1.ListIndex = 0
Else
ListBox1.List(ListBox1.ListIndex) = ListBox1.List(ListBox1.ListIndex + 1)
ListBox1.List(ListBox1.ListIndex + 1) = Temp
ListBox1.ListIndex = ListBox1.ListIndex + 1
End If
End Sub

Pour que cela marche, il ne faut pas que la zone de liste soit remplie avec sa propriété "ListFillRange", il faut utiliser la méthode "AddItem".
Par exemple comme ceci :

Private Sub Workbook_Open()
Dim I As Integer
Feuil3.ListBox1.Clear
For I = 1 To 7
Feuil3.ListBox1.AddItem Feuil3.Cells(I, 1).Text
Next
Feuil3.ListBox1.ListIndex = 0
End Sub

A+
Horatio
 

Discussions similaires

  • Question
Microsoft 365 Listbox
Réponses
3
Affichages
235
Réponses
9
Affichages
393
Réponses
43
Affichages
2 K

Statistiques des forums

Discussions
312 177
Messages
2 085 972
Membres
103 073
dernier inscrit
MSCHOE16