TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# MonthCalendar Control: Windows Forms

Use 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.
Start. To get started, you can double-click on the MonthCalendar icon in the Toolbox pane in Visual Studio. A MonthCalendar with all its defaults is inserted into your form. Next, you can adjust the properties of the MonthCalendar.Property

Usually: You can begin referencing the MonthControl in your program's C# code through the identifier monthControl1.

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.
Date properties: MinDate: 1/1/1753 MaxDate: 12/31/9998
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.
Properties: BackColor ForeColor TitleBackColor TitleForeColor TrailingForeColor
BoldedDates. MonthCalendar has several properties related to bolded dates. In the MonthCalendar, some dates can be bolded to indicate an important event. These properties include AnnuallyBoldedDates, MonthlyBoldedDates and BoldedDates.

Next: We look at a code example that demonstrates setting the BoldedDates property.

C# program that uses MonthCalendar and BoldedDates 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) }; } } }
Please note that you can use any array initialization syntax you choose to set the BoldedDates property. You could populate a List collection and then convert it to a DateTime array using the ToArray extension method.ListToArray
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.
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.

C# program that uses SelectionRange 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.

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.

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.

C# program that uses DateChanged 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()); } } }
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.

And: For programs where the calendar is important or key, this may not be sufficient. But in most cases this control is ideal.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf