Korrektur-Timer - Frage

  • VB.NET
  • .NET (FX) 4.0

Es gibt 39 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Also das Problem bei einer MessageBox im Timer ist einfach folgendes: Wenn du irgendeinen kleinen Code-Fetzen falsch programmiert hast, oder der User einfach immer wieder die gleiche, oder eine falsche eingabe macht, welche du dann in ​Timer.Tick abfragst, und eventuell eine MessageBox ausgibst, dann ist die Kacke -je nach Timer-Intervall- erst richtig am dampfen, weil der User dann mit MessageBoxen zugespamt wird.
    Ich habe das ganze so geändert und in eine Sub geschoben.
    Ist das so besser?

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub Zünd_Timer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Zünd_timer.Tick
    2. Label_Time.Text = stopwatch.Elapsed.ToString.Remove(10)
    3. timer_vergleichen()
    4. End Sub
    5. Sub timer_vergleichen()
    6. Dim LT As DateTime = DateTime.Parse(Label_Time.Text)
    7. Dim t1 As DateTime = DateTime.Parse(TextBox1.Text)
    8. Dim t2 As DateTime = DateTime.Parse(TextBox2.Text)
    9. Dim t3 As DateTime = DateTime.Parse(TextBox3.Text)
    10. Dim t4 As DateTime = DateTime.Parse(TextBox4.Text)
    11. Dim t5 As DateTime = DateTime.Parse(TextBox5.Text)
    12. Dim t6 As DateTime = DateTime.Parse(TextBox6.Text)
    13. Dim t7 As DateTime = DateTime.Parse(TextBox7.Text)
    14. Dim t8 As DateTime = DateTime.Parse(TextBox8.Text)
    15. Dim t9 As DateTime = DateTime.Parse(TextBox9.Text)
    16. Dim t10 As DateTime = DateTime.Parse(TextBox10.Text)
    17. Dim t11 As DateTime = DateTime.Parse(TextBox11.Text)
    18. Dim t12 As DateTime = DateTime.Parse(TextBox12.Text)
    19. Dim t13 As DateTime = DateTime.Parse(TextBox13.Text)
    20. Dim t14 As DateTime = DateTime.Parse(TextBox14.Text)
    21. Dim t15 As DateTime = DateTime.Parse(TextBox15.Text)
    22. Dim t16 As DateTime = DateTime.Parse(TextBox16.Text)
    23. Dim t17 As DateTime = DateTime.Parse(TextBox17.Text)
    24. Dim t18 As DateTime = DateTime.Parse(TextBox18.Text)
    25. Dim t19 As DateTime = DateTime.Parse(TextBox19.Text)
    26. Dim t20 As DateTime = DateTime.Parse(TextBox20.Text)
    27. Dim t21 As DateTime = DateTime.Parse(TextBox21.Text)
    28. Dim t22 As DateTime = DateTime.Parse(TextBox22.Text)
    29. Dim t23 As DateTime = DateTime.Parse(TextBox23.Text)
    30. Dim t24 As DateTime = DateTime.Parse(TextBox24.Text)
    31. Dim t25 As DateTime = DateTime.Parse(TextBox25.Text)
    32. Dim t26 As DateTime = DateTime.Parse(TextBox26.Text)
    33. Dim t27 As DateTime = DateTime.Parse(TextBox27.Text)
    34. Dim t28 As DateTime = DateTime.Parse(TextBox28.Text)
    35. Dim t29 As DateTime = DateTime.Parse(TextBox29.Text)
    36. Dim t30 As DateTime = DateTime.Parse(TextBox30.Text)
    37. Dim t31 As DateTime = DateTime.Parse(TextBox31.Text)
    38. Dim t32 As DateTime = DateTime.Parse(TextBox32.Text)
    39. Dim t33 As DateTime = DateTime.Parse(TextBox33.Text)
    40. Dim t34 As DateTime = DateTime.Parse(TextBox34.Text)
    41. Dim t35 As DateTime = DateTime.Parse(TextBox35.Text)
    42. Dim t36 As DateTime = DateTime.Parse(TextBox36.Text)
    43. Dim t37 As DateTime = DateTime.Parse(TextBox37.Text)
    44. Dim t38 As DateTime = DateTime.Parse(TextBox38.Text)
    45. Dim t39 As DateTime = DateTime.Parse(TextBox39.Text)
    46. Dim t40 As DateTime = DateTime.Parse(TextBox40.Text)
    47. If LT >= t1 Then
    48. TextBox1.BackColor = Color.Red
    49. Else
    50. TextBox1.BackColor = Color.White
    51. End If
    52. If LT >= t2 Then
    53. TextBox2.BackColor = Color.Red
    54. Else
    55. TextBox2.BackColor = Color.White
    56. End If
    57. If LT >= t3 Then
    58. TextBox3.BackColor = Color.Red
    59. Else
    60. TextBox3.BackColor = Color.White
    61. End If
    62. If LT >= t4 Then
    63. TextBox4.BackColor = Color.Red
    64. Else
    65. TextBox4.BackColor = Color.White
    66. End If
    67. If LT >= t5 Then
    68. TextBox5.BackColor = Color.Red
    69. Else
    70. TextBox5.BackColor = Color.White
    71. End If
    72. If LT >= t6 Then
    73. TextBox6.BackColor = Color.Red
    74. Else
    75. TextBox6.BackColor = Color.White
    76. End If
    77. If LT >= t7 Then
    78. TextBox7.BackColor = Color.Red
    79. Else
    80. TextBox7.BackColor = Color.White
    81. End If
    82. If LT >= t8 Then
    83. TextBox8.BackColor = Color.Red
    84. Else
    85. TextBox8.BackColor = Color.White
    86. End If
    87. If LT >= t9 Then
    88. TextBox9.BackColor = Color.Red
    89. Else
    90. TextBox9.BackColor = Color.White
    91. End If
    92. If LT >= t10 Then
    93. TextBox10.BackColor = Color.Red
    94. Else
    95. TextBox10.BackColor = Color.White
    96. End If
    97. If LT >= t11 Then
    98. TextBox11.BackColor = Color.Red
    99. Else
    100. TextBox11.BackColor = Color.White
    101. End If
    102. If LT >= t12 Then
    103. TextBox12.BackColor = Color.Red
    104. Else
    105. TextBox12.BackColor = Color.White
    106. End If
    107. If LT >= t13 Then
    108. TextBox13.BackColor = Color.Red
    109. Else
    110. TextBox13.BackColor = Color.White
    111. End If
    112. If LT >= t14 Then
    113. TextBox14.BackColor = Color.Red
    114. Else
    115. TextBox14.BackColor = Color.White
    116. End If
    117. If LT >= t15 Then
    118. TextBox15.BackColor = Color.Red
    119. Else
    120. TextBox15.BackColor = Color.White
    121. End If
    122. If LT >= t16 Then
    123. TextBox16.BackColor = Color.Red
    124. Else
    125. TextBox16.BackColor = Color.White
    126. End If
    127. If LT >= t17 Then
    128. TextBox17.BackColor = Color.Red
    129. Else
    130. TextBox17.BackColor = Color.White
    131. End If
    132. If LT >= t18 Then
    133. TextBox18.BackColor = Color.Red
    134. Else
    135. TextBox18.BackColor = Color.White
    136. End If
    137. If LT >= t19 Then
    138. TextBox19.BackColor = Color.Red
    139. Else
    140. TextBox19.BackColor = Color.White
    141. End If
    142. If LT >= t20 Then
    143. TextBox20.BackColor = Color.Red
    144. Else
    145. TextBox20.BackColor = Color.White
    146. End If
    147. If LT >= t21 Then
    148. TextBox21.BackColor = Color.Red
    149. Else
    150. TextBox21.BackColor = Color.White
    151. End If
    152. If LT >= t22 Then
    153. TextBox22.BackColor = Color.Red
    154. Else
    155. TextBox22.BackColor = Color.White
    156. End If
    157. If LT >= t23 Then
    158. TextBox23.BackColor = Color.Red
    159. Else
    160. TextBox23.BackColor = Color.White
    161. End If
    162. If LT >= t24 Then
    163. TextBox24.BackColor = Color.Red
    164. Else
    165. TextBox24.BackColor = Color.White
    166. End If
    167. If LT >= t25 Then
    168. TextBox25.BackColor = Color.Red
    169. Else
    170. TextBox25.BackColor = Color.White
    171. End If
    172. If LT >= t26 Then
    173. TextBox26.BackColor = Color.Red
    174. Else
    175. TextBox26.BackColor = Color.White
    176. End If
    177. If LT >= t27 Then
    178. TextBox27.BackColor = Color.Red
    179. Else
    180. TextBox27.BackColor = Color.White
    181. End If
    182. If LT >= t28 Then
    183. TextBox28.BackColor = Color.Red
    184. Else
    185. TextBox28.BackColor = Color.White
    186. End If
    187. If LT >= t29 Then
    188. TextBox29.BackColor = Color.Red
    189. Else
    190. TextBox29.BackColor = Color.White
    191. End If
    192. If LT >= t30 Then
    193. TextBox30.BackColor = Color.Red
    194. Else
    195. TextBox30.BackColor = Color.White
    196. End If
    197. If LT >= t31 Then
    198. TextBox31.BackColor = Color.Red
    199. Else
    200. TextBox31.BackColor = Color.White
    201. End If
    202. If LT >= t32 Then
    203. TextBox32.BackColor = Color.Red
    204. Else
    205. TextBox32.BackColor = Color.White
    206. End If
    207. If LT >= t33 Then
    208. TextBox33.BackColor = Color.Red
    209. Else
    210. TextBox33.BackColor = Color.White
    211. End If
    212. If LT >= t34 Then
    213. TextBox34.BackColor = Color.Red
    214. Else
    215. TextBox34.BackColor = Color.White
    216. End If
    217. If LT >= t35 Then
    218. TextBox35.BackColor = Color.Red
    219. Else
    220. TextBox35.BackColor = Color.White
    221. End If
    222. If LT >= t36 Then
    223. TextBox36.BackColor = Color.Red
    224. Else
    225. TextBox36.BackColor = Color.White
    226. End If
    227. If LT >= t37 Then
    228. TextBox37.BackColor = Color.Red
    229. Else
    230. TextBox37.BackColor = Color.White
    231. End If
    232. If LT >= t38 Then
    233. TextBox38.BackColor = Color.Red
    234. Else
    235. TextBox38.BackColor = Color.White
    236. End If
    237. If LT >= t39 Then
    238. TextBox39.BackColor = Color.Red
    239. Else
    240. TextBox39.BackColor = Color.White
    241. End If
    242. If LT >= t40 Then
    243. TextBox40.BackColor = Color.Red
    244. Else
    245. TextBox40.BackColor = Color.White
    246. End If
    247. End Sub

    Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von „ErfinderDesRades“ ()

    Da ist 40 mal dasselbe gecodet - solch kann man mit einer Schleife besser abhandeln:

    VB.NET-Quellcode

    1. Private Sub Zünd_Timer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Zünd_timer.Tick
    2. Dim time = Stopwatch.Elapsed 'die Zeit merken, nicht das Label
    3. Label_Time.Text = time.ToString.Remove(10)
    4. timer_vergleichen(time)
    5. End Sub
    6. Sub timer_vergleichen(ByVal time As Date) 'die Zeit übergeben, nicht von irgendwoher holen
    7. Dim textboxes As TextBox() = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9, TextBox10, TextBox11, TextBox12, TextBox13, TextBox14, TextBox15, TextBox16, TextBox17, TextBox18, TextBox19, TextBox20, TextBox21, TextBox22, TextBox23, TextBox24, TextBox25, TextBox26, TextBox27, TextBox28, TextBox29, TextBox30, TextBox31, TextBox32, TextBox33, TextBox34, TextBox35, TextBox36, TextBox37, TextBox38, TextBox39, TextBox40}
    8. For Each tb In textboxes
    9. tb.BackColor = If(time >= Date.Parse(tb.Text), Color.Red, Color.White)
    10. Next
    11. End Sub

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „ErfinderDesRades“ ()

    ist gut, wenn man typisiert arbeitet, ne?
    Dann kommt man drauf, dass man keine Datumse vorliegen hat, sondern Zeitspannen.

    VB.NET-Quellcode

    1. Private _Stopwatch As New Stopwatch
    2. Private Sub Zünd_Timer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Zünd_timer.Tick
    3. Dim time = _Stopwatch.Elapsed 'die Zeit merken, nicht das Label
    4. Label_Time.Text = time.ToString.Remove(10)
    5. timer_vergleichen(time)
    6. End Sub
    7. Sub timer_vergleichen(ByVal time As TimeSpan) 'die Zeit übergeben, nicht von irgendwoher holen
    8. Dim textboxes As TextBox() = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9, TextBox10, TextBox11, TextBox12, TextBox13, TextBox14, TextBox15, TextBox16, TextBox17, TextBox18, TextBox19, TextBox20, TextBox21, TextBox22, TextBox23, TextBox24, TextBox25, TextBox26, TextBox27, TextBox28, TextBox29, TextBox30, TextBox31, TextBox32, TextBox33, TextBox34, TextBox35, TextBox36, TextBox37, TextBox38, TextBox39, TextBox40}
    9. For Each tb In textboxes
    10. tb.BackColor = If(time >= TimeSpan.Parse(tb.Text), Color.Red, Color.White)
    11. Next
    12. End Sub
    Der Fehler ist mir unterlaufen, weil ich den Code nicht testen kann, weil ich keine 40 TextBoxen hab
    hier der Screenshot - ist nur ein Testprogramm für eine spätere Anwendung.
    Und jetzt kommt diese Fehlermeldung - 2. Screenshoot
    Bilder
    • VB2.png

      38,59 kB, 1.152×648, 106 mal angesehen
    • vb3.png

      74,77 kB, 1.405×672, 97 mal angesehen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Hicki“ ()

    habe ich gändert auf den Wert 7, sieht dann aber so aus 00:00:0 und keine Zeit läuft. Ich brauche aber Stunden:Minuten:Sekunden.Zentelsekunde
    Ich habe das Projekt mal angehangen.

    Gruß Andreas
    Dateien

    Hicki schrieb:

    Und wo plaziere ich die MessageBoxen im Code?
    Wenn das lediglich zu Testzwecken dient, mach Die ein Label oder mach eine Ausgabe über Console.WriteLine("bla"), das wird dann im Studio unten im Debug-Tab ausgegeben.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!