Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Win32

Desktop Alarm Clock

Rate me:
Please Sign up or sign in to vote.
4.73/5 (21 votes)
23 Jan 2010CPOL3 min read 127.9K   6.8K   82  
The Desktop Alarm Clock is a very useful application that can perform several tasks.
Imports System.Windows.Forms

#Region " Enumarations "

Public Enum EventFilter
    Notify = 0
    Execute = 1
    All = 2
End Enum

#End Region

Public Class CalendarDialog

    Dim _scheduledEvents As New List(Of ClockDBDataSet.EventsTBRow)
    Dim _selectedEvents As New System.Collections.ObjectModel.Collection(Of ClockDBDataSet.EventsTBRow)

#Region " Methods "

    Private Function GetAllDates() As Date()
        Dim dates As New List(Of Date)
        For Each dr As ClockDBDataSet.EventsTBRow In Me._scheduledEvents
            If dr.Alarm = "Once" Then
                dates.Add(dr.OnceDate)
            End If
        Next
        Return dates.ToArray
    End Function

    Private Sub PrintEvents()
        Me.RichTextBoxCtrl1.Clear()
        Dim boldFont As Font = New Font(Me.RichTextBoxCtrl1.Font, FontStyle.Bold)
        Dim regularFont As Font = New Font(Me.RichTextBoxCtrl1.Font, FontStyle.Regular)
        Me.RichTextBoxCtrl1.SelectionFont = boldFont
        If Me.MonthCalendar1.SelectionRange.Start = Me.MonthCalendar1.SelectionRange.End Then
            Me.RichTextBoxCtrl1.AppendText("Events For  " & Me.MonthCalendar1.SelectionRange.Start.ToShortDateString)
        Else
            Me.RichTextBoxCtrl1.AppendText("Events For  " & Me.MonthCalendar1.SelectionRange.Start.ToShortDateString)
            Me.RichTextBoxCtrl1.AppendText(" - " & Me.MonthCalendar1.SelectionRange.End.ToShortDateString)
        End If
        Me.RichTextBoxCtrl1.AppendText(Environment.NewLine)
        Me.RichTextBoxCtrl1.AppendText(Environment.NewLine)
        Me.RichTextBoxCtrl1.SelectionFont = regularFont
        Me.SetSelectedEvents(Me.MonthCalendar1.SelectionRange)
        Dim ef As EventFilter = CType(Me.EventFilterComboBox.SelectedItem, EventFilter)
        For Each dr As ClockDBDataSet.EventsTBRow In Me._selectedEvents
            Dim et As EventType = CType([Enum].Parse(GetType(EventType), dr.Action), EventType)
            If ef = et OrElse ef = EventFilter.All Then
                Dim at As AlarmType = CType([Enum].Parse(GetType(AlarmType), dr.Alarm), AlarmType)
                Me.RichTextBoxCtrl1.SelectionFont = boldFont
                Me.RichTextBoxCtrl1.AppendText(dr.Action)
                Me.RichTextBoxCtrl1.AppendText(" " & dr.Alarm)
                Me.RichTextBoxCtrl1.SelectionFont = regularFont
                If at = AlarmType.Once Then
                    Me.RichTextBoxCtrl1.AppendText(" for  " & dr.OnceDate.ToShortDateString & " " & dr.Time.ToShortTimeString)
                ElseIf at = AlarmType.AfterStart Then
                    Me.RichTextBoxCtrl1.AppendText(" within  " & dr.Hours & _
                                                   Globalization.CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator & dr.Minutes)
                Else
                    Me.RichTextBoxCtrl1.AppendText(" at  " & dr.Time.ToShortTimeString)
                End If
                Me.RichTextBoxCtrl1.AppendText(Environment.NewLine)
                If dr.Information <> String.Empty Then
                    Me.RichTextBoxCtrl1.AppendText(dr.Information)
                Else
                    Me.RichTextBoxCtrl1.AppendText("Nothing")
                End If
                Me.RichTextBoxCtrl1.AppendText(Environment.NewLine)
                Me.RichTextBoxCtrl1.AppendText(Environment.NewLine)
            End If
        Next
        boldFont.Dispose()
        regularFont.Dispose()
    End Sub

    Public Sub SetSelectedEvents(ByVal sr As SelectionRange)
        Me._selectedEvents.Clear()
        For Each dr As ClockDBDataSet.EventsTBRow In Me._scheduledEvents
            Dim d As Date = sr.Start
            While d <= sr.End
                If Not Me._selectedEvents.Contains(dr) Then
                    Dim at As AlarmType = CType([Enum].Parse(GetType(AlarmType), dr.Alarm), AlarmType)
                    Select Case at
                        Case AlarmType.Daily
                            Me._selectedEvents.Add(dr)
                        Case AlarmType.Once
                            If dr.OnceDate.Date = d.Date Then
                                Me._selectedEvents.Add(dr)
                            End If
                        Case AlarmType.AfterStart
                            Me._selectedEvents.Add(dr)
                        Case AlarmType.Weekends
                            If (d.DayOfWeek = DayOfWeek.Saturday OrElse d.DayOfWeek = DayOfWeek.Sunday) Then
                                Me._selectedEvents.Add(dr)
                            End If
                        Case Else
                            If (at = d.DayOfWeek) Then
                                Me._selectedEvents.Add(dr)
                            End If
                    End Select
                Else
                    Exit While
                End If
                d = d.AddDays(1)
            End While
        Next
    End Sub

