Is it possible to set monthCalendar to show the current month and the previous 2 months?

WinForms (3.5) has a form with monthCalendar .

A calendar control has a 3-column calendarDimension per row. This means that it currently shows June, July, August 2010.

Is it possible for the calendar to show April, May, June 2010? My dataset has no future dates, so the date selection will be for current or older dates.

+4
source share
3 answers

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

 monthCalendar1.MaxDate = DateTime.Now; 
+5
source

If you set the current date for MaxDate MonthCalendar, the calendar of the month will only be displayed - and thus allow the choice of a date or earlier than the current date.

+2
source

To force the current month to the right, I used the idea of ​​Pavan, but I added a timer to reset MaxDate after opening it in the calendar control. Now I can scroll to 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(); } } } 
+1
source

Source: https://habr.com/ru/post/1312346/


All Articles