好吧,伙计们我有项目(美食时报(好吗? 我有5次食物
(早上,中午,日落,晚上,午夜(
- 早上 : 03:20 上午
- 中午 : 12:05 下午
- 日落 : 03:46 PM
- 晚上 : 07:33 下午
- 午夜 : 08:28 下午
现在我需要放计时器来检查 它将当前时间与数组时间匹配 如果他找到数组到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% 工作,但需要System.Linq.dll在 .Net Fremwork 3.5中,因为选择功能这就是为什么我想要不需要此功能的东西(选择( 并且不需要System.Linq.dllin .Net fremwork 3.5
这应该是为了你想要的。在每个计时器时钟周期中,sub 遍历您的数组并根据数组检查当前时间,并且时差小于 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