#End Region

#Region " Events "

    Private Sub CalendarForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me._scheduledEvents.Clear()
    End Sub

    Private Sub CalendarForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mergin As Integer = 18
        Me.MonthCalendar1.Location = New Point(mergin, mergin)
        Me.EventsGroupBox.Location = New Point(mergin, Me.MonthCalendar1.Height + 2 * mergin)
        Me.ClientSize = New Size(Me.MonthCalendar1.Width + 2 * mergin, Me.MonthCalendar1.Height + Me.EventsGroupBox.Height + 3 * mergin)
        For Each ef As EventFilter In [Enum].GetValues(GetType(EventFilter))
            Me.EventFilterComboBox.Items.Add(ef)
        Next
        Me.EventFilterComboBox.SelectedIndex = 2
        Me._scheduledEvents.Clear()
        For Each dr As ClockDBDataSet.EventsTBRow In My.Forms.ClockForm.ClockDBDataSet.EventsTB.Rows
            Me._scheduledEvents.Add(dr)
        Next
        Me._scheduledEvents.Sort(New SpecialComparer)
        Me.RichTextBoxCtrl1.Clear()
        Me.MonthCalendar1.TodayDate = My.Forms.ClockForm.Clock1.Value.Date
        Me.MonthCalendar1.SetDate(Me.MonthCalendar1.TodayDate)
        Me.MonthCalendar1.BoldedDates = GetAllDates()

        Me.PrintButton.Enabled = (Printing.PrinterSettings.InstalledPrinters.Count > 0)
        Me.PrintPreviewButton.Enabled = Me.PrintButton.Enabled
        Me.PageSetupButton.Enabled = Me.PrintButton.Enabled
        If Me.PrintDocument1.PrinterSettings.PrinterName = String.Empty Then
            Dim ps As New System.Drawing.Printing.PrinterSettings
            Me.PrintDocument1.PrinterSettings.PrinterName = ps.PrinterName
        End If
    End Sub

    Private Sub CalendarForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Me.PrintEvents()
    End Sub

    Private Sub MonthCalendar1_DateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
        Me.PrintEvents()
    End Sub

    Private Sub SaveAsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsButton.Click
        Me.SaveFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        Me.SaveFileDialog1.FileName = "Scheduled Events"
        Me.SaveFileDialog1.Title = "Save As"
        If Me.SaveFileDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            If Me.SaveFileDialog1.FilterIndex = 1 Then
                Me.RichTextBoxCtrl1.SaveFile(Me.SaveFileDialog1.FileName, RichTextBoxStreamType.RichText)
            Else
                Me.RichTextBoxCtrl1.SaveFile(Me.SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
            End If
        End If
    End Sub

    Private Sub CopyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyButton.Click
        Me.RichTextBoxCtrl1.Copy()
    End Sub

    Private Sub RichTextBoxCtrl1_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs) Handles RichTextBoxCtrl1.LinkClicked
        ProcessData.Start(e.LinkText)
    End Sub

    Private Sub RichTextBoxCtrl1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBoxCtrl1.SelectionChanged
        Me.CopyButton.Enabled = Me.RichTextBoxCtrl1.SelectedText.Trim.Length > 0
    End Sub

    Private Sub RichTextBoxCtrl1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBoxCtrl1.TextChanged
        Me.SaveAsButton.Enabled = Me.RichTextBoxCtrl1.Text.Trim.Length > 0
        Me.PrintButton.Enabled = Me.SaveAsButton.Enabled
        Me.PrintPreviewButton.Enabled = Me.SaveAsButton.Enabled
    End Sub

    Private Sub CopyMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyMenuItem.Click
        Me.RichTextBoxCtrl1.Focus()
        Me.RichTextBoxCtrl1.Copy()
    End Sub

    Private Sub SelectAllMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllMenuItem.Click
        Me.RichTextBoxCtrl1.Focus()
        Me.RichTextBoxCtrl1.SelectAll()
    End Sub

    Private Sub ContextMenuStrip1_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
        Me.CopyMenuItem.Enabled = Me.RichTextBoxCtrl1.SelectedText.Trim.Length > 0
        Me.SelectAllMenuItem.Enabled = Me.RichTextBoxCtrl1.Text.Trim.Length > 0
    End Sub

    Private Sub GoToTodayMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoToTodayMenuItem.Click
        Me.MonthCalendar1.SetDate(Me.MonthCalendar1.TodayDate)
    End Sub

    Private Sub AddEventsMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddEventsMenuItem.Click
        My.Forms.EventsForm.SelectionRange = Me.MonthCalendar1.SelectionRange
        My.Forms.EventsForm.View = ViewTypes.Add
        If Me.MonthCalendar1.SelectionRange.Start <> Me.MonthCalendar1.SelectionRange.End Then
            Dim dr As DialogResult = MessageBox.Show( _
            "Would you like to set the events with the same values?", _
            "Desktop Alarm Clock", MessageBoxButtons.YesNoCancel, _
            MessageBoxIcon.Question)
            If dr = Windows.Forms.DialogResult.Cancel Then
                Me.DialogResult = Windows.Forms.DialogResult.None
            Else
                Me.DialogResult = dr
            End If
        Else
            Me.DialogResult = Windows.Forms.DialogResult.No
        End If
    End Sub

    Private Sub ViewEditEventsMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewEditEventsMenuItem.Click
        My.Forms.EventsForm.SelectedEvents.Clear()
        For Each dr As ClockDBDataSet.EventsTBRow In Me._selectedEvents
            My.Forms.EventsForm.SelectedEvents.Add(dr)
        Next
        My.Forms.EventsForm.View = ViewTypes.ViewOrEdit
        Me.DialogResult = Windows.Forms.DialogResult.No
    End Sub

    Private Sub CalendarContextMenu_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles CalendarContextMenu.Opening
        Me.ViewEditEventsMenuItem.Enabled = Me._selectedEvents.Count > 0
    End Sub

    Private Sub EventFilterComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles EventFilterComboBox.SelectedIndexChanged
        Me.PrintEvents()
    End Sub

