Some Calendar Functions in Windows Store App

Introduction

In this article I will explain how to create and use the calendar class and functions. In this article I made a Windows 8 app using some calendar functions, which shows the current date, week and name of the day of a current month. Also some functions are provided so we can display the current year and current month of the current year etc. By using the calendar function we can create the calendar class first, and use it which resides in the Windows.Globalization namespace. To see how this works let's use the following steps.

Step 1

First open the Visual Studio 2012 RC and click on File -> New -> Project. A window is shown; select Windows Store in Visual C# from the left pane and Blank page from the center pane as in:

Select-Blank-Page-In-Windows-8-Apps.jpg

Step 2

In the MainPage.xaml file write the code as:

<Page

    x:Class="App10.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:App10"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

    <Grid>

        <Grid.Background>

            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                <GradientStop Color="Black"/>

                <GradientStop Color="#FFF99AE8" Offset="1"/>

            </LinearGradientBrush>

        </Grid.Background>

        <TextBlock Text="Calender Examples" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Top"/>

        <StackPanel Orientation="Vertical">

            <StackPanel Orientation="Horizontal">

                <TextBlock Text="Select any options" FontSize="15" Margin="50,150,0,0"/>

                <TextBlock Name="Description" FontSize="20" Margin="350,150,0,0" Width="650" Height="50"/>

            </StackPanel>

            <ListBox x:Name="list1" Height="250" Width="500" HorizontalAlignment="Left" Margin="50" SelectionChanged="list1_SelectionChanged" Background="#CCB6EECA">

                <x:String>Display The Calender Type</x:String>

                <x:String>Display the Month of this Year</x:String>

                <x:String>Display the year</x:String>

                <x:String>Display the Day of this month</x:String>

                <x:String>Display the week of this month</x:String>              

            </ListBox>

        </StackPanel>

        <StackPanel Orientation="Vertical">

            <TextBlock Name="output" Text="OUTPUT" FontSize="15" Margin="50,550,0,0"/>

            <TextBlock Name="DisplayData" FontSize="25" Margin="50,50,0,0"/>

        </StackPanel>

    </Grid>

</Page>

Step 3

Add the following namespace in the MainPage.xaml.cs file as:

using Windows.Globalization;

Step 4

Write the code in the MainPage.xaml.cs file as:

namespace App10

{

    public sealed partial class MainPage : Page

    {

        Calendar calender = new Calendar();

        public MainPage()

        {

            this.InitializeComponent();

        }

        private void list1_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            if (list1.SelectedIndex == 0)

            {

                DisplayData.Text = calender.GetCalendarSystem();

                Description.Text = "This Option display the calender system";

            }

            else if (list1.SelectedIndex == 1)

            {

                DisplayData.Text = calender.MonthAsSoloString();

                Description.Text = "This option gives the current month of the current year";

            }

            else if (list1.SelectedIndex == 2)

            {

                DisplayData.Text = calender.YearAsString();

                Description.Text = "This will display the current yaer";

            }

            else if (list1.SelectedIndex == 3)

            {

                DisplayData.Text = calender.DayAsPaddedString(1);

                Description.Text = "This option display the day of this particular month";

            }

            else if (list1.SelectedIndex == 4)

            {

                DisplayData.Text = calender.DayOfWeekAsString();

                Description.Text = "This will display the current week of the day";

            }

            else

            {

                DisplayData.Text = "wrong selection";

            }

        }

    }

} 

Step 5

Now run the application to see the output. The first screen that is displayed looks like:

Calender-Functions-List-In-Windows-8-Apps.jpg

Now I select the options from the listbox. The output screen will look like:

Display-Calender-Type-In-Windows-8-Apps.jpg


Display-Month-In-The-Calender-In-Windows-8-Apps.jpg


Display-Year-In-Windows-8-Apps-Using-Calender-Class.jpg


Display-Date-In-Windows-8-Apps-Using-Calender-Class.jpg


Display-Week-In-Windows-8-Apps-Using-Calender-Class.jpg

Summary

In this article I explained some basic functions of the calendar class and the use them in Windows 8 apps.


Similar Articles