Today, I'm going to uncover the details on the introduction to C# MonthCalendar Control. It is referred as a graphical interface that is widely used to modify and set date information based on your requirements.
Hello Everyone! I'm back to give you daily dose of information that resonates with your needs and requirements. Today, I'm going to uncover the details on the introduction to C# MonthCalendar Control. It is referred as a graphical interface that is widely used to modify and set date information based on your requirements. It is slightly different than DateTimePicker control in which you can select range of dates. DateTimePicker allows you to set both date and time, however, MonthCalendar control gives you a permission to select date only, but it gives you flexibility of selecting range of dates. Let's dive in and explore what this control does and what are its main applications.
C# MonthCalendar Control
C# MonthCalendar Control is known as graphical interface that is used to modify and select range of date information for required application.
In order to create C# MonthCalendar control, open the windows form application and go to Toolbar appearing on the left side.
Find the MonthCalendar control, drag and drop it over the Form.
You can play with it and move it around over the Form with the help of mouse.
There is another way of creating the MonthCalendar control. Just double click on the MonthCalendar control, it will automatically place the MonthCalendar control on the Form.
MonthCalendar Control Properties
In order to set the MonthCalendar control properties, just right click on the MonthCalendar control and go to properties.
Properties window will appear on the right side of the windows form application.
MonthCalendar control comes with different useful properties including, name, location, font, forecolor, back color, margin, MaximumDate, MaximumSize etc. Let’s discuss them one by one. Following window will appear as click on the properties.
Name
Name property defines the specific name of the control that is used to access it in the code. In the figure above, MonthCalendar control name is monthCalendar1.
BackColor and ForeColor
BackColor property is used to change the background color of the MonthCalendar control.
ForeColor is used to display text within a month.
SelectionRange and SelectionStart
SelectionRange is widely used property which defines the selected range of dates in the control.
SelectionStart property specifies the start date of the selected range of dates.
FirstDayOfWeek and ShowTodayCircle
FirstDayOfWeek property gives you an option to start week in the application with your preferred day. By default Sunday is selected as the start of the week and Saturday is considered as the last day of the week.
ShowTodayCircle property is used to set the circle around current date. By default the value of this property is set as true. You can set it to false if you want to remove the circle around the current date.
ShowDate, MinDate and MaxDate
ShowDate property displays the current date at the bottom of the calendar if its value is set as true. Setting the value to false will disappear the date at the bottom of the calendar.
The maximum and minimum time period in the control is set by using two properties MaxDate and MinDate. MaxDate determines the maximum valid date for the control.
MinDate determines the minimum valid date for the control.
The Visual Basic version we are using shows MaxDate as 12/31/9998 and MinDate as 1/1/1753
CalendarDimensions and TodayDate
CalendarDimensions determines the number of months in a single grid. Default dimension is set as (1,1) which will only display one month range in the grid.
Maximum 12 month can be displayed in a single grid. And maximum dimension you can set is (4,3) which shows 12 months in four columns and three rows.
Following figure shows two columns and two rows in the calendar grid because dimensions are set as (2,2)
Following code can be used to set the number of months vertically and horizontally.
monthCalendar1.CalendarDimensions = new System.Drawing.Size (3,2);
TodayDate is very useful property that determines the current date it captures from the system. Great thing is that you can select any date explicitly using TodayDate property and set it as current date.
ShowWeekNumbers
ShowWeekNumbers property allows you to display week number on the calendar. By default this property value is set as false.
Set this value as true if you want to display number of weeks in the current month of the calendar.
In the following figure, 9,10,11,12,13,14 are the week numbers of the calendar year.
BoldedDates and Dock
BoldedDates is an important property that is used to highlight some dates on the calendar.
In order to create bold dates, right click on the calendar and go to properties.
Find the BoldedDates property and click the ellipsis of its field.
This will allow you to open DateTime Collection Editor from where you can bold the dates of your own choice.
You can click add in order to create date member.
As you click add, DateTime field would appear under which you can select any date value.
Repeat the same process again if you want to bold more dates on the calendar. Following figure shows how you can bold some dates.
In order to create bolded dates in the code, you must create DateTime object. Add following code if you want to create specific dates in bold numbers.
DateTime myVacation1 = new DateTime(2018, 3, 16);
DateTime myVacation2 = new DateTime(2018, 3, 17);
monthCalendar1.AddBoldedDate(myVacation1);
monthCalendar1.AddBoldedDate(myVacation2);
Dock property determines the location on the calendar on the main Form. It comes with different values including top, bottom, right, left, fill and none.
Example 1
Following example shows two month of the calendar year in the MonthCalendar Control.
This example shows how you can bold some specific dates of your own choice and how you can make use of properties like MaxDate, MinDate, MaxSelectionCount, ShowToday, ShowTodayCircle etc.
The DateSelected event is also used and its output is displayed on the form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication26
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
DateTime myVacation1 = new DateTime(2018, 3, 16);
DateTime myVacation2 = new DateTime(2018, 3, 17);
monthCalendar1.AddBoldedDate(myVacation1);
monthCalendar1.AddBoldedDate(myVacation2);
this.monthCalendar1.CalendarDimensions = new System.Drawing.Size(2, 1);
this.monthCalendar1.FirstDayOfWeek = System.Windows.Forms.Day.Tuesday;
this.monthCalendar1.MaxDate = new System.DateTime(2028, 12, 31, 0, 0, 0, 0);
this.monthCalendar1.MinDate = new System.DateTime(1990, 1, 1, 0, 0, 0, 0);
this.monthCalendar1.MaxSelectionCount = 20;
this.monthCalendar1.ShowToday = true;
this.monthCalendar1.ShowTodayCircle = true;
this.monthCalendar1.ShowWeekNumbers = true;
this.monthCalendar1.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateSelected);
}
private void monthCalendar1_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e)
{
// Show the start and end dates in the text box.
this.txtLabel.Text = "Date Selected: Start = " +
e.Start.ToShortDateString() + " : End = " + e.End.ToShortDateString();
}
And in Visual Studio Windows Form Application code will appear like below.
That's all for today. I hope you have enjoyed the article. However, if you need help, you can ask me in the comment section below. I'd love to help you in this regard according to best of my expertise. Stay Tuned!
syedzainnasir
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow
Get Connected