1 Previous Next 

Scheduler with a dataforms DataGridView and a dataset writeXML


In the forums is often the question "How can I make a scheduler, one of the good answers is, use the inbuild scheduler of the OS.
However, it is also a good learning practise to try to make it yourself.
Therefore we have created a simple sample for that scheduler here.
It needs only a label with beneath that a datetimepicer and a DataGridView on a Windows Forms form
Public Class Form1
    'SP10SP1 style code
    Private WithEvents Tm As New Timer With {.Enabled = False, .Interval = 3000}
    'Be aware that 3 seconds is to short
    Private DS As New DataSet
    Private Sub Form1_Load(sender As Object, e As System.EventArgsHandles Me.Load
        DateTimePicker1.Format = DateTimePickerFormat.Time
        Label1.Text = Now.ToLongDateString
        If IO.File.Exists(IO.Path.Combine(CStr(Environment.SpecialFolder.ApplicationData),
                                          Me.CompanyName, "Scheduler.xlm")) Then
            DS.ReadXml(IO.Path.Combine(CStr(Environment.SpecialFolder.ApplicationData),
                                       Me.CompanyName, "Scheduler.xlm"))
            Tm.Enabled = True
        Else
            DS.Tables.Add(New DataTable("Schedule"))
            DS.Tables(0).Columns.Add(New DataColumn("Appointment"))
            IO.Directory.CreateDirectory(IO.Path.Combine(CStr(Environment.
                                                         SpecialFolder.ApplicationData), Me.CompanyName))
        End If
        'Set the appointments in order of appointment
        DS.Tables(0).DefaultView.Sort = "Appointment"
        DataGridView1.DataSource = DS.Tables(0).DefaultView
        'Next appointment
        DS.Tables(0).DefaultView.RowFilter = "Appointment > '" & CStr(Now) & "'"
        If DS.Tables(0).DefaultView.Count > 0 Then
            TextBox1.Text = CStr(DS.Tables(0).DefaultView(0)(0))
        End If
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.
                              EventArgsHandles Button1.Click
        Dim nr As DataRow = DS.Tables(0).NewRow
        nr(0) = DateTimePicker1.Value
        DS.Tables(0).Rows.Add(nr)
        Tm.Enabled = True
    End Sub
    Private Sub Tm_Tick(sender As Object, e As System.EventArgsHandles Tm.Tick
        If DS.Tables(0).DefaultView.Count > 0 Then
            If CDate(DS.Tables(0).DefaultView(0)(0)) < Now.
                AddSeconds(4) And CDate(DS.Tables(0).DefaultView(0)(0)) > Now.AddSeconds(-3) Then
                MessageBox.Show("You Have an appointment")
            End If
            DS.Tables(0).DefaultView.RowFilter = "Appointment > '" & CStr(Now) & "'"
        End If
    End Sub
    Private Sub Form1_FormClosing(sender As Object,
                                  e As System.Windows.Forms.FormClosingEventArgsHandles Me.FormClosing
        DS.WriteXml(IO.Path.Combine(CStr(Environment.SpecialFolder.ApplicationData), Me.CompanyName, "Scheduler.xlm"))
    End Sub
End Class



1 Previous Next