XL 2016 Autre curiosité ComboBox

Dudu2

XLDnaute Barbatruc
Bonjour,

J'aimerais que le ComboBox.DropDown s'exécute automatiquement à chaque fois qu'on entre dans une ComboBox.
J'ai donc codé:
VB:
Private Sub UserForm_Initialize()
    Me.ComboBox1.List = [A1:A3].Value
    Me.ComboBox2.List = [B1:B3].Value
    Me.ComboBox3.List = [C1:C3].Value
    Me.ComboBox1.SetFocus
End Sub

Private Sub ComboBox1_Enter()
    [I2].Value = "ComboBox1_Enter"
    Me.ComboBox1.DropDown
End Sub

Private Sub ComboBox2_Enter()
    [I2].Value = "ComboBox2_Enter"
    Me.ComboBox2.DropDown
End Sub

Private Sub ComboBox3_Enter()
    [I2].Value = "ComboBox3_Enter"
    Me.ComboBox3.DropDown
End Sub

Hélas ! Quand on utilise Tabulation pour passer à la ComboBox suivante, le ComboBox.DropDown ne s'exécute qu'une fois sur 2. Et pourquoi donc ?
 

Pièces jointes

  • Classeur5.xlsm
    20.1 KB · Affichages: 25
Solution
purée dudu2 si tu continu a filer mauvais cotton tu va m'entendre chanter toi !!!!:p
VB:
Private Sub UserForm_Initialize()
   Me.ComboBox1.List = [A1:A3].Value
   Me.ComboBox2.List = [B1:B3].Value
   Me.ComboBox3.List = [C1:C3].Value
End Sub

Private Sub ComboBox1_Enter()
    CreateObject("wscript.shell").SendKeys "{F4}" 'DropDown
End Sub

Private Sub ComboBox2_Enter()
 CreateObject("wscript.shell").SendKeys "{F4}" 'DropDown
End Sub

Private Sub ComboBox3_Enter()
 CreateObject("wscript.shell").SendKeys "{F4}" 'DropDown
End Sub

laurent950

XLDnaute Accro
Bonjour @Dudu2 @patricktoulon @mapomme

