Q

How do I write a calendar control that allows specification of start/end date from the database?

I want to write my own calendar control like where I will specify start date and end date from database. Is this possible? If yes, can you send me code?
The procedure basically involves inheriting from Calendar and attaching to the SelectionChanged and VisibleMonthChanged events, where you can compare the new values with your DB-valid dates and set the VisibleDate/SelectedDate properties accordingly. You can also catch the DayRender and make the invalid dates unselectable, such as:
public class MyCalendar : Calendar
{
 //Get the dates from a db connection.
 DateTime _last = new DateTime(2003, 7, 30); 
 DateTime _first = new DateTime(2003, 4, 1);

 public MyCalendar()
 {
  base.SelectionChanged += new EventHandler(OnDateChange);
  base.VisibleMonthChanged += new MonthChangedEventHandler
(OnMonthChanged);
base.DayRender += new DayRenderEventHandler(OnDayRender);
 }

 private void OnDateChange(object sender, EventArgs e)
 {
  //Check valid date or show last/first valid ones.
  if (SelectedDate > _last)
  {
   SelectedDate = _last;
  }
  else if (SelectedDate < _first)
  {
   SelectedDate = _first;
  }
 }

 private void OnMonthChanged(object sender, MonthChangedEventArgs e)
 {
  //Check valid month or show last/first valid ones.
  if (e.NewDate > _last || e.NewDate < _first)
  {
   VisibleDate = e.PreviousDate;
  }
 }

 private void OnDayRender(object sender, DayRenderEventArgs e)
 {
  if (e.Day.Date > _last || e.Day.Date < _first)
  {
   e.Day.IsSelectable = false;
  }
 }
}

This was last published in June 2003

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close