Besoin d'aide, sortir d'un For avec un seul bouton

cs_Aramil Messages postés 5 Date d'inscription lundi 6 novembre 2000 Statut Membre Dernière intervention 21 janvier 2005 - 20 janv. 2005 à 14:30
cs_Aramil Messages postés 5 Date d'inscription lundi 6 novembre 2000 Statut Membre Dernière intervention 21 janvier 2005 - 21 janv. 2005 à 16:25
J'explique, j'ai un seul bouton sur mon formulaire.

Quand je clique dessus, il lance un For... Next, et il se renomme(btn_1.text) en "Stop".
Je veux que quand je clique sur Stop, sa stop ma boucle et sa réaffiche "Start" sur le bouton.

Voilà mon code si ca peux aider:



Dim annul
As
Boolean



If btn_1.Text = "Start"
Then



Dim rand
As
New Random



Dim rand2
As
New Random



Dim nb
As
Integer



Dim nb2
As
Integer



Dim total
As
Integer



Dim chiffre
As
Integer


total = 0


nb2 = rand2.Next()


btn_1.Text = "Stop"



For nb = 0
To nb2


chiffre = rand.Next(1000)


Label1.Text = chiffre


total = total + chiffre



If annul =
True
Then



Exit
For



End
If



Next



Else


annul =
True


btn_1.Text = "Start"


Application.DoEvents()



End
If

7 réponses

cs_Aramil Messages postés 5 Date d'inscription lundi 6 novembre 2000 Statut Membre Dernière intervention 21 janvier 2005
20 janv. 2005 à 14:41
Ok, j'ai trouver tout seul, il fallait mettre le Dim annul as Boolean en dehors du bouton, comme ca:



Dim annul
As
Boolean



Private
Sub btn_1_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles btn_1.Click



If btn_1.Text = "Start"
Then



Dim rand
As
New Random



Dim rand2
As
New Random



Dim nb
As
Integer



Dim nb2
As
Integer



Dim total
As
Integer



Dim chiffre
As
Integer


annul =
False


total = 0


nb2 = rand2.Next()


btn_1.Text = "Stop"



For nb = 0
To nb2


chiffre = rand.Next(1000)


Label1.Text = chiffre


total = total + chiffre


Application.DoEvents()



If annul =
True
Then



Exit
For



End
If



Next



Else


annul =
True


btn_1.Text = "Start"



End
If
0
mrdep1978 Messages postés 402 Date d'inscription jeudi 25 novembre 2004 Statut Membre Dernière intervention 7 juin 2009 7
20 janv. 2005 à 14:50
C'est presque ça :

Il faut que ton boolean d'annulation soit déclaré en variable globale de ta Form et le DoEvents doit être dans la boucle For. C'est lui qui va faire en sorte que ton programme puisse récupérer le 2e click sur ton bouton. Le DoEvents dans la partie "Stop" ne sert à rien car, comme il est à la fin du code, tu sors de l'événement de toute façon.

Option Explicit

Private annul As Boolean

Private Sub btn_1_Click()

If btn_1.Text = "Start" Then
Dim rand As New Random
Dim rand2 As New Random
Dim nb As Integer
Dim nb2 As Integer
Dim total As Integer
Dim chiffre As Integer

'Initialise le bool d'annulation
annul = False
total = 0
nb2 = rand2.Next()
btn_1.Text = "Stop"
For nb = 0 To nb2
chiffre = rand.Next(1000)
Label1.Text = chiffre
total = total + chiffre
'Permet de récupérer le click
DoEvents
If annul = True Then
Exit For
End If
Next
Else
'Met le bool d'annulation à vrai pr sortir de la boucle
annul = True
btn_1.Text = "Start"
<STRIKE>Application.DoEvents()</STRIKE>
End If


End Sub
0
cs_Aramil Messages postés 5 Date d'inscription lundi 6 novembre 2000 Statut Membre Dernière intervention 21 janvier 2005
20 janv. 2005 à 15:10
Ok, merci c'est bon. Maintenant je veux compte le temps écouler entre le 1er et le 2eme clique(ou bien la fin de la génération des chiffres)... Je pense qu'il me faut un timer, mais je veux afficher le temps écouler dans une Messagebox après qu'on est cliqué sur "stop"...

Aide plz^^
0
mrdep1978 Messages postés 402 Date d'inscription jeudi 25 novembre 2004 Statut Membre Dernière intervention 7 juin 2009 7
20 janv. 2005 à 21:41
Tu déclares une variable HeureDebut as Date en global à la feuille.
Tu l'initialises a HeureDebut = Now dans le 1e clic et tu affiches
Format(Now -HeureDebut, "hh:nn:ss") dans le 2e clic
0

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

Posez votre question
cs_Aramil Messages postés 5 Date d'inscription lundi 6 novembre 2000 Statut Membre Dernière intervention 21 janvier 2005
20 janv. 2005 à 21:56
Sa ne marche pas, pourrai tu me donner le code exact a partir de celui que je fourni plus haut ? Merci d'avance :)
0
cqui789 Messages postés 261 Date d'inscription jeudi 13 janvier 2005 Statut Membre Dernière intervention 18 mai 2009 3
21 janv. 2005 à 01:16
j'ai sorti le text="start" pour si tu voulait pouvoir relancer la boucle une fois qu'elle est finie

le temp ecoule est dans la variable tempecoule





Dim annul
As
Boolean



dim tempecoule as long





Private
Sub btn_1_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles btn_1.Click



If btn_1.Text = "Start"
Then





Dim rand
As
New Random



Dim rand2
As
New Random



Dim nb
As
Integer





Dim nb2
As
Integer





Dim total
As
Integer





Dim chiffre
As
Integer



tempecoule = now







annul = False





total = 0



nb2 = rand2.Next()



btn_1.Text = "Stop"



For nb = 0
To nb2



chiffre = rand.Next(1000)



Label1.Text = chiffre



total = total + chiffre



Application.DoEvents()



If annul =
True
Then





Exit
For





End
If





Next





Else





annul = True










End
If


btn_1.Text = "Start"




tempecoule = now -
tempecoule



end sub
0
cs_Aramil Messages postés 5 Date d'inscription lundi 6 novembre 2000 Statut Membre Dernière intervention 21 janvier 2005
21 janv. 2005 à 16:25
Je précise que je suis en VB.net... Car là le code ne marche pas. Il veux pas du " tempsecoule = now"
0
Rejoignez-nous