Meme code, mais listbox différentes

Résolu
cs_Fabian123 Messages postés 180 Date d'inscription vendredi 18 janvier 2008 Statut Membre Dernière intervention 22 novembre 2013 - 25 mars 2008 à 14:34
cs_Fabian123 Messages postés 180 Date d'inscription vendredi 18 janvier 2008 Statut Membre Dernière intervention 22 novembre 2013 - 25 mars 2008 à 15:32
bonjour!

voilà, j'ai deux evenements click identiques, mais liés à deux listbox, est-il possible d'encoder l'evenement dans une sub et de l'appeler dans les evenement de ma listbox?

voilà mes codes, j'ai essayé de ne faire qu'une seule sub et de l'appeler dans mes listbox, mais ça plante...

Private Sub ListeStock_Click()
Dim i As Integer
i = ListeStock.ListIndex
    If Dir$("d:\profiles\beufab\Bureau\Images" & .List(.ListIndex, 0) & ".jpg") = "" Then
         Set ImageArticle.Picture = LoadPicture("d:\Images\default.jpg")
    Else
        Set ImageArticle.Picture = LoadPicture("d:\Images" & .List(.ListIndex, 0) & ".jpg")
    End If
    InfoArticle.Caption = ListeStock.List(i, 0)
    InfoPrix.Caption = ListeStock.List(i, 1)
End Sub

Private Sub ListeVente_Click()
Dim i As Integer
 i = ListeVente.ListIndex
   With ListeVente
    If Dir$("d:\\Images" & .List(.ListIndex, 0) & ".jpg") = "" Then
         Set ImageArticle.Picture = LoadPicture("d:\Images\default.jpg")
    Else
        Set ImageArticle.Picture = LoadPicture("d:\Images" & .List(.ListIndex, 0) & ".jpg")
    End If
    InfoArticle.Caption = ListeVente.List(i, 0)
    InfoPrix.Caption = ListeVente.List(i, 1)
    End With
End Sub

j'ai essayé ceci, mais ça plante au niveau de la ligne .list(.listindex)...



Private Sub AfficherImage()
Dim i As Integer
   
    If Dir$("d:\profiles\beufab\Bureau\Images" & .List(. ListIndex , 0) & ".jpg") = "" Then
         Set ImageArticle.Picture = LoadPicture("d:\Images\default.jpg")
    Else
        Set ImageArticle.Picture = LoadPicture("d:\Images" & .List(.ListIndex, 0) & ".jpg")
    End If
    InfoArticle.Caption = ListeStock.List(i, 0)
    InfoPrix.Caption = ListeStock.List(i, 1)
   
End Sub

Private Sub ListeStock_Click()
 i = ListeStock.ListIndex
    With liststock
        Call AfficherImage
    End With
End Sub

Private Sub ListeVente_Click()
 i = ListeVente.ListIndex
    With liststock
        Call AfficherImage
    End With
End Sub

6 réponses

Le grand Zorro Messages postés 50 Date d'inscription mercredi 4 août 2004 Statut Membre Dernière intervention 12 mai 2008
25 mars 2008 à 14:39
Bonjour,
Pourquoi ne pas créer une procédure que tu appelle dans chacun des événement click et passer en paramètre la référence de la listebox et celle de l'image.
genre:
sub AfficherImageDansListBox(by ref LeControl as ListBox, byval LImage as string) Ou quelque chose du genre..
3
jrivet Messages postés 7392 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 6 avril 2012 60
25 mars 2008 à 14:40
Salut,
Tu dois passer ne parametre à afficherImage le list box.

Regarde un exemple

Private Sub ListeStock_Click()
Call ProcessListClick(ListeStock)
End Sub<hr />
Private Sub ListeVente_Click()
   Call ProcessListClick(ListeVente)
End Sub<hr />

Private Sub ProcessListClick(L As ListBox)
Dim i As Integer
   i = L.ListIndex
   With L
       If Dir$("d:\\Images\" & .List(i, 0) & ".jpg") = vbNullString Then
           Set ImageArticle.Picture = LoadPicture("d:\Images\default.jpg")
       Else
           Set ImageArticle.Picture = LoadPicture("d:\Images\" & .List(i, 0) & ".jpg")
       End If
       InfoArticle.Caption = .List(i, 0)
       InfoPrix.Caption = .List(i, 1)
   End With
End Sub<hr />
, ----
[code.aspx?ID=41455 By Renfield]

@+: Ju£i?n
Pensez: Réponse acceptée
3
jrivet Messages postés 7392 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 6 avril 2012 60
25 mars 2008 à 14:41
Re,
Voila...le temps que je prépare... et [auteur/LEGRANDZORRO/318197.aspx Le grand Zorro] t'as soufflé la même chose.

NOTE:

C'est mieux

Private Sub ProcessListClick(L As ListBox)
Dim i As Integer
  
   With L
       i = .ListIndex
       If Dir$( "d:\\Images" & .List(i, 0) & ".jpg") = vbNullString Then
           Set ImageArticle.Picture = LoadPicture( "d:\Images\default.jpg" )
       Else
           Set ImageArticle.Picture = LoadPicture("d:\Images" & .List(i, 0) & ".jpg")
       End If
       InfoArticle.Caption = .List(i, 0 )
       InfoPrix.Caption = .List(i, 1)
   End With
End Sub

@+: Ju£i?n
Pensez: Réponse acceptée
3
jrivet Messages postés 7392 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 6 avril 2012 60
25 mars 2008 à 15:24
Re,
J'avais pas vu la catégorie.

Essaie peu être

Private Sub ProcessListClick(L As MSForms.ListBox)
Dim i As Integer
 
  With L
      i = .ListIndex
      If Dir$("d:\\Images\" & .List(i, 0) & ".jpg") = vbNullString Then
          Set ImageArticle.Picture = LoadPicture("d:\Images\default.jpg")
      Else
          Set ImageArticle.Picture = LoadPicture("d:\Images\" & .List(i, 0) & ".jpg")
      End If
      InfoArticle.Caption = .List(i, 0)
      InfoPrix.Caption = .List(i, 1)
  End With
End Sub<hr />
, ----
[code.aspx?ID=41455 By Renfield]
Si cela ne fonctionne toujours pas, il va falloir que tu nous rappelle où sont placées tes ListBox

@+: Ju£i?n
Pensez: Réponse acceptée
3

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_Fabian123 Messages postés 180 Date d'inscription vendredi 18 janvier 2008 Statut Membre Dernière intervention 22 novembre 2013
25 mars 2008 à 15:19
salut!

merci pour cette réponse rapide!

j'ai essayé, ce que tu m'as proposé, julien, mais il plante avec le message d'erreur suivant:

'erreur d'exécution '13' Incompatibilité de type'
et il surligne ceci: Call ProcessListClick(ListeStock)
0
cs_Fabian123 Messages postés 180 Date d'inscription vendredi 18 janvier 2008 Statut Membre Dernière intervention 22 novembre 2013
25 mars 2008 à 15:32
ca fonctionne! Super!
merci pour ton aide!
0
Rejoignez-nous