Auto Generate Password in Windows store App

Introduction

Today we will learn about a very interesting class GUID (Globally Unique Identifier) in Metro Style applications. It is a predefined utility that we can use to randomly generate an identifier number. GUIDs are used for many purposes, including the purpose of generating automatic words since a GUID is highly unlikely to be duplicated each time one is generated. Here we will take a simple example to generate a unique 10-digit word. GUIDs have many other purposes, including identification of interfaces for COM.

In the following we are including 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 to 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:App7"

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

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

    mc:Ignorable="d">

 

<Grid x:Name="LayoutRoot">

        <Grid.RowDefinitions>

            <RowDefinition Height="76*"/>

            <RowDefinition Height="55*"/>

            <RowDefinition Height="40*"/>

            <RowDefinition Height="37*"/>

            <RowDefinition Height="70*"/>

            <RowDefinition Height="3*"/>

            <RowDefinition Height="119*"/>

            <RowDefinition Height="9*"/>

            <RowDefinition Height="359*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="0.333*"/>

            <ColumnDefinition Width="0.022*"/>

            <ColumnDefinition Width="0.312*"/>

            <ColumnDefinition Width="0.333*"/>

        </Grid.ColumnDefinitions>

        <Grid.Background>

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

                <GradientStop Color="Blue" Offset="1"/>

                <GradientStop Color="Green"/>

            </LinearGradientBrush>

        </Grid.Background>

        <TextBlock Text="User Name" FontSize="20" TextWrapping="Wrap"

                   Margin="0,8" Grid.Row="2" HorizontalAlignment="Right"

                   VerticalAlignment="Center" Height="24" Width="98"/>

        <TextBlock x:Name="namvalidate" FontSize="20" Foreground="Red"

                   TextWrapping="Wrap" Margin="0,20" Grid.Column="3"

                   Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Center"

                   Visibility="Collapsed" Height="0" Width="0"/>

        <TextBlock HorizontalAlignment="Right" FontSize="20" Margin="0,7,0,6"

                   VerticalAlignment="Center" Grid.Row="3" Text="Email ID"

                   TextWrapping="Wrap" Height="24" Width="72"/>

        <TextBlock x:Name="emailvalidate" FontSize="20" Foreground="Red"

                   TextWrapping="Wrap" Margin="0,19,0,18" Grid.Column="3"

                   Grid.Row="3" HorizontalAlignment="Left" VerticalAlignment="Center"

                   Visibility="Collapsed" Height="0" Width="0"/>

       

        <TextBlock x:Name="agevalidate" FontSize="20" Foreground="Red"

                   TextWrapping="Wrap" Margin="0,60,0,59" Grid.Column="3"

                   Grid.Row="6" HorizontalAlignment="Left" VerticalAlignment="Center"

                   Visibility="Collapsed" Height="0" Width="0"/>

        <TextBox x:Name="txtUserName" TextWrapping="Wrap" Margin="0,4"

                 Grid.Column="2" Grid.Row="2" VerticalAlignment="Center" Height="32">

            <TextBox.Text>

                <Binding Mode="TwoWay" Path="UserName" />

            </TextBox.Text>

        </TextBox>

        <TextBox x:Name="txtEmailID" Margin="0,3,0,2" VerticalAlignment="Center"

                 Grid.Column="2" Grid.Row="3" TextWrapping="Wrap" Height="32">

            <TextBox.Text>

                <Binding Mode="TwoWay" Path="EmailID" />

            </TextBox.Text>

        </TextBox>

        <Button Click="Button_Click_1" Grid.Column="2" Grid.Row="4" 

                Content="Get word" Background="Red" HorizontalAlignment="Center"

                Height="35" Margin="151,23,151,12" Width="124"></Button>

  

    <Grid x:Name="grid1" Grid.Column="0" Grid.ColumnSpan="4"

          Grid.Row="6" Visibility="Collapsed">

     <Grid.RowDefinitions>

                <RowDefinition Height="0.333*"/>

                <RowDefinition Height="0.333*"/>

                <RowDefinition Height="0.333*"/>

            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="0.333*"/>

                <ColumnDefinition Width="0.022*"/>

                <ColumnDefinition Width="0.312*"/>

                <ColumnDefinition Width="0.333*"/>

            </Grid.ColumnDefinitions>

       <TextBlock x:Name="txthead" Grid.Column="2" Grid.Row="0"

                  Text="Automatic Generate UserID and word" FontSize="20"

                  FontWeight="ExtraBold"></TextBlock>

       <TextBlock HorizontalAlignment="Right" FontSize="20" Margin="0"

                  VerticalAlignment="Center" Grid.Row="2" Text="Your word"

                  TextWrapping="Wrap"/>

       <TextBlock HorizontalAlignment="Right" Margin="0" FontSize="20"

                  VerticalAlignment="Center" Grid.Row="1" Text="Your User ID"

                  TextWrapping="Wrap"/>

       <TextBox x:Name="txt" Margin="0" VerticalAlignment="Center"

                Grid.Column="2" Grid.Row="2" TextWrapping="Wrap">

       </TextBox>

       <TextBox x:Name="txtID" Margin="0" VerticalAlignment="Center"

                Grid.Column="2" Grid.Row="1" TextWrapping="Wrap">

       </TextBox>

    </Grid>

    </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)

        {

            grid1.Visibility = Visibility.Visible;

            string id = txtEmailID.Text;

            txtID.Text = id.Substring(0, id.IndexOf("@"));

            string guidResult = System.Guid.NewGuid().ToString();

            guidResult = guidResult.Replace("-", string.Empty);

            txt.Text = guidResult.Substring(0,10);

        }

    }

}

 

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

img3.gif

Fill in the value and click the Getword button:

img4.gif


Similar Articles