/// <summary>
/// Occurs when the Date of the Calendar changes
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void monthCalendarDate_DateChanged(object sender, System.Windows.Forms.DateRangeEventArgs e)
{
if ( this.acceptOnlyBoldedDates == false )
{
this.dateValue = this.monthCalendarDate.SelectionStart.ToShortDateString();
this.textBoxDate.Text = this.dateValue ;
this.monthCalendarDate.Visible = false ;
}
else
{
bool setDateTime = false ;
DateTime selectedDateTime= this.monthCalendarDate.SelectionStart;
foreach(DateTime dt in this.monthCalendarDate.AnnuallyBoldedDates)
{
if ( ( dt.Date.Day == selectedDateTime.Date.Day ) &&( dt.Date.Month == selectedDateTime.Date.Month ))
{
this.dateValue = this.monthCalendarDate.SelectionStart.ToShortDateString();
this.textBoxDate.Text = this.dateValue ;
this.monthCalendarDate.Visible = false ;
setDateTime = true ;
break;
}
}
if (!setDateTime)
{
this.dateValue = "";
this.textBoxDate.Text = this.dateValue ;
this.monthCalendarDate.Visible = false ;
}
}
}
/// <summary>
/// Occurs when the KeyUp Event of the TextBoxDate occurs
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBoxDate_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
this.dateValue = "";
}
}
/// <summary>
/// Occurs on click event of the buttonDate Control
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDate_Click(object sender, System.EventArgs e)
{
this.monthCalendarDate.Visible = true;
}
/// <summary>
/// Occurs on Calendar Load event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Calendar_Load(object sender, System.EventArgs e)
{
this.monthCalendarDate.Visible = false ;
}
/// <summary>
/// Occurs on TextBox Leave Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBoxDate_Leave(object sender, System.EventArgs e)
{
try
{
DateTime dt = Convert.ToDateTime(this.textBoxDate.Text);
}
catch(Exception ex)
{
this.dateValue = "";
this.textBoxDate.Text = "";
}
}
/// <summary>
/// public method exposed to set the annuallyboldedDates
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void SetAnnuallyBoldedDates(DateTime [] dates)
{
this.monthCalendarDate.AnnuallyBoldedDates = dates ;
}
Step 6:
Add to Solution a Test Project. Add a reference to the Calendar Library Project in the References section. Rename the form appropriately and drag and drop the Control from the Toolbox on the Form. On the load event of the Form set the bolded dates as follows:
this.calendar.SetAnnuallyBoldedDates(
new System.DateTime[] {
new System.DateTime(2005, 4, 20, 0, 0, 0, 0),
new System.DateTime(2005, 4, 28, 0, 0, 0, 0),
new System.DateTime(2005, 5, 5, 0, 0, 0, 0),
new System.DateTime(2005, 7, 4, 0, 0, 0, 0),
new System.DateTime(2005, 12, 15, 0, 0, 0, 0),
new System.DateTime(2005, 12, 18, 0, 0, 0, 0)});
In the Design View set the acceptOnlyBoldedDates property of the Calendar Control to true.
Step 7:
Set the Test Project as the startup project and execute the project. Since the acceptOnlyBoldedDates property was set to true, the control accepts only Dates that are in the Bolded Date Collection.
Conclusion
Thus we have successfully created a Control that has all the capabilities of the existing DateTimePicker enhanced along with additional capabilities imparted from that of MonthCalendar Control.