Home
Search
C# MonthCalendar Control: Windows FormsUse the MonthCalendar control in Windows Forms to display months and days for user selection.
MonthCalendar is a selectable calendar widget. On the MonthCalendar, a user can select a day, or a range of days. The user can also scroll through the months. This control provides many useful options. It is ideal for instant calendars.
BoldedDates. To get started, you can double-click on the MonthCalendar icon in the Toolbox pane in Visual Studio. MonthCalendar has several properties related to bolded dates. In the MonthCalendar, some dates can be bolded to indicate an important event.
Next We look at a code example that demonstrates setting the BoldedDates property.
MaxDate, MinDate. The MonthControl provides two important properties of the calendar called MaxDate and MinDate. These indicate the maximum and minimum selectable dates. These dates give you a lot of range to select dates.
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) }; } } }
Color properties. 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 dynamically change the colors.
Date properties:
MinDate: 1/1/1753 MaxDate: 12/31/9998
SelectionRange. The SelectionRange 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. These are the start and end dates.
Also You can assign to all of these properties. This will change the currently selected square on the MonthCalendar.
Properties:
BackColor ForeColor TitleBackColor TitleForeColor TrailingForeColor
DateChanged. 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 allows you to detect whenever the user changes the date to something else.
Note When this event handler executes, you can detect the SelectionRange. You can also access the properties on the DateRangeEventArgs.
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; } } }
ShowToday. There are two properties 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, it will not be present at the bottom of the calendar.
ShowTodayCircle The ShowTodayCircle property adjusts the visibility of the box on the left of the "Today" display.
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. MessageBox.Show("DateChanged: " + monthCalendar1.SelectionRange.ToString()); } } }
ShowWeekNumbers. 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.
Here 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.
CalendarDimensions. It 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. I adjusted the CalendarDimensions to 1, 2.
Summary. You could implement a custom calendar widget in the C# language. But 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.
Home
© 2007-2022 sam allen.
see site info on the changelog.