好吧,我为(美食时报)设计了项目,好吗? 我有五次吃饭
(早晨,中午,日落,夜晚,午夜)< / p>
现在我需要放置计时器进行检查 它将当前时间与数组时间匹配 如果他找到阵列的时间到PC的当前时间,则显示一条消息
我实际上是在努力并编写了代码
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim TimesArray() As String = {"03:20 AM", "12:05 PM", "03:46 PM", "07:33 PM", "08:28 PM"}
Dim NamesArray() As String = {"Morning", "Midday", "Sunset", "Night", "Midnight"}
Dim time = DateTime.Now.TimeOfDay
Dim q = TimesArray.Select(Function(t, i) New With {.Time = DateTime.Parse(t).TimeOfDay, .I = i}).Select(Function(d) New With {d.Time, .Diff = If(d.Time >= time, d.Time - time, New TimeSpan(24, 0, 0) + d.Time - time), d.I})
Dim m = q.Min(Function(d) d.Diff)
Dim r = q.First(Function(d) d.Diff = m)
MsgBox("Food" & " " & NamesArray(r.I) & " " & "After" & " " & r.Diff.Hours & " " & "Hour" & " " & "and" & " " & r.Diff.Minutes & " " & "Minutes.")
End Sub
和它可以100%工作,但是由于选择功能<,在 .Net Fremwork 3.5 中需要 System.Linq.dll 。 / strong> 这就是为什么我想要一些不需要此功能的东西(选择) 并且不需要 .Net fremwork 3.5
中的 System.Linq.dll答案 0 :(得分:0)
这应该满足您的需求。在每个计时器滴答声中,该子项都会遍历您的数组,并对照该数组检查当前时间,并且时差小于0小时0分钟,将显示消息框。
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim TimesArray() As String = {"03:20 AM", "12:05 PM", "03:46 PM", "07:33 PM", "08:28 PM"}
Dim NamesArray() As String = {"Morning", "Midday", "Sunset", "Night", "Midnight"}
For i As Integer = 0 To TimesArray.Count - 1
Dim tempDateTime As Date = Date.Parse(TimesArray(i))
Dim tempTimeDiff As TimeSpan = tempDateTime.Subtract(DateTime.Now)
If tempTimeDiff.Hours = 0 And tempTimeDiff.Minutes = 0 Then
MessageBox.Show("Food " & NamesArray(i) & " After " & tempDateTime.Hour & " and " & tempDateTime.Minute & " Minutes.")
End If
Next
End Sub