Bonjour à tous !

Je réalise actuellement un programme et j'ai besoin de votre aide : je développe en VB.net sous visual studio 2019

Pour vous donner le contexte, c'est une appli qui va servir en prod : j'ai un Userform avec 10 boutons : 9 d'entre eux représentent une quantité de pièces et le dernier un bouton RESET

L'idée c'est que si l'utilisateur voit son stock de pièces diminuer et être presque à sec, il va appuyer une fois sur le bouton correspondant à la pièce. Ce bouton va donc changer de couleur et devenir orange. Là, le programme va enregistrer la date au format jj/mm/aa hh/mm/ss (on a envie de récupérer ces informations pour voir si on peut pas faire d'approvisionnement en pièce de façon prédictive). Puis, si le stock est vide, l'opérateur ré-appuie sur le bouton et celui-ci devient rouge : le programme ré enregistre la date.
le bouton reset est utilisé dès que l'employé voit son stock de pièce re remplit : l'appli enregistre la date et fais le calcul du temps passé avec le stock vide/presque vide.
je récupère le tout sur un fichier texte pour le moment (plus tard ça sera directement sur un fichier excel (j'ai un peu de mal avec le lien VB excel mais je pense avoir trouver un truc).
tout ce code fonctionne avec des FLAGS : qui permettent de gérer l'état des boutons.

Vu que l'opérateur peut être à cours de plusieurs pièces en même temps, j'ai fais le choix de ne pas passer par une fonction pour éviter d'écraser des variables (c'est peut être pas judicieux mais pour l'instant ça marche).

Le soucis que j'ai avec ce programme, c'est que je ne suis pas capable de récupérer le temps réel d'arrêt de la cellule.
Je m'explique : si l'utilisateur vient à cours de 3 pièces différentes, le temps d'arrêt n'est pas obligatoirement la somme des 3 temps d'arrêt.

Et c'est là que ça pêche. je peux récupérer les temps d'arrêt respectif par pièces, mais je n'arrive pas à créer un programme qui tournerait en tâche de fond
Autrement dit un chrono qui start dès qu'un bouton est rouge, qui stop dès que tous les boutons du Userform ne sont plus rouge (ils peuvent être orange pour autant, le chrono doit s'arreter).
De plus, j'ai besoin de récupérer cette valeur (valeur chronométrée) en temps réel et de l'enregistrer dans le même fichier texte que celui cité plus haut, mais dans une nouvelle colonne.


Voici le programme que j'ai écrit :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
Imports System.IO
Imports System.Text
 
Imports Excel = Microsoft.Office.Interop.Excel
 
Public Class DEMO
 
    Dim pathwriter As StreamWriter
    Dim reset As Boolean = False
    Dim resetcounter As Integer = 0
    'Dim flagtimer As Integer = 0
    Dim Fronts, Backs, Elem, Screws, Boxes, Wires, Poly, Wf, others As Integer 'flags
    Dim cellN As String
    Dim df1, df2, db1, db2, de1, de2, ds1, ds2, dx1, dx2, dw1, dw2, dp1, dp2, dwf1, dwf2, do1, do2, path As String
    Dim df, dfR, dfe, db, dbr, dbe, de, der, dee, ds, dsr, dse, dx, dxr, dxe, dw, dwr, dwe, dp, dpr, dpe, dwf, dwfr, dwfe, dot, dotr, dote As Date
    'Dim dreal, dreale As Date
    'Dim dstop As String
 
    Private Sub butReset_Click(sender As Object, e As EventArgs) Handles butReset.Click
        If resetcounter = 0 Then
            resetcounter = resetcounter + 1
            reset = True
            butReset.BackColor = Color.Green
        Else
            resetcounter = 0
            reset = False
            butReset.BackColor = Color.LightGray
        End If
' dernier essai en date  pour répondre au problème : pour l'instant peu concluant
        'If Fronts = 2 Then
        'dreal = DateTime.Now
        ' ElseIf Backs = 2 Then
        'dreal = DateTime.Now
        'ElseIf Elem = 2 Then
        'dreal = DateTime.Now
        'ElseIf Screws = 2 Then
        'dreal = DateTime.Now
        'ElseIf Boxes = 2 Then
        'dreal = DateTime.Now
        'ElseIf Wires = 2 Then
        'dreal = DateTime.Now
        ' ElseIf Poly = 2 Then
        'dreal = DateTime.Now
        'ElseIf Wf = 2 Then
        'dreal = DateTime.Now
        'ElseIf others = 2 Then
        ' dreal = DateTime.Now
        ' If Fronts = 0 And Backs = 0 And Elem = 0 And Screws = 0 And Boxes = 0 And Wires = 0 And Poly = 0 And Wf = 0 And others = 0 Then
        ' dreale = DateTime.Now
        '     dstop = DateDiff(DateInterval.Second, dreal, dreale)
        'pathwriter = New StreamWriter(path, True, Encoding.Unicode)
        'pathwriter.WriteLine("Stoppage time of " & "Cell N" & cellN & "; " & dstop)
        '      pathwriter.Close()
        'End If
        'End If
    End Sub
 
 
    Private Sub butOthers_Click(sender As Object, e As EventArgs) Handles butOthers.Click
 
        If reset = False Then
            If others = 0 Then
                butOthers.BackColor = Color.Orange
                others = others + 1
                dot = DateTime.Now
            ElseIf others = 1 Then
                butOthers.BackColor = Color.Red
                others = others + 1
                dotr = DateTime.Now
            End If
        ElseIf reset = True Then
            others = 0
            butOthers.BackColor = Color.LightGray
            dote = DateTime.Now
            do1 = DateDiff(DateInterval.Second, dotr, dote)
            do2 = DateDiff(DateInterval.Second, dot, dote)
            pathwriter = New StreamWriter(path, True, Encoding.Unicode)
            pathwriter.WriteLine("Cell" & cellN & " ; " & "Others" & " ; " & dot & " ; " & TimeSpan.FromSeconds(do2).ToString("hh\:mm\:ss") & " ; " & dotr & " ; " & TimeSpan.FromSeconds(do1).ToString("hh\:mm\:ss"))
            pathwriter.Close()
        End If
    End Sub
 
    Private Sub butWires_Click(sender As Object, e As EventArgs) Handles butWires.Click
 
        If reset = False Then
            If Wires = 0 Then
                butWires.BackColor = Color.Orange
                Wires = Wires + 1
                dw = DateTime.Now
 
            ElseIf Wires = 1 Then
                butWires.BackColor = Color.Red
                Wires = Wires + 1
                dwr = DateTime.Now
            End If
        ElseIf reset = True Then
            butWires.BackColor = Color.LightGray
            Wires = 0
            dwe = DateTime.Now
            dw1 = DateDiff(DateInterval.Second, dwr, dwe)
            dw2 = DateDiff(DateInterval.Second, dw, dwe)
            pathwriter = New StreamWriter(path, True, Encoding.Unicode)
            pathwriter.WriteLine("Cell" & cellN & " ; " & "Wires" & " ; " & dw & " ; " & TimeSpan.FromSeconds(dw2).ToString("hh\:mm\:ss") & " ; " & dwr & " ; " & TimeSpan.FromSeconds(dw1).ToString("hh\:mm\:ss"))
            pathwriter.Close()
        End If
    End Sub
 
    Private Sub ButScrews_Click(sender As Object, e As EventArgs) Handles butScrews.Click
        If reset = False Then
            If Screws = 0 Then
                butScrews.BackColor = Color.Orange
                Screws = Screws + 1
                ds = DateTime.Now
 
            ElseIf Screws = 1 Then
                butScrews.BackColor = Color.Red
                Screws = Screws + 1
                dsr = DateTime.Now
            End If
        ElseIf reset = True Then
            butScrews.BackColor = Color.LightGray
            Screws = 0
            dse = DateTime.Now
            ds1 = DateDiff(DateInterval.Second, dsr, dse)
            ds2 = DateDiff(DateInterval.Second, ds, dse)
            pathwriter = New StreamWriter(path, True, Encoding.Unicode)
            pathwriter.WriteLine("Cell" & cellN & " ; " & "Screws" & " ; " & ds & " ; " & TimeSpan.FromSeconds(ds2).ToString("hh\:mm\:ss") & " ; " & dsr & " ; " & TimeSpan.FromSeconds(ds1).ToString("hh\:mm\:ss"))
            pathwriter.Close()
        End If
    End Sub
 
    Private Sub ButPoly_Click(sender As Object, e As EventArgs) Handles butPoly.Click
        If reset = False Then
            If Poly = 0 Then
                butPoly.BackColor = Color.Orange
                Poly = Poly + 1
                dp = DateTime.Now
 
            ElseIf Poly = 1 Then
                butPoly.BackColor = Color.Red
                Poly = Poly + 1
                dpr = DateTime.Now
            End If
        ElseIf reset = True Then
            butPoly.BackColor = Color.LightGray
            Poly = 0
            dpe = DateTime.Now
            dp1 = DateDiff(DateInterval.Second, dpr, dpe)
            dp2 = DateDiff(DateInterval.Second, dp, dpe)
            pathwriter = New StreamWriter(path, True, Encoding.Unicode)
            pathwriter.WriteLine("Cell" & cellN & " ; " & "Poly" & " ; " & dp & " ; " & TimeSpan.FromSeconds(dp2).ToString("hh\:mm\:ss") & " ; " & dpr & " ; " & TimeSpan.FromSeconds(dp1).ToString("hh\:mm\:ss"))
            pathwriter.Close()
        End If
    End Sub
 
 
    Private Sub butboxes_Click(sender As Object, e As EventArgs) Handles butboxes.Click
        If reset = False Then
            If Boxes = 0 Then
                butboxes.BackColor = Color.Orange
                Boxes = Boxes + 1
                dx = DateTime.Now
 
            ElseIf Boxes = 1 Then
                butboxes.BackColor = Color.Red
                Boxes = Boxes + 1
                dxr = DateTime.Now
            End If
        ElseIf reset = True Then
            butboxes.BackColor = Color.LightGray
            Boxes = 0
            dxe = DateTime.Now
            dx1 = DateDiff(DateInterval.Second, dxr, dxe)
            dx2 = DateDiff(DateInterval.Second, dx, dxe)
            pathwriter = New StreamWriter(path, True, Encoding.Unicode)
            pathwriter.WriteLine("Cell" & cellN & " ; " & "Boxes" & " ; " & dx & " ; " & TimeSpan.FromSeconds(dx2).ToString("hh\:mm\:ss") & " ; " & dxr & " ; " & TimeSpan.FromSeconds(dx1).ToString("hh\:mm\:ss"))
            pathwriter.Close()
        End If
    End Sub
 
 
    Private Sub butwall_Click(sender As Object, e As EventArgs) Handles butwall.Click
        If reset = False Then
            If Wf = 0 Then
                butwall.BackColor = Color.Orange
                Wf = Wf + 1
                dwf = DateTime.Now
 
            ElseIf Wf = 1 Then
                butwall.BackColor = Color.Red
                Wf = Wf + 1
                dwfr = DateTime.Now
            End If
        ElseIf reset = True Then
            butwall.BackColor = Color.LightGray
            Wf = 0
            dwfe = DateTime.Now
            dwf1 = DateDiff(DateInterval.Second, dwfr, dwfe)
            dwf2 = DateDiff(DateInterval.Second, dwf, dwfe)
            pathwriter = New StreamWriter(path, True, Encoding.Unicode)
            pathwriter.WriteLine("Cell" & cellN & " ; " & "Wallframes" & " ; " & dwf & " ; " & TimeSpan.FromSeconds(dwf2).ToString("hh\:mm\:ss") & " ; " & dwfr & " ; " & TimeSpan.FromSeconds(dwf1).ToString("hh\:mm\:ss"))
            pathwriter.Close()
        End If
    End Sub
 
 
 
    Private Sub butElem_Click(sender As Object, e As EventArgs) Handles butElem.Click
        If reset = False Then
            If Elem = 0 Then
                butElem.BackColor = Color.Orange
                Elem = Elem + 1
                de = DateTime.Now
 
            ElseIf Elem = 1 Then
                butElem.BackColor = Color.Red
                Elem = Elem + 1
                der = DateTime.Now
            End If
        ElseIf reset = True Then
            butElem.BackColor = Color.LightGray
            Elem = 0
            dee = DateTime.Now
            de1 = DateDiff(DateInterval.Second, der, dee)
            de2 = DateDiff(DateInterval.Second, de, dee)
            pathwriter = New StreamWriter(path, True, Encoding.Unicode)
            pathwriter.WriteLine("Cell" & cellN & " ; " & "Elements" & " ; " & de & " ; " & TimeSpan.FromSeconds(de2).ToString("hh\:mm\:ss") & " ; " & der & " ; " & TimeSpan.FromSeconds(de1).ToString("hh\:mm\:ss"))
            pathwriter.Close()
        End If
    End Sub
 
 
    Private Sub butBack_Click(sender As Object, e As EventArgs) Handles butBack.Click
        If reset = False Then
            If Backs = 0 Then
                butBack.BackColor = Color.Orange
                Backs = Backs + 1
                db = DateTime.Now
 
            ElseIf Backs = 1 Then
                butBack.BackColor = Color.Red
                Backs = Backs + 1
                dbr = DateTime.Now
            End If
        ElseIf reset = True Then
            butBack.BackColor = Color.LightGray
            Backs = 0
            dbe = DateTime.Now
            db1 = DateDiff(DateInterval.Second, dbr, dbe)
            db2 = DateDiff(DateInterval.Second, db, dbe)
            pathwriter = New StreamWriter(path, True, Encoding.Unicode)
            pathwriter.WriteLine("Cell" & cellN & " ; " & "Backs" & " ; " & db & " ; " & TimeSpan.FromSeconds(db2).ToString("hh\:mm\:ss") & " ; " & dbr & " ; " & TimeSpan.FromSeconds(db1).ToString("hh\:mm\:ss"))
            pathwriter.Close()
        End If
    End Sub
 
    Private Sub butFronts_Click(sender As Object, e As EventArgs) Handles butFronts.Click
        If reset = False Then
            If Fronts = 0 Then
                butFronts.BackColor = Color.Orange
                Fronts = Fronts + 1
                df = DateTime.Now
 
            ElseIf Fronts = 1 Then
                butFronts.BackColor = Color.Red
                Fronts = Fronts + 1
                dfR = DateTime.Now
            End If
        ElseIf reset = True Then
            butFronts.BackColor = Color.LightGray
            Fronts = 0
            dfe = DateTime.Now
            'lbldatef.Text = df 
            'lbldatefr.Text = dfR
            df1 = DateDiff(DateInterval.Second, dfR, dfe)
            df2 = DateDiff(DateInterval.Second, df, dfe)
            'lblFront.Text = TimeSpan.FromSeconds(df1).ToString("hh\:mm\:ss")
            pathwriter = New StreamWriter(path, True, Encoding.Unicode)
            pathwriter.WriteLine("Cell" & cellN & " ; " & "Fronts" & " ; " & df & " ; " & TimeSpan.FromSeconds(df2).ToString("hh\:mm\:ss") & " ; " & dfR & " ; " & TimeSpan.FromSeconds(df1).ToString("hh\:mm\:ss"))
            pathwriter.Close()
        End If
    End Sub
 
 
    Private Sub DEMO_Load(sender As Object, e As EventArgs) Handles Me.Load
        'initialisation
        Fronts = 0
        Backs = 0
        Elem = 0
        Screws = 0
        Boxes = 0
        Wires = 0
        Poly = 0
        Wf = 0
        others = 0
        'Choix du numéro de cellule
        cellN = InputBox("please, Write a cell number")
        While cellN = ""
            cellN = InputBox("please, Write a cell number")
        End While
 
        'chemin du fichier texte
        OpenFileDialog2.Filter = "Text (*.txt)|*.txt"
        If OpenFileDialog2.ShowDialog() = Windows.Forms.DialogResult.OK Then
            path = OpenFileDialog2.FileName
        Else
            MsgBox("No file selected, please choose one file", MsgBoxStyle.Exclamation, "No file selected")
        End If
        pathwriter = New StreamWriter(path, True, Encoding.Unicode)
        pathwriter.WriteLine("Cell" & " ; " & "part" & " ; " & "Amber" & " ; " & "Amber time" & " ; " & "RED" & " ; " & "Stoppage")
        pathwriter.Close()
    End Sub
 
 
 
End Class
Si vous pouviez m'aider ça serait super sympa. J'ai parcouru plusieurs forums pour trouver une réponse mais je ne sais même pas si ce que je cherche à programmer porte un nom particulier. j'ai rien trouver de tel et je ne sais même pas si c'est faisable en VB.net du coup

Merci par avance !