Day Finder of Any Date in Windows Store App

Introduction

In this article we will create a Metro style application to determine the name of the week day of any specific date. In this application we are able to fetch the name of the day given any date as input. In this application we will use three comboboxes for the input; date, month and year. On the click event of the button the code in the code behind file will be executed and will give the precise result. In this application we will utilize the DateTime class and their method to get the result that is a pre- defined class of .Net environment.    

In the following we are providing the entire code of XAML file and code behind file to create this mini application. 

Step 1 : First, you will create a new Metro Style Application. Let us see the description with images of how you will create it.

  • Open Visual Studio 2012
  • File -> New -> Project
  • Choose Template -> Visual C# -> Metro Style Application
  • Rename the application

img1.gif

Step 2 : In the Solution Explorer there are two files that we will primarily work with; the MainPage.xaml and MainPage.xaml.cs files.

img2.gif

Step 3 : The MainPage.xaml file is as in the following code:

Code :

<Page

    x:Class="App1.MainPage"

    IsTabStop="false"

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

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

    xmlns:local="using:App1"

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

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

    mc:Ignorable="d">

 

    <Grid Background="SkyBlue">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width=".133*"></ColumnDefinition>

            <ColumnDefinition Width=".033*"></ColumnDefinition>

            <ColumnDefinition Width=".333*"></ColumnDefinition>

            <ColumnDefinition Width=".333*"></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height=".333*"></RowDefinition>

            <RowDefinition Height=".063*"></RowDefinition>

            <RowDefinition Height=".063*"></RowDefinition>

            <RowDefinition Height=".063*"></RowDefinition>

            <RowDefinition Height=".133*"></RowDefinition>

        </Grid.RowDefinitions>

        <TextBlock Grid.Column="2" Grid.Row="0" Text="Find the Name of Day of any Date" FontSize="30" Foreground="Blue" FontWeight="ExtraBlack" Margin="0,350,0,0" ></TextBlock>

        <TextBlock Grid.Column="0" Grid.Row="1" Text="Select Date:" HorizontalAlignment="Right" FontSize="20" FontWeight="Bold" Foreground="Red"></TextBlock>

        <ComboBox x:Name="dd" Grid.Column="2" Grid.Row="1" BorderThickness="5" BorderBrush="Black" FontWeight="Bold" Background="Yellow" Height="50" Width="300" HorizontalAlignment="Left"  VerticalAlignment="Top">

            <ComboBoxItem>1</ComboBoxItem>

            <ComboBoxItem>2</ComboBoxItem>

            <ComboBoxItem>3</ComboBoxItem>

            <ComboBoxItem>4</ComboBoxItem>

            <ComboBoxItem>5</ComboBoxItem>

            <ComboBoxItem>6</ComboBoxItem>

            <ComboBoxItem>7</ComboBoxItem>

            <ComboBoxItem>8</ComboBoxItem>

            <ComboBoxItem>9</ComboBoxItem>

            <ComboBoxItem>10</ComboBoxItem>

            <ComboBoxItem>11</ComboBoxItem>

            <ComboBoxItem>12</ComboBoxItem>

            <ComboBoxItem>13</ComboBoxItem>

            <ComboBoxItem>14</ComboBoxItem>

            <ComboBoxItem>15</ComboBoxItem>

            <ComboBoxItem>16</ComboBoxItem>

            <ComboBoxItem>17</ComboBoxItem>

            <ComboBoxItem>18</ComboBoxItem>

            <ComboBoxItem>19</ComboBoxItem>

            <ComboBoxItem>20</ComboBoxItem>

            <ComboBoxItem>21</ComboBoxItem>

            <ComboBoxItem>22</ComboBoxItem>

            <ComboBoxItem>23</ComboBoxItem>

            <ComboBoxItem>24</ComboBoxItem>

            <ComboBoxItem>25</ComboBoxItem>

            <ComboBoxItem>26</ComboBoxItem>

            <ComboBoxItem>27</ComboBoxItem>

            <ComboBoxItem>28</ComboBoxItem>

            <ComboBoxItem>29</ComboBoxItem>

            <ComboBoxItem>30</ComboBoxItem>

            <ComboBoxItem>31</ComboBoxItem>

        </ComboBox>

        <TextBlock Grid.Column="0" Grid.Row="2" Text="Select Month:" HorizontalAlignment="Right" FontSize="20" FontWeight="Bold" Foreground="Red"></TextBlock>

        <ComboBox x:Name="mm" Grid.Column="2" Grid.Row="2" BorderThickness="5" BorderBrush="Black" FontWeight="Bold" Background="Yellow" Height="50" Width="300" HorizontalAlignment="Left" VerticalAlignment="Top">

            <ComboBoxItem>Januvary</ComboBoxItem>

            <ComboBoxItem>February</ComboBoxItem>

            <ComboBoxItem>March</ComboBoxItem>

            <ComboBoxItem>April</ComboBoxItem>

            <ComboBoxItem>May</ComboBoxItem>

            <ComboBoxItem>June</ComboBoxItem>

            <ComboBoxItem>July</ComboBoxItem>

            <ComboBoxItem>August</ComboBoxItem>

            <ComboBoxItem>September</ComboBoxItem>

            <ComboBoxItem>October</ComboBoxItem>

            <ComboBoxItem>November</ComboBoxItem>

            <ComboBoxItem>December</ComboBoxItem>

        </ComboBox>

        <TextBlock Grid.Column="0" Grid.Row="3" Text="Select Year:" HorizontalAlignment="Right" FontSize="20" FontWeight="Bold" Foreground="Red"></TextBlock>

        <ComboBox x:Name="yy" Grid.Column="2" Grid.Row="3" BorderThickness="5" BorderBrush="Black" FontWeight="Bold" Background="Yellow" Height="50" Width="300" HorizontalAlignment="Left"  VerticalAlignment="Top">

            <ComboBoxItem>2000</ComboBoxItem>

            <ComboBoxItem>2001</ComboBoxItem>

            <ComboBoxItem>2002</ComboBoxItem>

            <ComboBoxItem>2003</ComboBoxItem>

            <ComboBoxItem>2004</ComboBoxItem>

            <ComboBoxItem>2005</ComboBoxItem>

            <ComboBoxItem>2006</ComboBoxItem>

            <ComboBoxItem>2007</ComboBoxItem>

            <ComboBoxItem>2008</ComboBoxItem>

            <ComboBoxItem>2009</ComboBoxItem>

            <ComboBoxItem>2010</ComboBoxItem>

            <ComboBoxItem>2011</ComboBoxItem>

            <ComboBoxItem>2012</ComboBoxItem>

        </ComboBox>

        <TextBlock x:Name="res" Grid.Column="2" Grid.Row="4" Text="Name of the Day" FontSize="30" Foreground="Red" FontWeight="ExtraBlack" VerticalAlignment="Top" Visibility="Collapsed"></TextBlock>

        <Button Grid.Column="2" Grid.Row="4" VerticalAlignment="Center" Content="Get Day_Name" Background="Red" HorizontalAlignment="Center" Click="Button_Click_1"></Button>

    </Grid>

</Page>

 

Step 4 : The MainPage.xaml.cs file is as in the following code:

Code :

 

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

 

namespace App1

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

 

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            int d = Convert.ToInt32(dd.SelectionBoxItem);

            int m = Convert.ToInt32(mm.SelectedIndex);

            int y = Convert.ToInt32(yy.SelectionBoxItem);

            DateTime dateValue = new DateTime(y, ++m, d);

            res.Visibility = Windows.UI.Xaml.Visibility.Visible

            res.Text=dateValue.ToString("dddd");

        }

    }

}

 

Step 5 : After running this code the output looks like this:

img3.gif

img4.gif

img5.gif


Similar Articles