Pour désactivé l'évènement et le réactivé :
il faut stocké une variable dans une classe (j'ai créer un module de classe : stopEvent)
J'ai pas trouvé d'autre solution
Impossible de créer une variable Public dans des modules Privés (d'où se module de classe)
et cela fonctionne très bien mais c'est copieux (juste pour stocké une variable Boolean)
VB:
Private ClsFlag As Boolean
Property Let GestEvent(ByVal Flag As Boolean)
'Désactivation de l'événement = True
    ClsFlag = Flag
End Property
Property Get GestEvent() As Boolean
' Premiére Opération
'Désactivation de l'événement = True
    GestEvent = ClsFlag
' Réactive l'évenemnt pour la suite
    If ClsFlag = True Then ClsFlag = False
End Property
et le code dans le module Userform
VB:
Option Explicit
Public ActDesaEven As New stopEvent
Private Sub UserForm_Initialize()
'Désactivation de l'événement = True
ActDesaEven.GestEvent = True
Me.ComboBox1.List = [A1:A3].Value
Me.ComboBox2.List = [B1:B3].Value
Me.ComboBox3.List = [C1:C3].Value
'Me.ComboBox1.SetFocus
End Sub
'
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'Si l'événement est déjà en cours : exit !
If ActDesaEven.GestEvent = True Then Exit Sub
[I2].Value = "ComboBox1_Enter"
If KeyCode = vbKeyTab Then Me.ComboBox1.DropDown
End Sub
'
Private Sub ComboBox2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'Si l'événement est déjà en cours : exit !
If ActDesaEven.GestEvent = True Then Exit Sub
[I2].Value = "ComboBox2_Enter"
If KeyCode = vbKeyTab Then Me.ComboBox2.DropDown
End Sub
'
Private Sub ComboBox3_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'Si l'événement est déjà en cours : exit !
If ActDesaEven.GestEvent = True Then Exit Sub
[I2].Value = "ComboBox3_Enter"
If KeyCode = vbKeyTab Then Me.ComboBox3.DropDown
End Sub

cdt
 
Dernière édition:

patricktoulon

XLDnaute Barbatruc
re
a oui y a de le recherche Laurent
mais ça fait exactement le même effet que Dudu2 a savoir
tab sur la 1 active la 2 sans dropdown
tab sur la 2 active la 3 et dropdown
tab sur la 3 active la 1 sans dropdown
et enfin a nouveau tab sur la 1 active la 2 avec dropdown
 

patricktoulon

XLDnaute Barbatruc
re
VB:
Option Explicit

Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    With ComboBox1
        If Val(.Tag) = 0 Then .Tag = 1
        If .Tag = 1 Then .Tag = 0: .DropDown
    End With
End Sub


Private Sub ComboBox2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    With ComboBox2
        If Val(.Tag) = 0 Then .Tag = 1
        If .Tag = 1 Then .Tag = 0: .DropDown
      End With
End Sub

Private Sub ComboBox3_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    With ComboBox3
        If Val(.Tag) = 0 Then .Tag = 1
        If .Tag = 1 Then .Tag = 0: .DropDown
    End With
End Sub



Private Sub UserForm_Initialize()
    Columns("i:i").Clear
    Me.ComboBox1.List = [A1:A3].Value
    Me.ComboBox2.List = [B1:B3].Value
    Me.ComboBox3.List = [C1:C3].Value
    TextBox1.SetFocus
End Sub
ce qui prouve bien que c'est bien depuis le debut une question d'events

même si les events ne sont pas utilisés dans le code l'instance de classe interne de l'object est bien instanciée et donc tout ses events dispo
 

Dudu2

XLDnaute Barbatruc
Vous êtes très fort !
1607792058718.gif
 
Dernière édition:

Dudu2

XLDnaute Barbatruc
Bon je me suis un peu embrouillé dans ma première interprétation 🤢

J'ai utilisé la version de @mapomme (en limitant à 1 ou 2 KeyCodes).

Mais y a un truc que je ne comprends pas...
Au lancement du UserFom1, l'évènement ComboBox1_Enter est déclenché et pourtant le ComboBox1.ScrollDown ne se fait pas. Alors que j'ai un autre classeur dans lequel ça marche parfaitement.
Il a fallu que je bidouille un flag pour laisser le ComboBox1_KeyUp (qui reçoit KeyCode = 255 au lancement) faire le boulot.
 

Pièces jointes

  • ComboBox DropDown.xlsm
    20.2 KB · Affichages: 6
Dernière édition:

Dudu2

XLDnaute Barbatruc
@jcf6464, oui cela je l'ai essayé et ça marche bien. Mais cela a quelques inconvénients et c'est pour ça que je suis passé sur l'évènement Enter().
Par exemple si un ComboBox1.DropDown est déclenché par le MouseMove(), on ne peut pas en sortir en passant la souris sur une autre ComboBox. Ou alors en sortir avec un clic ou Tab.
Donc on doit mixer clavier et souris pour passer de l'un à l'autre.
De plus on peut passer la souris involontairement sur la ComboBox, cela se déclenche et il faut en sortir.
 

Pièces jointes

  • ComboBox DropDown MouseMove.xlsm
    20.1 KB · Affichages: 10

Dudu2

XLDnaute Barbatruc
Sinon il y a une solution un peu basique mais qui fonctionne: envoyer F4 comme documenté ici.
Et là c'est trivial.
VB:
Private Sub UserForm_Initialize()
   Me.ComboBox1.List = [A1:A3].Value
   Me.ComboBox2.List = [B1:B3].Value
   Me.ComboBox3.List = [C1:C3].Value
End Sub

Private Sub ComboBox1_Enter()
    SendKeys "{F4}" 'DropDown
End Sub

Private Sub ComboBox2_Enter()
    SendKeys "{F4}" 'DropDown
End Sub

Private Sub ComboBox3_Enter()
    SendKeys "{F4}" 'DropDown
End Sub
 

Pièces jointes

  • ComboBox DropDown.xlsm
    25.8 KB · Affichages: 5

patricktoulon

XLDnaute Barbatruc
purée dudu2 si tu continu a filer mauvais cotton tu va m'entendre chanter toi !!!!:p
VB:
Private Sub UserForm_Initialize()
   Me.ComboBox1.List = [A1:A3].Value
   Me.ComboBox2.List = [B1:B3].Value
   Me.ComboBox3.List = [C1:C3].Value
End Sub

Private Sub ComboBox1_Enter()
    CreateObject("wscript.shell").SendKeys "{F4}" 'DropDown
End Sub

Private Sub ComboBox2_Enter()
 CreateObject("wscript.shell").SendKeys "{F4}" 'DropDown
End Sub

Private Sub ComboBox3_Enter()
 CreateObject("wscript.shell").SendKeys "{F4}" 'DropDown
End Sub
 

Discussions similaires

Réponses
17
Affichages
314
Réponses
6
Affichages
229

Statistiques des forums

Discussions
312 108
Messages
2 085 372
Membres
102 876
dernier inscrit
BouteilleMan