In this article I am going to explain how you can check to determine if a specified year is a leap year or not. This article is created as a Window Store App in C# that determines whether a year is a leap year. For implementing this application I am using a simple XAML textbox that accepts the year in the form of input and a button to implement the BL logic.
To create the app use the following intructions.
Step 1
First of all you have to create a New Window Store Application.
- Open Visual Studio 2012
- "File" -> "New" -> "Project..."
- Choose "Template" -> "Visual C#" -> "Windows Store app" -> "Blank App (XAML)"
- Rename the application
Step 2
Now the project will be opened. Go into Solution Explorer and choose "MainPage.Xaml" for designing and "MainPage.Xaml.cs" to write the business logic.
Step 3
Write the following simple code in "MainPage.Xaml":
<Page
x:Class="App5.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App5"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Blue">
<Grid.RowDefinitions>
<RowDefinition Height="0*"/>
<RowDefinition Height="46*"/>
<RowDefinition Height="73*"/>
<RowDefinition Height="64*"/>
<RowDefinition Height="60*"/>
<RowDefinition Height="474*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="167*"/>
<ColumnDefinition Width="593*"/>
<ColumnDefinition Width="606*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Do you want to know Wether year is leap year or not " FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Foreground="Red" Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="3" ></TextBlock>
<TextBlock Text="Enter year:" FontFamily=" Arial" FontSize="20" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="2" Foreground="Red" HorizontalAlignment="Left" Width="181" Margin="76,10,0,36" />
<TextBox x:Name="tex1" Grid.Column="1" Grid.Row="2" Width="159" HorizontalAlignment="Left" Margin="250,8,0,25" Height="10" VerticalAlignment="Top" />
<TextBlock Text="Year is: " FontFamily=" Arial" FontWeight="ExtraBold" FontSize="20" Grid.Column="1" Grid.Row="3" Foreground="Red" Margin="76,10,376,18" />
<TextBlock x:Name="tex2" FontSize="20" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="3" Foreground="Red" Margin="180,10,255,0" Width="158" Height="26" VerticalAlignment="Top" HorizontalAlignment="Center" RenderTransformOrigin="0.954,0.743"/>
<Button x:Name="button1" Content="Check" Click="Button1_click" Grid.Column="1" Grid.Row="3" Background="Yellow" Foreground="Red" Margin="180,62,0,24" Width="158" Height="38" Grid.RowSpan="2"/>
</Grid>
</Page>
Step 4
Finally write C# code in "MainPage.Xaml.cs".
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 App5
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Button1_click(object sender, RoutedEventArgs e)
{
int y = Convert.ToInt32(tex1.Text);
if(y % 4 == 0 && y%100!=0|| y % 400==0 )
{
tex2.Text = "leap year";
}
else
{
tex2.Text = "Not leap year";
}
}
}
}
Step 5
Now Run your application.