XL 2010 [RESOLU] Eviter affichage Msgbox à 2 reprises

cathodique

XLDnaute Barbatruc
Bonjour,

Je viens vers vous pour un petit problème. J'avoue que je suis un peu perdu.
Je voudrai vider une textbox si aucuns boutons coché avec un message avertissant l'utilisateur.
J'arrive à le faire mais le message s'affiche à 2 reprises.
J'ai pensé à une variable booléenne mais j'avoue que je n'ai pas trouvé l'astuce pour l'utiliser.
VB:
Option Explicit
Private Sub TextBox1_Change()
If Opt_Ajouter.Value = False And Opt_Corriger.Value = False And Opt_Supprimer.Value = False Then
TextBox1 = "": TextBox1.SetFocus
MsgBox "Choisir une Option"
End If
End Sub
En vous remerciant.
 

Pièces jointes

  • Option_Textbox.xlsm
    16.9 KB · Affichages: 13
Solution
Bonjour cathodique, le forum,

Oui il faut une variable booléenne Static :
VB:
Private Sub TextBox1_Change()
Static flag As Boolean
If flag Then Exit Sub
If Not (Opt_Ajouter Or Opt_Corriger Or Opt_Supprimer) Then
flag = True: TextBox1 = "": flag = False: TextBox1.SetFocus
MsgBox "Choisir une Option"
End If
End Sub
A+

job75

XLDnaute Barbatruc
Bonjour cathodique, le forum,

Oui il faut une variable booléenne Static :
VB:
Private Sub TextBox1_Change()
Static flag As Boolean
If flag Then Exit Sub
If Not (Opt_Ajouter Or Opt_Corriger Or Opt_Supprimer) Then
flag = True: TextBox1 = "": flag = False: TextBox1.SetFocus
MsgBox "Choisir une Option"
End If
End Sub
A+
 

Pièces jointes

  • Option_Textbox(1).xlsm
    21.1 KB · Affichages: 3
Dernière édition:

youky(BJ)

XLDnaute Barbatruc
Bonjour tous,
Une autre solution en utilisant le KeyUp et non le Change
Bruno

VB:
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Opt_Ajouter.Value = False And Opt_Corriger.Value = False And Opt_Supprimer.Value = False Then
TextBox1 = "": TextBox1.SetFocus
MsgBox "Choisir une Option"
End If
End Sub
 

cathodique

XLDnaute Barbatruc
Bonjour cathodique, le forum,

Oui il faut une variable booléenne Static :
VB:
Private Sub TextBox1_Change()
Static flag As Boolean
If flag Then Exit Sub
If Not (Opt_Ajouter Or Opt_Corriger Or Opt_Supprimer) Then
flag = True: TextBox1 = "": flag = False: TextBox1.SetFocus
MsgBox "Choisir une Option"
End If
End Sub
A+
Bonjour Job75;),

C'est parfait. Merci beaucoup, tu me permets d'avancer.
Stp, une dernière question: Static signifie que la variable garde toujours le même état (vrai/faux)?
Encore merci.
 

cathodique

XLDnaute Barbatruc
Bonjour tous,
Une autre solution en utilisant le KeyUp et non le Change
Bruno

VB:
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Opt_Ajouter.Value = False And Opt_Corriger.Value = False And Opt_Supprimer.Value = False Then
TextBox1 = "": TextBox1.SetFocus
MsgBox "Choisir une Option"
End If
End Sub
Bonjour Youky(BJ);),

C'est aussi parfait pour moi. Merci beaucoup.
Pour être honnête, tu viens de me faire découvrir l'utilisation du KeyUp.
Encore merci.
 

Discussions similaires

Statistiques des forums

Discussions
312 223
Messages
2 086 407
Membres
103 201
dernier inscrit
centrale vet