Introduction
Visual Studio 2003 ships in with DateTimePicker Control for selecting Date. But the DateTimePicker inherently has it own limitations. It does not allow the following:
- To edit Dates; it allows only selection of Dates.
- Null Dates
- Set a collection of Dates to be bolded.
The Custom Calendar Control described in this paper overcomes the above limitations of the DateTimePicker by enhancing the capabilities of the existing MonthCalendar Control provided by Visual Studio .Net.
The Solution
Visual Studio 2003 ships in with a Month Calendar Control which allows Dates to be bolded. We can enhance the Month Calendar by coupling it with a TextBox and a Button Control and overcome the limitations of the DateTimePicker Control.
Implementation
For enhancing the capabilities of the DateTimePicker Control, the following steps are required to be implemented:
Step 1:
Create a custom project of type Windows Control Library. Within the library add a new file of type Custom Control. We are in effect creating a new control altogether using the Textbox, Button and MonthCalendar Control
Step 2:
To the Custom Control add a TextBox and a Button Control name the textBoxDate and buttonDate respectively. Name the new Control as Calendar Control.
The general look and feel of the custom Calendar is as follows:

Step 3:
The functionality to be implemented requires controlling the visibility of the MonthCalendar Control depending on the clicking of the buttonDate Control. Once the Date is selected, the Month Calendar control should become invisible by default.
Also to support nullable dates, the textbox controls text is used to support blank values.
Declare the following variables in the Custom Control:
#Region "Variables"
' dateValue would contain the selected Date
Private dateValue As String
' acceptOnlyBoldedDates would contain the selected Date
Private acceptOnlyBoldedDates As Boolean
#End Region
The dateValue string would contain the selectedDate in string format, which allows Null Dates as well.
The acceptOnlyBoldedDates is a property that is exposed to the user of the Control. Setting this property to true would enable the validation check that the selected Date falls in one of the bolded Dates already set by the user programmatically.
If the acceptOnlyBoldedDates is set to false, any valid date that the user enters would be selected. When set to true, the date entered would necessarily should be one previously set bolded dates.
Step 4:
For setting the bolded Dates, the following function is exposed. The user can programmatically use the SetAnnuallyBoldedDates to set a list of Bolded Dates.
'<summary>
' public method exposed to set the annuallyboldedDates
</summary>
' <param name="sender"></param>
' <param name="e"></param>
Public Sub SetAnnuallyBoldedDates(ByVal dates As DateTime())Me.monthCalendarDate.AnnuallyBoldedDates = dates
End Sub
Step 5:
Declare the following event handlers in the Custom Calendar Class:
' <summary>''' Occurs when the Date of the Calendar changes
' </summary>
' <param name="sender"></param>
' <param name="e"></param>
Private Sub monthCalendarDate_DateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs)
If Me.acceptOnlyBoldedDates = False Then
Me.dateValue = Me.monthCalendarDate.SelectionStart.ToShortDateString()Me.textBoxDate.Text = Me.dateValue
Me.monthCalendarDate.Visible = False
Else
Dim setDateTime As Boolean = False
Dim selectedDateTime As DateTime= Me.monthCalendarDate.SelectionStart
For Each dt As DateTime In Me.monthCalendarDate.AnnuallyBoldedDates
If (dt.Date.Day = selectedDateTime.Date.Day) AndAlso (dt.Date.Month = selectedDateTime.Date.Month) Then
Me.dateValue = Me.monthCalendarDate.SelectionStart.ToShortDateString()
Me.textBoxDate.Text = Me.dateValue
Me.monthCalendarDate.Visible = False
setDateTime = True
Exit For
End If
Next dt
If (Not setDateTime) Then
Me.dateValue = ""
Me.textBoxDate.Text = Me.dateValue
Me.monthCalendarDate.Visible = False
End If
End If
End Sub
' <summary>
' Occurs when the KeyUp Event of the TextBoxDate occurs
' </summary>
' <param name="sender"></param>
' <param name="e"></param>