I recently came across a requirement on Month calendar control from a project. The req. is as follows:
: I used MouseDown and HitTest method to solve this. I removed DateChanged event to call WS method. I placed WS call to get list of dates to bold in MouseDown event.
Find blelow code for your reference.
Note: Add MouseDown and DateSelected events to MonthCalendar control as shown below
/// <summary>
/// This is the actual WS call. I have not included WS call here. Assume, this method makes WS call and returns you back
/// list of dates to bold
/// </summary>
/// <returns></returns>
private DateTime[] GetDatesToBold(int monthNo)
{
DateTime[] d = new DateTime[10];
d[0] = new DateTime(2008, monthNo, 5);
d[1] = new DateTime(2008, monthNo, 6);
d[2] = new DateTime(2008, monthNo, 7);
d[3] = new DateTime(2008, monthNo, 8);
d[4] = new DateTime(2008, monthNo, 9);
d[5] = new DateTime(2008, monthNo, 10);
d[6] = new DateTime(2008, monthNo, 11);
d[7] = new DateTime(2008, monthNo, 12);
d[8] = new DateTime(2008, monthNo, 13);
d[9] = new DateTime(2008, monthNo, 14);
return (d);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void monthCalendarDeliveryDates_MouseDown(object sender, MouseEventArgs e)
{
Point p = e.Location;
DateTime[] boldDates;
MonthCalendar.HitTestInfo h = this.monthCalendarDeliveryDates.HitTest(p);
if (h.HitArea == MonthCalendar.HitArea.NextMonthButton ||
h.HitArea == MonthCalendar.HitArea.PrevMonthButton)
{
boldDates = GetDatesToBold(monthCalendarDeliveryDates.SelectionStart.Month);
SetBoldedDates(boldDates);
}
}
/// <summary>
/// This method sets all the requested dates to be in bold
/// </summary>
/// <param name="bDates"></param>
private void SetBoldedDates(DateTime[] bDates)
{
this.monthCalendarDeliveryDates.BoldedDates = bDates;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
textBoxSelectedDate.Text = e.Start.ToShortDateString();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonShow_Click(object sender, EventArgs e)
{
if(textBoxSelectedDate.Text != string.Empty)
MessageBox.Show(textBoxSelectedDate.Text);
}