MonthCalendar is a selectable calendar widget. On the MonthCalendar, a user can select a day, or a range of days, and can also scroll through the months. This control provides many useful options. It is ideal for instant calendars.


To get started, you can double-click on the MonthCalendar icon in the Toolbox pane in Visual Studio. You will see that a MonthCalendar with all its defaults is inserted into your form. Next, you can adjust the properties of the MonthCalendar; these properties are described in the following sections of this article. Usually, you can begin referencing the MonthControl in your program's C# code through the identifier monthControl1.
The MonthControl provides two important properties of the calendar called MaxDate and MinDate. These indicate the maximum and minimum selectable dates. By default, the MinDate is 1/1/1753, which was a long time ago. The default MaxDate is 12/31/9998, which is obviously a time in the far distant future.

Another set of properties you can edit on the MonthCalendar are the color properties. These allow you to set your control to be in garish, ugly shades of puce, magenta and lavender. You can even change the colors dynamically, which can cause headaches and even worse neurological problems for your victims (I mean users). Some color properties include:
BackColor
ForeColor
TitleBackColor
TitleForeColor
TrailingForeColor
The MonthCalendar control has several properties related to bolded dates. In the MonthCalendar, some dates can be bolded to indicate an important event (such as a holiday). These properties include AnnuallyBoldedDates, which repeat each year; MonthlyBoldedDates, which repeat each month; and BoldedDates, which do not repeat—these are absolute and not recurrent. Next, we look at a code example that demonstrates setting the BoldedDates property.
Program that uses MonthCalendar and BoldedDates [C#]
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Set the BoldedDates property to a DateTime array.
// ... Use array initializer syntax to add tomorrow and two days after.
monthCalendar1.BoldedDates = new DateTime[]
{
DateTime.Today.AddDays(1),
DateTime.Today.AddDays(2),
DateTime.Today.AddDays(4)
};
}
}
}Array syntax. Also, please note that you can use any array initialization syntax you choose to set the BoldedDates property. You could, for example, populate a List collection and then convert it to a DateTime array using the ToArray extension method; this would probably be easier to construct in memory than a raw array.
List Examples ToArray Extension MethodIt is also possible to change the MonthCalendar so that it displays more than one month at a time in the visual area. You could, for example, display four months together, or just two. In the following screenshot, I adjusted the MonthControl so that its CalendarDimensions were equal to 1, 2.

One of the most important properties of the MonthCalendar is the SelectionRange property. This property, as well as the SelectionStart and SelectionEnd properties, allow you to get the dates that are selected in the form of two DateTime values: the start and end dates. Also, you can assign to all of these properties; this will change the currently selected square on the MonthCalendar.
Program that uses SelectionRange [C#]
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// You can acquire the range using this property accessor.
SelectionRange range = monthCalendar1.SelectionRange;
DateTime start = range.Start;
DateTime end = range.End;
// Alternatively, you can use the SelectionStart and End properties.
DateTime startB = monthCalendar1.SelectionStart;
DateTime endB = monthCalendar1.SelectionEnd;
}
}
}There are two properties on the MonthCalendar that allow you to change whether and how the "Today" text at the bottom of the calendar appears. ShowToday is by default set to true; if you set it to false, you will see that it is not present at the bottom of the calendar. The ShowTodayCircle property adjusts the visibility of the box on the left of the "Today" display. It will not hide the entire "Today" part if you set it to false.

In some applications, you may want users to be able to know what the number of the week is in the year they are looking at. For example, some offices might index years based on an integer, and this can be useful for the visual display to reinforce and improve correctness. In this screenshot, the ShowWeekNumbers property was set to true; the numbers 14, 15, 16, 17, 18, and 19 are the numbers of the weeks in the year.

The MonthCalendar provides an event-driven user interface and you can provide and hook up event handlers to execute code on user actions. The DateChanged event is particularly helpful: it allows you to detect whenever the user changes the date to something else. When this event handler executes, you can detect the SelectionRange; alternatively, you can access the properties on the DateRangeEventArgs provided in the formal parameter list.
Program that uses DateChanged [C#]
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
// The user changed the current selection.
// ... Bug him or her with an obnoxious message box.
// ... It will really help the world be a better place.
MessageBox.Show("DateChanged: " +
monthCalendar1.SelectionRange.ToString());
}
}
}
Although you could implement a custom calendar widget in the C# language, this would require a lot of time and effort. As a solution, the MonthCalendar control in Windows Forms provides a fast and easy-to-use calendar. For programs where the calendar is very important or key, then this may not be sufficient, but in most cases this control is ideal.
Windows Forms