rolodo
Messages postés44Date d'inscriptionlundi 26 novembre 2007StatutMembreDernière intervention18 avril 2012
-
24 avril 2009 à 03:25
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 2021
-
30 avril 2009 à 06:28
je m'explique j'ai créé une calculatrice avec VB6 et pour appuyer sur mes bouton avec le paver numérique j'utilise keydown mais lorsque japuis sur le paver numeric il garde toutes les numéraux que j'ai appuyer et il les récrit.
EX: je touche sur 1 ensuite 2 et ensuite 3 il écrit 112123
pour 1 12 123 aulieux de 123.
j'aimerais que au moment ou j'appuie il oublie les autres touches auxquelles j'ai touché.
Merci de me répondre !!!!!
Code (une partie du code a été pris sue vbfrance ^^) :
Private Sub Digits" numbre que j'apuie "_KeyDown(KeyCode As Integer, Shift As Integer)
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la variable
If InStr(touches, "97") Then
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits1.Caption
Equals.Enabled = True
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "98") Then
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits2.Caption
Equals.Enabled = True
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "99") Then
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits3.Caption
Equals.Enabled = True
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "100") Then
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits4.Caption
Equals.Enabled = True
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "101") Then
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits5.Caption
Equals.Enabled = True
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "102") Then
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits6.Caption
Equals.Enabled = True
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "103") Then
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits7.Caption
Equals.Enabled = True
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "104") Then
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits8.Caption
Equals.Enabled = True
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "105") Then
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits9.Caption
Equals.Enabled = True
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "96") Then
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits0.Caption
Equals.Enabled = True
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "106") Then
Operand1 = Val(Display.Caption)
Operator = "*"
Display.Caption = "0"
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "111") Then
Operand1 = Val(Display.Caption)
Operator = "/"
Display.Caption = "0"
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "109") Then
Operand1 = Val(Display.Caption)
Operator = "-"
Display.Caption = "0"
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "107") Then
Operand1 = Val(Display.Caption)
Operator = "+"
Display.Caption = "0"
End If
touches = touches & KeyCode 'Enregistrement de la touche tapée dans la varible
If InStr(touches, "112") Then
Dim result As Double
On Error GoTo ErrorHandler
Operand2 = Val(Display.Caption)
result = Operand1 + Operand2 If Operator "-" Then result Operand1 - Operand2 If Operator "*" Then result Operand1 * Operand2 If Operator "/" And Operand2 <> "0" Then result Operand1 / Operand2
Display.Caption = result
ClearDisplay = True
Equals.Enabled = True
Exit Sub
ErrorHandler:
MsgBox "Votre équation a engendrer une erreur" & vbCrLf & Err.Description
Display.Caption = "ERREUR"
ClearDisplay = True
End if
End sub
rolodo
Messages postés44Date d'inscriptionlundi 26 novembre 2007StatutMembreDernière intervention18 avril 2012 29 avril 2009 à 01:06
tout est régler jai trouver ^^
c'est ce code :
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer()
Dim CodeASCII
For CodeASCII = 96 To 105
If GetAsyncKeyState(CodeASCII) Then
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption & CodeASCII - "96"
End If
Next
End Sub
cs_Jack
Messages postés14006Date d'inscriptionsamedi 29 décembre 2001StatutModérateurDernière intervention28 août 201579 24 avril 2009 à 04:18
BONJOUR aussi
Question mal posée, code sans queue ni tête, orthographe, no comment : tout pour plaire !
KeyCode est un chiffre.
Pourquoi l'ajoutes-tu à cette variable "touches" dont on ne connait même pas la type ?
Et le Select Case, en as-tu entendu parlé ?
Que représentent Display ou Digits4 ... ?
Donner des noms de variable ressemblant à des mots clés du système est une très mlauvaise habitude, on le répète tous les jours.
Ta Sub Digits" numbre que j'apuie "_KeyDown : Tu as copié autant de fois ce code horrible qu'il y a de boutons sur ta forme ? Intéresse-toi à la propriété Index d'un CommandButton
Comment sont dimensionnées tes variables Operand1 et 2 ?
Vala
Jack, MVP VB
NB : Je ne répondrai pas aux messages privés
<hr />Le savoir est la seule matière qui s'accroit quand on la partage (Socrate)
rolodo
Messages postés44Date d'inscriptionlundi 26 novembre 2007StatutMembreDernière intervention18 avril 2012 24 avril 2009 à 23:40
je vais inscrire le reste des mes code ici
bouton + = Plus
bouton - = Minus
bouton / = Div
bouton * = Times
bouton 1/X = Over
bouton -/+ = PlusMinus
bouton "=" = Equals
bouton 0 à 9 = Digits" numero "
bouton "." = DotBttn
bouton CE = ClearBttn
'et pour le code je ne les pas construit je lai prit sur vbfrance je crois alors ne le critiquer pas !!!
'moi je veux seulement le convertir a l'appui de touche. Et je débute.
Merci !!
Code:
Option Explicit
Dim touches As String
Dim Operand1 As Double, Operand2 As Double
Dim Operator As String
Dim ClearDisplay As Boolean
Private Sub Addition_Click()
Operand1 = Val(Display.Caption)
Operator = "+"
Display.Caption = "0"
End Sub
Private Sub ClearBttn_Click()
Display.Caption = "0"
Operand1 = "0"
Operand2 = "0"
Equals.Enabled = True
End Sub
Private Sub Digits_Click(Index As Integer)
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits(Index).Caption
End Sub
Private Sub Coller_Click()
Text1.Text = Clipboard.GetText
End Sub
Private Sub Copier_Click()
Clipboard.Clear
Clipboard.SetText Display.Caption
End Sub
Private Sub Copyright_Click()
Form2.Show
End Sub
Private Sub Digits0_Click()
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits0.Caption
Equals.Enabled = True
End Sub
Private Sub Digits1_Click()
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits1.Caption
Equals.Enabled = True
End Sub
Private Sub Digits2_Click()
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits2.Caption
Equals.Enabled = True
End Sub
Private Sub Digits3_Click()
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits3.Caption
Equals.Enabled = True
End Sub
Private Sub Digits4_Click()
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits4.Caption
Equals.Enabled = True
End Sub
Private Sub Digits5_Click()
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits5.Caption
Equals.Enabled = True
End Sub
Private Sub Digits6_Click()
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits6.Caption
Equals.Enabled = True
End Sub
Private Sub Digits7_Click()
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits7.Caption
Equals.Enabled = True
End Sub
Private Sub Digits8_Click()
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits8.Caption
Equals.Enabled = True
End Sub
Private Sub Digits9_Click()
If Display.Caption = "0" Then
Display.Caption = ""
End If
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption + Digits9.Caption
Equals.Enabled = True
End Sub
Private Sub Div_Click()
Operand1 = Val(Display.Caption)
Operator = "/"
Display.Caption = "0"
End Sub
Private Sub Division_Click()
Operand1 = Val(Display.Caption)
Operator = "/"
Display.Caption = "0"
End Sub
Private Sub DotBttn_Click()
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
If InStr(Display.Caption, ".") Then
Exit Sub
Else
Display.Caption = Display.Caption & "."
Equals.Enabled = True
End If
End Sub
Private Sub Egale_Click()
Dim result As Double
On Error GoTo ErrorHandler
Operand2 = Val(Display.Caption)
result = Operand1 + Operand2 If Operator "-" Then result Operand1 - Operand2 If Operator "*" Then result Operand1 * Operand2 If Operator "/" And Operand2 <> "0" Then result Operand1 / Operand2
Display.Caption = result
ClearDisplay = True
Equals.Enabled = True
Exit Sub
ErrorHandler:
MsgBox "Votre équation a engendrer une erreur" & vbCrLf & Err.Description
Display.Caption = "ERREUR"
ClearDisplay = True
End Sub
Private Sub Equals_Click()
Dim result As Double
On Error GoTo ErrorHandler
Operand2 = Val(Display.Caption)
result = Operand1 + Operand2 If Operator "-" Then result Operand1 - Operand2 If Operator "*" Then result Operand1 * Operand2 If Operator "/" And Operand2 <> "0" Then result Operand1 / Operand2
Display.Caption = result
ClearDisplay = True
Equals.Enabled = True
Exit Sub
ErrorHandler:
MsgBox "Votre équation a engendrer une erreur" & vbCrLf & Err.Description
Display.Caption = "ERREUR"
ClearDisplay = True
End Sub
Private Sub Minus_Click()
Operand1 = Val(Display.Caption)
Operator = "-"
Display.Caption = "0"
End Sub
Private Sub Multiplication_Click()
Operand1 = Val(Display.Caption)
Operator = "*"
Display.Caption = "0"
End Sub
Private Sub Over_Click()
If Val(Display.Caption) <> 0 Then Display.Caption = 1 / Val(Display.Caption)
End Sub
Private Sub Plus_Click()
Operand1 = Val(Display.Caption)
Operator = "+"
Display.Caption = "0"
End Sub
Private Sub PlusMinus_Click()
Display.Caption = -Val(Display.Caption)
End Sub
Private Sub Quiter_Click()
End
End Sub
Private Sub Soustraction_Click()
Operand1 = Val(Display.Caption)
Operator = "-"
Display.Caption = "0"
End Sub
Private Sub Times_Click()
Operand1 = Val(Display.Caption)
Operator = "*"
Display.Caption = "0"
End Sub
cs_Jack
Messages postés14006Date d'inscriptionsamedi 29 décembre 2001StatutModérateurDernière intervention28 août 201579 25 avril 2009 à 04:31
Re
Parmi les codes du site, il y en a des moins bons que d'autres.
Oui, ton code fonctionnera surement un jour, mais ce qu'on veut te dire, c'est que ce n'est pas la meilleure solution que tu as utilisée : Si un groupe de boutons fait pratiquement la même chose et qu'il est facile de les identifier, il faut utiliser des objets indexés.
En informatique, il est absolument anormal d'avoir à retaper 50 fois les mêmes séquences : c'est une perte de temps et de la perte de lisibilité + risque d'erreur/bug.
Dans ton cas, on ne va pas lire ton code car il n'est pas optimisé, un minimum.
Fais des essais :
- Crée un nouveau projet et place un bouton sur une forme.
- Donne le chiffre 0 à la propriété Index de ce bouton, puis copie/colle-le 9 autres fois
- Ensuite, double clique sur n'importe lequel de ces boutons : tu arriveras dans la procédure par défaut _Click et tu observeras que parmi les paramètres renvoyés par cet évènement, il y a un nouveau paramètre "Index" qui te dira sur quel bouton tu as cliqué.
La procédure est unique pour tous ces 10 boutons, seul l'Index évoluera de 0 à 9
Choisis donc bien l'Index en fonction de la fonction du bouton et tu auras dans la variable Index, le numéro correspond au bouton.
A partir de là, reprend ton code en partie et aménage-le pour utiliser Index au lieu de personnaliser chaque code.
Apprends en faisant des essais et comprendre ce que représente un code ASCII.
Calcule le code ASCII correspondant au chiffre désiré en utilisant Index : facile une fois qu'on sait que le code ASCII du chiffre 0 est 48 et celui du chiffre 9 est 57
Une fois que tu auras associé l'index 0 à la touche "0" et l'index 1 à la touche "1" (...), le code ASCII correspondant sera donc 48 + Index
Revois aussi la concaténation de chaine : Utilise & et pas +
Utilise le mode debugage pour suivre pas à pas ton programme : Touches F9, F8 et F5 : Une fois le programme arrêté dans ton code, un survol de la souris te donnera le contenu de tes variables : à toi de voir le programme fait bien ce que tu veux.
Derniers conseils :
# Dans les options de VB6, coche la case "Déclaration obligatoire des variables" : Cela peut paraitre chiant de devoir dimensionner toutes tes variables, mais tu apprendras comme cela à comprendre leur étendue, et à les dimensionner dans le bon type, c'est important pour éviter les conflits entre variables.
# Lance ton programme avec Ctrl-F5 au lieu de F5 : Le code sera ainsi vérifié avant lancement et tu gagneras du temps pour le débugage.
# Ne jamais nommer des variables ou des objets comme des mots clés du langage. Voir internet et la notation "à la hongroise"
Bien sûr, l'aide de VB6 est ton premier alié !
Vala
Jack, MVP VB
NB : Je ne répondrai pas aux messages privés
<hr />Le savoir est la seule matière qui s'accroit quand on la partage (Socrate)
rolodo
Messages postés44Date d'inscriptionlundi 26 novembre 2007StatutMembreDernière intervention18 avril 2012 28 avril 2009 à 04:25
voilà j'ai modifier mon code si vous voyer des erreurs dites les moi ^^
bon maintenant je veux modifier mon code afin que lorsque j'appuie sur ex la touche 1 et bien que cela face comme pour mon code.
Mais (précision en appuient sur mon clavier.)
Option Explicit
Dim touches As String
Dim Operand1 As Double, Operand2 As Double
Dim Operator As String
Dim ClearDisplay As Boolean
Private Sub Addition_Click()
Operand1 = Val(Display.Caption)
Operator = "+"
Display.Caption = "0"
End Sub
Private Sub ClearBttn_Click()
Display.Caption = "0"
Operand1 = "0"
Operand2 = "0"
Equals.Enabled = True
End Sub
Private Sub Digits_Click(Index As Integer)
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption & Digits(Index).Caption
End Sub
Private Sub Coller_Click()
Text1.Text = Clipboard.GetText
End Sub
Private Sub Copier_Click()
Clipboard.Clear
Clipboard.SetText Display.Caption
End Sub
Private Sub Copyright_Click()
Form2.Show
End Sub
Private Sub Div_Click()
Operand1 = Val(Display.Caption)
Operator = "/"
Display.Caption = "0"
End Sub
Private Sub Division_Click()
Operand1 = Val(Display.Caption)
Operator = "/"
Display.Caption = "0"
End Sub
Private Sub DotBttn_Click()
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
If InStr(Display.Caption, ".") Then
Exit Sub
Else
Display.Caption = Display.Caption & "."
Equals.Enabled = True
End If
End Sub
Private Sub Equals_Click()
Dim result As Double
On Error GoTo ErrorHandler
Operand2 = Val(Display.Caption)
result = Operand1 + Operand2 If Operator "-" Then result Operand1 - Operand2 If Operator "*" Then result Operand1 * Operand2 If Operator "/" And Operand2 <> "0" Then result Operand1 / Operand2
Display.Caption = result
ClearDisplay = True
Equals.Enabled = True
Exit Sub
ErrorHandler:
MsgBox "Votre équation a engendrer une erreur" & vbCrLf & Err.Description
Display.Caption = "ERREUR"
ClearDisplay = True
End Sub
Private Sub Minus_Click()
Operand1 = Val(Display.Caption)
Operator = "-"
Display.Caption = "0"
End Sub
Private Sub Multiplication_Click()
Operand1 = Val(Display.Caption)
Operator = "*"
Display.Caption = "0"
End Sub
Private Sub Over_Click()
If Val(Display.Caption) <> 0 Then Display.Caption = 1 / Val(Display.Caption)
End Sub
Private Sub Plus_Click()
Operand1 = Val(Display.Caption)
Operator = "+"
Display.Caption = "0"
End Sub
Private Sub PlusMinus_Click()
Display.Caption = -Val(Display.Caption)
End Sub
Private Sub Quiter_Click()
End
End Sub
Private Sub Soustraction_Click()
Operand1 = Val(Display.Caption)
Operator = "-"
Display.Caption = "0"
End Sub
Private Sub Times_Click()
Operand1 = Val(Display.Caption)
Operator = "*"
Display.Caption = "0"
End Sub
rolodo
Messages postés44Date d'inscriptionlundi 26 novembre 2007StatutMembreDernière intervention18 avril 2012 28 avril 2009 à 04:35
encore plus réduit
Option Explicit
Dim touches As String
Dim Operand1 As Double, Operand2 As Double
Dim Operator As String
Dim ClearDisplay As Boolean
Private Sub ClearBttn_Click()
Display.Caption = "0"
Operand1 = "0"
Operand2 = "0"
Equals.Enabled = True
End Sub
Private Sub Digits_Click(Index As Integer)
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption & Digits(Index).Caption
End Sub
Private Sub Coller_Click()
Text1.Text = Clipboard.GetText
End Sub
Private Sub Copier_Click()
Clipboard.Clear
Clipboard.SetText Display.Caption
End Sub
Private Sub Copyright_Click()
Form2.Show
End Sub
Private Sub DotBttn_Click()
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
If InStr(Display.Caption, ".") Then
Exit Sub
Else
Display.Caption = Display.Caption & "."
Equals.Enabled = True
End If
End Sub
Private Sub Equals_Click()
Dim result As Double
On Error GoTo ErrorHandler
Operand2 = Val(Display.Caption)
result = Operand1 + Operand2 If Operator "-" Then result Operand1 - Operand2 If Operator "*" Then result Operand1 * Operand2 If Operator "/" And Operand2 <> "0" Then result Operand1 / Operand2
Display.Caption = result
ClearDisplay = True
Equals.Enabled = True
Exit Sub
ErrorHandler:
MsgBox "Votre équation a engendrer une erreur" & vbCrLf & Err.Description
Display.Caption = "ERREUR"
ClearDisplay = True
End Sub
Private Sub Operateur_Click(Index As Integer)
Operand1 = Val(Display.Caption)
Operator = Operateur(Index).Caption
Display.Caption = "0"
End Sub
Private Sub Over_Click()
If Val(Display.Caption) <> 0 Then Display.Caption = 1 / Val(Display.Caption)
End Sub
Private Sub PlusMinus_Click()
Display.Caption = -Val(Display.Caption)
End Sub
rolodo
Messages postés44Date d'inscriptionlundi 26 novembre 2007StatutMembreDernière intervention18 avril 2012 29 avril 2009 à 00:38
je veux modifier le code si dessous pour faire les consigne avec le clavier ^^, mais je ne sais pas comment. Merci de m'aider
mon code le plus réduit que je suis capable de faire :
Option Explicit
Dim touches As String
Dim Operand1 As Double, Operand2 As Double
Dim Operator As String
Dim ClearDisplay As Boolean
Private Sub ClearBttn_Click()
Display.Caption = "0"
Operand1 = "0"
Operand2 = "0"
Equals.Enabled = True
End Sub
Private Sub Digits_Click(Index As Integer)
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
Display.Caption = Display.Caption & Digits(Index).Caption
End Sub
Private Sub Coller_Click()
Text1.Text = Clipboard.GetText
End Sub
Private Sub Copier_Click()
Clipboard.Clear
Clipboard.SetText Display.Caption
End Sub
Private Sub Copyright_Click()
Form2.Show
End Sub
Private Sub DotBttn_Click()
If ClearDisplay Then
Display.Caption = ""
ClearDisplay = False
End If
If InStr(Display.Caption, ".") Then
Exit Sub
Else
Display.Caption = Display.Caption & "."
Equals.Enabled = True
End If
End Sub
Private Sub Equals_Click()
Dim result As Double
On Error GoTo ErrorHandler
Operand2 = Val(Display.Caption)
result = Operand1 + Operand2 If Operator "-" Then result Operand1 - Operand2 If Operator "*" Then result Operand1 * Operand2 If Operator "/" And Operand2 <> "0" Then result Operand1 / Operand2
Display.Caption = result
ClearDisplay = True
Equals.Enabled = True
Exit Sub
ErrorHandler:
MsgBox "Votre équation a engendrer une erreur" & vbCrLf & Err.Description
Display.Caption = "ERREUR"
ClearDisplay = True
End Sub
Private Sub Operateur_Click(Index As Integer)
Operand1 = Val(Display.Caption)
Operator = Operateur(Index).Caption
Display.Caption = "0"
End Sub
Private Sub OperateurMenu_Click(Index As Integer)
Operand1 = Val(Display.Caption)
Operator = OperateurMenu(Index).Caption
Display.Caption = "0"
End Sub
Private Sub OperateurMoins_Click()
Operand1 = Val(Display.Caption)
Operator = "-"
Display.Caption = "0"
End Sub
Private Sub Over_Click()
If Val(Display.Caption) <> 0 Then Display.Caption = 1 / Val(Display.Caption)
End Sub
Private Sub PlusMinus_Click()
Display.Caption = -Val(Display.Caption)
End Sub
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 202174 29 avril 2009 à 08:39
j'ai pas testé, mais en reformulant un peu ton code ...
Option Explicit
Private Operand1 As Double
Private Operator As String
Private ClearDisplay As Boolean
Private Sub ClearBtn_Click()
Display.Caption = "0"
Operand1 = 0
End Sub
Private Sub Digits_Click(Index As Integer)
If ClearDisplay Then
Display.Caption = vbNullString
ClearDisplay = False
End If
Display.Caption = Display.Caption & Index
End Sub
Private Sub DotBtn_Click()
If ClearDisplay Then
Display.Caption = "0"
ClearDisplay = False
End If
If InStr(Display.Caption, ".") = 0 Then
Display.Caption = Display.Caption & "."
Equals.Enabled = True
End If
End Sub
Private Sub Equals_Click()
On Error GoTo ErrorHandler
If ClearDisplay Then
Display.Caption = "0"
ClearDisplay = False
End If
Select Case Operator
Case "+": Display.Caption = Operand1 + Val(Display.Caption)
Case "-": Display.Caption = Operand1 - Val(Display.Caption)
Case "*": Display.Caption = Operand1 * Val(Display.Caption)
Case "/": Display.Caption = Operand1 / Val(Display.Caption)
End Select
Exit Sub
ErrorHandler:
Display.Caption = "ERREUR"
ClearDisplay = True
End Sub
Private Sub Operateur_Click(Index As Integer)
Operand1 = Val(Display.Caption)
Operator = Operateur(Index).Caption
ClearDisplay = True
End Sub
Private Sub Over_Click()
If Val(Display.Caption) Then
Display.Caption = 1 / Val(Display.Caption)
Else
Display.Caption = "ERREUR"
End If
End Sub
Private Sub PlusMinus_Click()
Display.Caption = -Val(Display.Caption)
End Sub
Private Sub Quitter_Click()
Unload Me
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii >= 48 And KeyAscii <= 57 Then
Digits_Click (KeyAscii - 48)
End If
End Sub
Private Sub Form_Load()
KeyPreview = True
End Sub
et là tu as la gestion des touches, pour les chiffres
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 202174 30 avril 2009 à 06:28
"si j'ai une autre fenetre"
heureseuement que ca ferme pas ton programme...
si tu ais allusion a la fenetre 'about me'...
ouvre la comme il aut, deja:
FrmAbout.Show vbModal
ce sera bien plus efficace.
après, si tu veux tout fermer, suffit de décharger toutes les Forms :
Dim oForm As Form
For Each oForm in Forms
Is Not oForm Is Me Then
Unload oForm
End If
next oForm
unload me
Alors oui, le code est bien plus long que
End
Mais le résultat est similaire... en apparence. End coupe tout.... d'un coup.
pas de déchargement des objets, pas de _QueryUnload.... c'est très brutal.