Quantcast
Channel: AuthorCode » Windows Controls
Viewing all articles
Browse latest Browse all 10

How to Set the Minimum And Maximum Dates at run time in MonthCalendar control

$
0
0

Some time users need to select date within some specific date-range, for it developer can set the minimum and maximum date for the MonthCalendar control such as if user want to select date between last month’s dates than developer should set the first date of the last month as min date of the Monthcalendar control and last date of the last month as maximum date this enables user to select date between the last month date ranges. In this article i am describing you how we can set the min date and max date for Month Calendar control.

MinDate property of the Month Calendar

The MinDate represent the minimum allowable date and default date value is 01/01/1753.

if you will set the Mindate earlier than 01/01/1753,the code will give an System.ArgumentException.

MaxDate property of the Month Calendar

The MaxDate represent the maximum allowable date and default date value is 12/31/9998.
if you will set the Maxdate greater than 12/31/9998,the code will give an System.ArgumentException.

Example:

  1.   Private Sub Form3_Load(ByVal sender As System.Object, _
  2.                          ByVal e As System.EventArgs) Handles MyBase.Load
  3.         MonthCalendar1.MinDate = #1/1/2012#
  4.         MonthCalendar1.MaxDate = #6/30/2012#
  5.     End Sub

Another Example to set the first date and last date of the previous month:

  1.     Private Sub Form3_Load(ByVal sender As System.Object, _
  2.                          ByVal e As System.EventArgs) Handles MyBase.Load
  3.  
  4.         Dim firstDay_PrevMonth As DateTime
  5.         Dim lastDay_PrevMonth As DateTime
  6.         Dim Current_Month As New DateTime(DateTime.Today.Year, _
  7.                              DateTime.Today.Month, 1)
  8.  
  9.         firstDay_PrevMonth = Current_Month.AddMonths(-1)
  10.         lastDay_PrevMonth = Current_Month.AddDays(-1)
  11.  
  12.         MonthCalendar1.MinDate = firstDay_PrevMonth
  13.         MonthCalendar1.MaxDate = lastDay_PrevMonth
  14.     End Sub

 

Thanks


Viewing all articles
Browse latest Browse all 10

Trending Articles