Calendar Control in VB.NET
In this article, We will see how to use calendar control in VB.NET 2008.
In this article, We will see how to use calendar control in VB.NET 2008.
In this article, We will see how to use calendar control and show current date and time run time in VB.NET 2008.
This control displays a onemonth at a time that allows the user to select dates and move the next and previous months. This type we will see all months of year. This control displays the name of the current month, day headings for the days of the weeks, days of the month and arrow characters for navigation to the previous or next month.
This code is aspx:-
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Candelar Control</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p style="text-align: center">
<b>
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Arial Black" Font
Size="Medium" ForeColor="#0066FF">Indian List of Holidays 2010</asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server" ForeColor="Red" BackColor="#ffccff" Font
Bold="true"></asp:TextBox>
</b>
</p>
<asp:Calendar ID="CalendarControl" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#663399" ShowGridLines="True" OnDayRender="Calendar1_DayRender"
OnSelectionChanged="Calendar1_SelectionChanged"
OnVisibleMonthChanged="Calendar1_VisibleMonthChanged">
<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
<SelectorStyle BackColor="#FFCC66" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
</asp:Calendar>
<br />
<b></b>
<asp:Label ID="LabelAction" runat="server"></asp:Label><br />
</b>
</div>
</form>
</body>
</html>
This code is .vb:-
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Collections
-------------------------------------------------------------------------------------------------------
Partial Class _Default
Inherits System.Web.UI.Page
Private HolidayList As Hashtable
-------------------------------------------------------------------------------------------------------
Protected Sub CalendarControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalendarControl.Load
HolidayList = GetHolidayOfIndia()
CalendarControl.FirstDayOfWeek = FirstDayOfWeek.Sunday
CalendarControl.NextPrevFormat = NextPrevFormat.ShortMonth
CalendarControl.TitleFormat = TitleFormat.Month
CalendarControl.ShowGridLines = True
CalendarControl.DayStyle.Height = New Unit(40)
CalendarControl.DayStyle.Width = New Unit(130)
CalendarControl.DayStyle.HorizontalAlign = HorizontalAlign.Center
CalendarControl.DayStyle.VerticalAlign = VerticalAlign.Middle
CalendarControl.OtherMonthDayStyle.BackColor = System.Drawing.Color.AliceBlue
TextBox1.Text = DateTime.Now.ToString()
End Sub
-------------------------------------------------------------------------------------------------------
Private Function GetHolidayOfIndia() As Hashtable
Dim holiday As New Hashtable()
holiday("1/1/2010") = "New Year"
holiday("1/4/2010") = "Muharam (Al Hijra)"
holiday("1/5/2010") = "Guru Govind Singh Jayanti"
holiday("1/14/2010") = "Tamil New year, Pongal, Makar Sankranti"
holiday("1/15/2010") = "Basanta Panchami"
holiday("1/26/2010") = "Republic Day"
holiday("2/12/2010") = "Maha Shivaratri"
holiday("2/27/2010") = "Milad un Nabi (Birthday of the Prophet"
holiday("3/1/2010") = "Holi"
holiday("3/16/2010") = "Telugu New Year"
holiday("3/24/2010") = "Ram Navmi"
holiday("3/28/2010") = "Mahavir Jayanti"
holiday("4/2/2010") = "Good Friday"
holiday("4/4/2010") = "Easter"
holiday("5/1/2010") = "May Day"
holiday("5/27/2010") = "Buddha Jayanti and Buddha Purnima"
holiday("8/15/2010") = "Independence Day"
holiday("8/19/2010") = "Parsi New Year"
holiday("8/23/2010") = "Onam"
holiday("8/24/2010") = "Raksha Bandhan"
holiday("9/2/2010") = "Krishna Jayanthi"
holiday("9/5/2010") = "Teachers Day"
holiday("9/9/2010") = "Ramzan"
holiday("9/11/2010") = "Vinayaka Chaturthi"
holiday("10/2/2010") = "Gandhi Jayanti"
holiday("10/16/2010") = "Sarasvathi Pooja, Aytha Pooja"
holiday("10/17/2010") = "Vijayathasami"
holiday("11/5/2010") = "Diwali"
holiday("11/14/2010") = "Children's Day"
holiday("11/17/2010") = "Bakrid"
holiday("11/21/2010") = "Guru Nanak Jayanti"
holiday("12/17/2010") = "Muharram"
holiday("12/25/2010") = "Christmas"
Return holiday
End Function
-------------------------------------------------------------------------------------------------------
Protected Sub CalendarControl_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
LabelAction.Text = "Date changed to :" + Calendar1.SelectedDate.ToShortDateString()
End Sub
-------------------------------------------------------------------------------------------------------
Protected Sub CalendarControl_VisibleMonthChanged(ByVal sender As Object, ByVal e As
MonthChangedEventArgs)
LabelAction.Text = "Month changed to :" & e.NewDate.ToShortDateString()
End Sub
------------------------------------------------------------------------------------------------------
Protected Sub CalendarControl_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
If HolidayList(e.Day.[Date].ToShortDateString()) IsNot Nothing Then
Dim literal1 As New Literal()
literal1.Text = "<br/>"
e.Cell.Controls.Add(literal1)
Dim label1 As New Label()
label1.Text = DirectCast(HolidayList(e.Day.[Date].ToShortDateString()), String)
label1.Font.Size = New FontUnit(FontSize.Small)
e.Cell.Controls.Add(label1)
End If
End Sub
End Class
-------------------------------------------------------------------------------------------------------
Output:-

After run time we will see calendar of 2010 with India Holidays.
Conclusion:-
I hope this article would have helped you in understanding the calendar control.