JayBeeOH

MonthCalendar Demo

Aug 12th, 2016
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.04 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Date Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   12 AUG 2016
  16. '
  17. ' Description:    Show how to use the MonthCalendar control.
  18. '
  19. '                 Documentation is at:
  20. '                   App's Visual Basic .NET code is at http://pastebin.com/dFaqW4ke
  21. '
  22. '-------------------------------------------------------------------------------------------
  23.  
  24. Imports System.IO
  25. Imports System.Security
  26.  
  27. Public Class MainForm
  28.  
  29.     Private sunday, lastMonth As Date
  30.  
  31.     Enum DateSelection
  32.         Today = 0
  33.         Yesterday
  34.         Tomorrow
  35.         This_Week
  36.         Last_Week
  37.         Next_Week
  38.         This_Month
  39.         Last_Month
  40.         Next_Month
  41.     End Enum
  42.  
  43.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  44.         Handles MyBase.Load
  45.  
  46.         ' Load Date Selection ComboBox
  47.         With DateSelectionComboBox
  48.             .DataSource = [Enum].GetValues(GetType(DateSelection))
  49.             .SelectedIndex = DateSelection.Today
  50.             .AutoCompleteMode = AutoCompleteMode.Suggest
  51.             .AutoCompleteSource = AutoCompleteSource.ListItems
  52.         End With
  53.  
  54.         ' Display date labels
  55.         TodayLabel.Text = Today.ToShortDateString
  56.         YesterdayLabel.Text = Today.AddDays(-1).ToShortDateString
  57.         TomorrowLabel.Text = Today.AddDays(1).ToShortDateString
  58.  
  59.         sunday = Today.AddDays(Today.DayOfWeek * -1)
  60.         ThisWeekLabel.Text = sunday.ToShortDateString & " - " &
  61.                             sunday.AddDays(6).ToShortDateString
  62.         LastWeekLabel.Text = sunday.AddDays(-7).ToShortDateString & " - " &
  63.                             sunday.AddDays(-1).ToShortDateString
  64.         NextWeekLabel.Text = sunday.AddDays(7).ToShortDateString & " - " &
  65.                             sunday.AddDays(13).ToShortDateString
  66.  
  67.         ' Not perfect, but close
  68.         lastMonth = Today.AddMonths(-1).AddDays((Today.Day * -1) + 1)
  69.         ThisMonthLabel.Text = lastMonth.AddMonths(1).ToShortDateString & " - " &
  70.                             lastMonth.AddMonths(2).AddDays(-1)
  71.         LastMonthLabel.Text = lastMonth.ToShortDateString & " - " &
  72.                             lastMonth.AddMonths(1).AddDays(-1).ToShortDateString
  73.         NextMonthLabel.Text = lastMonth.AddMonths(2).ToShortDateString & " - " &
  74.                             lastMonth.AddMonths(3).AddDays(-1).ToShortDateString
  75.  
  76.         ' Initialize MonthCalendar control.
  77.         MonthCalendar1.MaxDate = Today.AddYears(5)
  78.         MonthCalendar1.MinDate = Today.AddYears(-80)
  79.         GetDates("AnnualDates.txt", True)
  80.         GetDates("BoldDates.txt", False)
  81.     End Sub
  82.  
  83.     Private Sub DateSelectionComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) _
  84.         Handles DateSelectionComboBox.SelectedIndexChanged
  85.  
  86.         If DateSelectionComboBox.SelectedIndex > -1 Then
  87.  
  88.             Dim BoldFont As New Font(Me.Font, FontStyle.Bold)
  89.             MonthCalendar1.MaxSelectionCount = 7
  90.  
  91.             Select Case CType(DateSelectionComboBox.SelectedItem, Integer)
  92.                 Case DateSelection.Today
  93.                     MonthCalendar1.SelectionRange = New SelectionRange(Today.Date, Today.Date)
  94.  
  95.                     TodayLabel.Font = BoldFont
  96.                     TodaysLabel.Font = BoldFont
  97.                 Case DateSelection.Yesterday
  98.                     MonthCalendar1.SelectionRange = New SelectionRange(Today.AddDays(-1).Date,
  99.                                                                        Today.AddDays(-1).Date)
  100.                     YesterdayLabel.Font = BoldFont
  101.                     YesterdaysLabel.Font = BoldFont
  102.                 Case DateSelection.Tomorrow
  103.                     MonthCalendar1.SelectionRange = New SelectionRange(Today.AddDays(1).Date,
  104.                                                                        Today.AddDays(1).Date)
  105.                     TomorrowLabel.Font = BoldFont
  106.                     TomorrowsLabel.Font = BoldFont
  107.                 Case DateSelection.This_Week
  108.                     MonthCalendar1.SelectionRange = New SelectionRange(sunday.Date,
  109.                                                                        sunday.AddDays(6).Date)
  110.                     ThisWeekLabel.Font = BoldFont
  111.                     ThisWeeksLabel.Font = BoldFont
  112.                 Case DateSelection.Last_Week
  113.                     MonthCalendar1.SelectionRange = New SelectionRange(sunday.AddDays(-7).Date,
  114.                                                                        sunday.AddDays(-1).Date)
  115.                     LastWeekLabel.Font = BoldFont
  116.                     LastWeeksLabel.Font = BoldFont
  117.                 Case DateSelection.Next_Week
  118.                     MonthCalendar1.SelectionRange = New SelectionRange(sunday.AddDays(7).Date,
  119.                                                                        sunday.AddDays(13).Date)
  120.                     NextWeekLabel.Font = BoldFont
  121.                     NextWeeksLabel.Font = BoldFont
  122.                 Case DateSelection.This_Month
  123.                     MonthCalendar1.MaxSelectionCount = 31
  124.                     MonthCalendar1.SelectionRange = New SelectionRange(lastMonth.AddMonths(1).Date,
  125.                                                                        lastMonth.AddMonths(2).AddDays(-1).Date)
  126.                     ThisMonthLabel.Font = BoldFont
  127.                     ThisMonthsLabel.Font = BoldFont
  128.                 Case DateSelection.Last_Month
  129.                     MonthCalendar1.MaxSelectionCount = 31
  130.                     MonthCalendar1.SelectionRange = New SelectionRange(lastMonth.Date,
  131.                                                                        lastMonth.AddMonths(1).AddDays(-1).Date)
  132.                     LastMonthLabel.Font = BoldFont
  133.                     LastMonthsLabel.Font = BoldFont
  134.                 Case DateSelection.Next_Month
  135.                     MonthCalendar1.MaxSelectionCount = 31
  136.                     MonthCalendar1.SelectionRange = New SelectionRange(lastMonth.AddMonths(2).Date,
  137.                                                                        lastMonth.AddMonths(3).AddDays(-1).Date)
  138.                     NextMonthLabel.Font = BoldFont
  139.                     NextMonthsLabel.Font = BoldFont
  140.             End Select
  141.  
  142.         End If
  143.     End Sub
  144.  
  145.     Private Sub MonthCalendar1_DateChanged(sender As Object, e As DateRangeEventArgs) _
  146.         Handles MonthCalendar1.DateChanged
  147.  
  148.         For Each ctrl As Control In Me.Controls
  149.             If TypeOf ctrl Is Label Then
  150.                 ctrl.Font = New Font(ctrl.Font, FontStyle.Regular)
  151.             End If
  152.         Next
  153.  
  154.         If MonthCalendar1.SelectionStart.Date = Today.Date AndAlso MonthCalendar1.SelectionEnd.Date = Today.Date Then
  155.             DateSelectionComboBox.SelectedIndex = DateSelection.Today
  156.         End If
  157.     End Sub
  158.  
  159.     Private Sub MonthCalendar1_DateSelected(sender As Object, e As DateRangeEventArgs) _
  160.         Handles MonthCalendar1.DateSelected
  161.  
  162.         If MonthCalendar1.SelectionStart.Date = Today.Date AndAlso MonthCalendar1.SelectionEnd.Date = Today.Date Then
  163.             DateSelectionComboBox.SelectedIndex = DateSelection.Today
  164.         Else
  165.             DateSelectionComboBox.SelectedIndex = -1
  166.         End If
  167.     End Sub
  168.  
  169.     Private Sub GetDates(ByVal file As String, ByVal isAnnual As Boolean)
  170.  
  171.         Try
  172.             Using sr As New StreamReader(file)
  173.                 Do While Not sr.EndOfStream
  174.                     Dim line As String
  175.                     line = sr.ReadLine.Trim
  176.                     If isAnnual Then
  177.                         MonthCalendar1.AddAnnuallyBoldedDate(DateTime.Parse(line))
  178.                     Else
  179.                         MonthCalendar1.AddBoldedDate(DateTime.Parse(line))
  180.                     End If
  181.                 Loop
  182.             End Using
  183.         Catch ex As IOException
  184.             MessageBox.Show(ex.Message, "File IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  185.         Catch ex As SecurityException
  186.             MessageBox.Show(ex.Message, "File Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  187.         Catch ex As Exception
  188.             MessageBox.Show("There was a problem opening the file." &
  189.                             ControlChars.NewLine & ex.Message,
  190.                            "Error Opening File",
  191.                            MessageBoxButtons.OK,
  192.                            MessageBoxIcon.Error)
  193.         End Try
  194.     End Sub
  195.  
  196. End Class
Advertisement
Add Comment
Please, Sign In to add comment