Hallo!
So etwa????
Public Class frmCheckDateTime
'Für die Auswahl der Zeitintervalle
Dim dt As New DataTable
Dim bs As New BindingSource
Dim dgv As New DataGridView With _
{.Parent = Me, .DataSource = bs, .Width = 300, .ReadOnly = true}
'Controls für Eingabe des zu prüfenden Zeitpunktes
Dim datepicker As New DateTimePicker With _
{.Parent = Me, .Left = 320, .Width = 100, _
.Format = DateTimePickerFormat.Short}
Dim timepicker As New DateTimePicker With _
{.Parent = Me, .Left = 430, .Width = 100, _
.Format = DateTimePickerFormat.Time, .ShowUpDown = True}
'Für die Prüfung des Zeitpunktes
Dim WithEvents btnCheck As New Button With _
{.Parent = Me, .Left = 320, .Width = 100, .Top = 100, .Text = "Check"}
Private Sub frmCheckDateTime_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
Me.Width = 550
Me.Height = 500
'Liste der wählbaren Zeitintervalle in die Datatable eintragen
dt.Columns.Add("Weekday", GetType(String))
dt.Columns.Add("StartTime", GetType(String))
Dim wd As String = GetWeekday(Now)
For i As Integer = 1 To 7
Dim wdn As String = _
DateAndTime.WeekdayName(i, False, FirstDayOfWeek.Monday)
For hr As Integer = 0 To 23
dt.Rows.Add(wdn, hr.ToString & ":00")
dt.Rows.Add(wdn, hr.ToString & ":30")
Next hr
Next i
bs.DataSource = dt
End Sub
Private Function GetWeekday(ByVal dtim As DateTime) As String
Return dtim.ToString("dddd", Globalization.CultureInfo.CurrentCulture)
End Function
Private Function IsIntime(ByVal r As DataRow, dtim As DateTime) As Boolean
'Liegt dtim im Zeitintervall, das in r eingetragen ist ??
Dim wdn As String = GetWeekday(dtim)
If Not wdn = r("Weekday").ToString Then Return False
Dim rhour As Integer = CInt(r("StartTime").ToString.Split(":"c)(0))
Dim rmin As Integer = CInt(r("StartTime").ToString.Split(":"c)(1))
If Not dtim.Hour = rhour Then Return False
If dtim.Minute < 30 AndAlso rmin = 30 Then Return False
If dtim.Minute >= 30 AndAlso rmin = 0 Then Return False
Return True
End Function
Private Function CheckDateTime(ByVal dtim As DateTime) As Boolean
For i As Integer = 0 To bs.Count - 1
If dgv.Rows(i).Selected Then
'Ein gewähltes Zeitintervall checken
If IsIntime(DirectCast(bs(i), DataRowView).Row, dtim) _
Then Return True
End If
Next i
Return False
End Function
Private Sub btnCheck_Click(sender As Object, _
e As System.EventArgs) Handles btnCheck.Click
'Gewähltes Datum und und gewählte Uhrzeit zusammenfügen
Dim dtim As DateTime = datepicker.Value.Date
dtim = dtim.AddHours(timepicker.Value.Hour)
dtim = dtim.AddMinutes(timepicker.Value.Minute)
If CheckDateTime(dtim) Then
MsgBox("Zeitpunkt liegt in einem gewählten Intervall")
Else
MsgBox("Zeitpunkt liegt nicht in einem gewählten Intervall")
End If
End Sub
End Class Füe die komfortable Auswahl der Intervalle, könntest Du eine zusätzliche
Boolsche Spalte in die Datatable einfügen.
Beitrag wurde zuletzt am 28.05.17 um 10:29:52 editiert. |