Calendar control and holidays






3.58/5 (8 votes)
Calendar control that distinguishes holidays and what day was chosen through the click event.
Introduction
This article is a simple walkthrough of how to use the Calendar
control in Visual Studio 2008 using the C# programming language. The Calendar
control is a feature which allows the user to select dates specified or move in between months. There are many things you can customize under the Properties window in Visual Studio, such as DayHeaderStyle
, DayStyle
, NextPrevStyle
, OtherMonthDayStyle
, SelectedDayStyle
, SelectorStyle
, and WeedendDayStyle
. These can be modified for a change in text, color, borders, alignment, width, and height. Since this is a simple basic walkthrough, we will leave those alone for now.
Using the Code
To start you off, you will want to run Visual Studio and create a new project. The ideal choice for this walkthrough would be to use a Web Form. Once your Web Project is created, you will notice your toolbox on the left of your screen. The first thing I will do is place a Label
at the top of the page, identifying the page to the user. My Label
will read “Calendar Selection Page”. I have also changed the format of the Label
and the color of the page for aesthetics (of course, this is optional). Under your Standard tools, there is a control labeled Calendar
. To insert this into your project, you may click and drag it into your design view screen, or double click the control in the toolbox.
Now, you should see a Calendar
in your project window along with a Label
. In this project, we want to acknowledge the selected date through a Label
on the bottom portion of the screen. This is all determined through the properties you set with the SelectionMode
in your properties window. By default, this property is set to Day
, but you also have the options of DayWeek
or DayWeekMonth
. In this walkthrough, we are going to create a calendar that acknowledges which date you chose, by telling you the clicked upon date.
In order to make the date visible, once chosen, you will insert another Label
directly under the Calendar
control, with no Text
property. I’m going to give my Label
a name of selectedLabel
. Now, let’s acknowledge a holiday through rendering the cell-size in the DayRender
method. All of this is shown in the code below. This should give you a basic calendar that reads a date and acknowledges holidays within the Calendar
control itself.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace calenderControl
{
public partial class _Default : System.Web.UI.Page
{
string[,] holiDay = new string[13, 32];
protected void Page_Load(object sender, EventArgs e)
{
holiDay[1, 1] = "New Years Day";
holiDay[1, 17] = "Martin Luther King Day";
holiDay[2, 1] = "National Freedom Day";
holiDay[2, 14] = "Valentine's Day";
holiDay[2, 21] = "Presidents' Day";
holiDay[3, 27] = "Easter Sunday";
holiDay[4, 15] = "Tax Day";
holiDay[5, 1] = "Loyalty Day";
holiDay[5, 8] = "Mothers' Day";
holiDay[5, 22] = "National Meritime Day";
holiDay[5, 30] = "Memorial Day";
holiDay[6, 19] = "Fathers' Day";
holiDay[7, 4] = "Independence Day";
holiDay[9, 5] = "Labor day";
holiDay[9, 11] = "Patriot day";
holiDay[10, 10] = "Columbus Day";
holiDay[10, 31] = "Halloween";
holiDay[11, 11] = "Veterans Day";
holiDay[11, 24] = "Thanksgiving Day";
holiDay[12, 24] = "Christmas Eve Day";
holiDay[12, 25] = "Christmas Day";
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
CalendarDay day = (CalendarDay)e.Day;
TableCell cell = (TableCell)e.Cell;
if (!day.IsOtherMonth)
{
String holidayStr = holiDay[day.Date.Month, day.Date.Day];
if (holidayStr != null)
{
cell.BackColor = System.Drawing.Color.HotPink;
cell.Controls.Add(new LiteralControl("
" + holidayStr));
}
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
selectedLabel.Text = "Date Chosen: " +
Calendar1.SelectedDate.ToLongDateString();
}
}
}