views:

159

answers:

3

In a WinForms (3.5) application there is form with a monthCalendar control.

The calendar control has a calendarDimension of 3 columns by 1 row. This means that it currently shows June, July, August of 2010.

Is it possible to have the calendar to show April, May, June of 2010 instead? My dataset doesn't have any future dates so the date selection will be for current or older dates.

+2  A: 

If you set the MonthCalendar's MaxDate to the current date, the month calendar will only show - and thus allow selection of - dates on or earlier than the current date.

Stuart Dunkeld
+3  A: 

You can use the following line of code to set the MonthCalendar's MaxDate property to current date in the form's load event.

monthCalendar1.MaxDate = DateTime.Now;
Pavan Navali
A: 

To force the current month to the right I used Pavan's idea, but I added a timer to reset MaxDate after opening on the calendar control. Now I can scroll into the future after loading the control.

public partial class Form1 : Form
{
   private DateTime _initialDateTime = DateTime.Now;

   public Form1()
   {
     InitializeComponent();
     // remember the default MAX date
     _initialDateTime = monthCalendar1.MaxDate;
     // set max date to NOW to force current month to right side
     monthCalendar1.MaxDate = DateTime.Now;
     // enable a timer to restore initial default date to enable scrolling into the future
     timer1.Start();
   }

   private void timer1_Tick(object sender, EventArgs e)
   {
     Timer timer = sender as Timer;
     if (timer != null)
     {
        // enable scrolling to the future
        monthCalendar1.MaxDate = _initialDateTime;
        // stop the timer...
        timer.Stop();
     }
   }
}
Zamboni