#Region " Print Document "

    Dim _fromIndex As Integer
    Dim _toIndex As Integer

    Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
        If Me.PrintDocument1.PrinterSettings.PrintRange = Printing.PrintRange.Selection Then
            Me._fromIndex = Me.RichTextBoxCtrl1.SelectionStart
            Me._toIndex = Me.RichTextBoxCtrl1.SelectionStart + Me.RichTextBoxCtrl1.SelectionLength
        Else
            Me._fromIndex = 0
            Me._toIndex = Me.RichTextBoxCtrl1.TextLength
        End If
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        ' Print the content of the RichTextBox. Store the last character printed.
        Me._fromIndex = Me.RichTextBoxCtrl1.Print(Me._fromIndex, Me._toIndex, e)
        ' Look for more pages
        If Me._fromIndex < Me._toIndex Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub

    Private Sub PageSetupButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetupButton.Click
        If Me.PageSetupDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            Me.RichTextBoxCtrl1.RightMargin = CInt(Me.PageSetupDialog1.PageSettings.PrintableArea.Width) _
            - (Me.PageSetupDialog1.PageSettings.Margins.Left + Me.PageSetupDialog1.PageSettings.Margins.Right)
        End If
    End Sub

    Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
        If Me.PrintDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            Me.PrintDocument1.Print()
        End If
    End Sub

    Private Sub PrintPreviewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewButton.Click
        Me.PrintPreviewDialog1.ShowDialog(Me)
    End Sub

#End Region

#End Region

#Region " Row Comparer "

    Private Class SpecialComparer
        Implements IComparer(Of ClockDBDataSet.EventsTBRow)

        Public Function Compare( _
        ByVal row1 As ClockDBDataSet.EventsTBRow, _
        ByVal row2 As ClockDBDataSet.EventsTBRow) As Integer _
        Implements System.Collections.Generic.IComparer(Of ClockDBDataSet.EventsTBRow).Compare
            Dim result As Integer = row1.OnceDate.CompareTo(row2.OnceDate)
            If result = 0 Then
                result = row1.Time.CompareTo(row2.Time)
            End If
            Return result
        End Function
    End Class

#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) ZipEdTech
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions