Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Date Demo
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 12 AUG 2016
- '
- ' Description: Show how to use the MonthCalendar control.
- '
- ' Documentation is at:
- ' App's Visual Basic .NET code is at http://pastebin.com/dFaqW4ke
- '
- '-------------------------------------------------------------------------------------------
- Imports System.IO
- Imports System.Security
- Public Class MainForm
- Private sunday, lastMonth As Date
- Enum DateSelection
- Today = 0
- Yesterday
- Tomorrow
- This_Week
- Last_Week
- Next_Week
- This_Month
- Last_Month
- Next_Month
- End Enum
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- ' Load Date Selection ComboBox
- With DateSelectionComboBox
- .DataSource = [Enum].GetValues(GetType(DateSelection))
- .SelectedIndex = DateSelection.Today
- .AutoCompleteMode = AutoCompleteMode.Suggest
- .AutoCompleteSource = AutoCompleteSource.ListItems
- End With
- ' Display date labels
- TodayLabel.Text = Today.ToShortDateString
- YesterdayLabel.Text = Today.AddDays(-1).ToShortDateString
- TomorrowLabel.Text = Today.AddDays(1).ToShortDateString
- sunday = Today.AddDays(Today.DayOfWeek * -1)
- ThisWeekLabel.Text = sunday.ToShortDateString & " - " &
- sunday.AddDays(6).ToShortDateString
- LastWeekLabel.Text = sunday.AddDays(-7).ToShortDateString & " - " &
- sunday.AddDays(-1).ToShortDateString
- NextWeekLabel.Text = sunday.AddDays(7).ToShortDateString & " - " &
- sunday.AddDays(13).ToShortDateString
- ' Not perfect, but close
- lastMonth = Today.AddMonths(-1).AddDays((Today.Day * -1) + 1)
- ThisMonthLabel.Text = lastMonth.AddMonths(1).ToShortDateString & " - " &
- lastMonth.AddMonths(2).AddDays(-1)
- LastMonthLabel.Text = lastMonth.ToShortDateString & " - " &
- lastMonth.AddMonths(1).AddDays(-1).ToShortDateString
- NextMonthLabel.Text = lastMonth.AddMonths(2).ToShortDateString & " - " &
- lastMonth.AddMonths(3).AddDays(-1).ToShortDateString
- ' Initialize MonthCalendar control.
- MonthCalendar1.MaxDate = Today.AddYears(5)
- MonthCalendar1.MinDate = Today.AddYears(-80)
- GetDates("AnnualDates.txt", True)
- GetDates("BoldDates.txt", False)
- End Sub
- Private Sub DateSelectionComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) _
- Handles DateSelectionComboBox.SelectedIndexChanged
- If DateSelectionComboBox.SelectedIndex > -1 Then
- Dim BoldFont As New Font(Me.Font, FontStyle.Bold)
- MonthCalendar1.MaxSelectionCount = 7
- Select Case CType(DateSelectionComboBox.SelectedItem, Integer)
- Case DateSelection.Today
- MonthCalendar1.SelectionRange = New SelectionRange(Today.Date, Today.Date)
- TodayLabel.Font = BoldFont
- TodaysLabel.Font = BoldFont
- Case DateSelection.Yesterday
- MonthCalendar1.SelectionRange = New SelectionRange(Today.AddDays(-1).Date,
- Today.AddDays(-1).Date)
- YesterdayLabel.Font = BoldFont
- YesterdaysLabel.Font = BoldFont
- Case DateSelection.Tomorrow
- MonthCalendar1.SelectionRange = New SelectionRange(Today.AddDays(1).Date,
- Today.AddDays(1).Date)
- TomorrowLabel.Font = BoldFont
- TomorrowsLabel.Font = BoldFont
- Case DateSelection.This_Week
- MonthCalendar1.SelectionRange = New SelectionRange(sunday.Date,
- sunday.AddDays(6).Date)
- ThisWeekLabel.Font = BoldFont
- ThisWeeksLabel.Font = BoldFont
- Case DateSelection.Last_Week
- MonthCalendar1.SelectionRange = New SelectionRange(sunday.AddDays(-7).Date,
- sunday.AddDays(-1).Date)
- LastWeekLabel.Font = BoldFont
- LastWeeksLabel.Font = BoldFont
- Case DateSelection.Next_Week
- MonthCalendar1.SelectionRange = New SelectionRange(sunday.AddDays(7).Date,
- sunday.AddDays(13).Date)
- NextWeekLabel.Font = BoldFont
- NextWeeksLabel.Font = BoldFont
- Case DateSelection.This_Month
- MonthCalendar1.MaxSelectionCount = 31
- MonthCalendar1.SelectionRange = New SelectionRange(lastMonth.AddMonths(1).Date,
- lastMonth.AddMonths(2).AddDays(-1).Date)
- ThisMonthLabel.Font = BoldFont
- ThisMonthsLabel.Font = BoldFont
- Case DateSelection.Last_Month
- MonthCalendar1.MaxSelectionCount = 31
- MonthCalendar1.SelectionRange = New SelectionRange(lastMonth.Date,
- lastMonth.AddMonths(1).AddDays(-1).Date)
- LastMonthLabel.Font = BoldFont
- LastMonthsLabel.Font = BoldFont
- Case DateSelection.Next_Month
- MonthCalendar1.MaxSelectionCount = 31
- MonthCalendar1.SelectionRange = New SelectionRange(lastMonth.AddMonths(2).Date,
- lastMonth.AddMonths(3).AddDays(-1).Date)
- NextMonthLabel.Font = BoldFont
- NextMonthsLabel.Font = BoldFont
- End Select
- End If
- End Sub
- Private Sub MonthCalendar1_DateChanged(sender As Object, e As DateRangeEventArgs) _
- Handles MonthCalendar1.DateChanged
- For Each ctrl As Control In Me.Controls
- If TypeOf ctrl Is Label Then
- ctrl.Font = New Font(ctrl.Font, FontStyle.Regular)
- End If
- Next
- If MonthCalendar1.SelectionStart.Date = Today.Date AndAlso MonthCalendar1.SelectionEnd.Date = Today.Date Then
- DateSelectionComboBox.SelectedIndex = DateSelection.Today
- End If
- End Sub
- Private Sub MonthCalendar1_DateSelected(sender As Object, e As DateRangeEventArgs) _
- Handles MonthCalendar1.DateSelected
- If MonthCalendar1.SelectionStart.Date = Today.Date AndAlso MonthCalendar1.SelectionEnd.Date = Today.Date Then
- DateSelectionComboBox.SelectedIndex = DateSelection.Today
- Else
- DateSelectionComboBox.SelectedIndex = -1
- End If
- End Sub
- Private Sub GetDates(ByVal file As String, ByVal isAnnual As Boolean)
- Try
- Using sr As New StreamReader(file)
- Do While Not sr.EndOfStream
- Dim line As String
- line = sr.ReadLine.Trim
- If isAnnual Then
- MonthCalendar1.AddAnnuallyBoldedDate(DateTime.Parse(line))
- Else
- MonthCalendar1.AddBoldedDate(DateTime.Parse(line))
- End If
- Loop
- End Using
- Catch ex As IOException
- MessageBox.Show(ex.Message, "File IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As SecurityException
- MessageBox.Show(ex.Message, "File Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As Exception
- MessageBox.Show("There was a problem opening the file." &
- ControlChars.NewLine & ex.Message,
- "Error Opening File",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- End Try
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment