Welcome to WindowsClient.net | Sign in | Join

radicaldesigns

Sponsors





  • advertise here

Archives

Month Calendar sample to highlight specific dates in bold, displays selected date in Textbox using HitTest and MouseDown

I recently came across a requirement on Month calendar control from a project.  The req. is as follows:

  1. The Month Calendar retrieves dates to be bolded from Web Service.[The actual req. was to disable some dates..but I could not.]
  2. The Month Calendar should display these dates in bold.
  3. The end user can select a bolded date.
  4. The system displays the selected date in Text Box.
  5. The end user can select a different month to get list of dates to be in bold from Web Service.
  6. Repeat step 1 to step 5.

Problem faced: I tried this with Month Calendar's DateSelected and DateChanged event.  I could not select a bolded date and display the same in the text box as DateChanged event gets triggered.  Also when I navigate between months, the Calendar control does not position itself to selected month.  Rather it started to shift successive months recursively.

Solution found: 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
this.monthCalendarDeliveryDates.MouseDown += new System.Windows.Forms.MouseEventHandler(this.monthCalendarDeliveryDates_MouseDown);

this.monthCalendarDeliveryDates.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateSelected);

/// <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);
}       

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Page view counter