Step 2 : In the Solution Explorer there are two files that we will primarily work with; the MainPage.xaml and MainPage.xaml.cs files.
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:App13"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".333*"></ColumnDefinition>
<ColumnDefinition Width=".333*"></ColumnDefinition>
<ColumnDefinition Width=".333*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height=".083*"></RowDefinition>
<RowDefinition Height=".033*"></RowDefinition>
<RowDefinition Height=".033*"></RowDefinition>
<RowDefinition Height=".133*"></RowDefinition>
<RowDefinition Height=".333*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="Would you like to know your Age" FontWeight="ExtraBold"
FontSize="30" Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2"
HorizontalAlignment="Center">
</TextBlock>
<TextBlock Text="Please Enter your DOB" Grid.Column="1" Grid.Row="1"
FontSize="20" HorizontalAlignment="Right" FontWeight="Bold">
</TextBlock>
<TextBox x:Name="txt1" Grid.Column="2" Grid.Row="1" Margin="10,0,100,0"
Width="200" VerticalAlignment="Top" FontWeight="Bold">
</TextBox>
<TextBlock Text="Your age is:" Grid.Column="1" Grid.Row="2" FontSize="20"
HorizontalAlignment="Right" FontWeight="Bold">
</TextBlock>
<TextBlock x:Name="txt2" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Left"
FontSize="20" FontWeight="Bold" Margin="50,0,0,0" VerticalAlignment="Top">
</TextBlock>
<Button Grid.Column="2" Grid.Row="3" Content="GetAge" Background="Green"
Click="Button_Click_1" Margin="10,10,0,0" VerticalAlignment="Top">
</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)
{
string s=txt1.Text;
int y=Convert.ToInt32(s.Substring(0,4));
int m=Convert.ToInt32(s.Substring(5,2));
int d=Convert.ToInt32(s.Substring(8,2));
DateTime dt=DateTime.Now;
int age=dt.Year-y;
if(dt.Month < m || dt.Month==m && dt.Day <d)
{
age--;
}
txt2.Text = age.ToString();
}
}
}
Step 5 : After running this code the output looks like this: