Introduction



Home Screen :

Name of Experiment : Display the Unicode for the key-board characters.

Exp No: WP3

Background : Student should have a basic knowledge of C#.

Summary : After going through this experiment, the student is aware of the fact that each character on the alpha-numeric virtual keyboard has a unique constant value (i.e. ASCII) associated with it.

Learning Objective : To explain the Unicode value associated with each alpha-numeric character in the keyboard.

Target

Target Platform : This application is tested on Windows Phone Emulator and Windows Phone ( Lumia 800).

Procedure

Source Code


MainPage.xaml


<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ExperimentTitle" Text="Exp No:WP3" TextAlignment="Right" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="ApplicationTitle" Text="Display Unicode Character" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Home Page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBox Height="72" HorizontalAlignment="Left" GotFocus="textBox1_GotFocus" LostFocus="textBox1_LostFocus" Margin="44,91,0,0" Name="textBox1" Text="TextBox" VerticalAlignment="Top" Width="338" />
<Button Content="Get Unicode" Height="72" HorizontalAlignment="Left" Margin="110,198,0,0" Name="button1" VerticalAlignment="Top" Width="230" Click="button1_Click" />
<TextBlock Height="64" HorizontalAlignment="Left" Margin="133,326,0,0" Name="mytext" VerticalAlignment="Top" Width="189" />
</Grid>
</Grid>

MainPage.xaml.cs
using System;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Text;

namespace Unicode
{

public partial class MainPage : PhoneApplicationPage
{

bool hint = true;
string hint_text = "Enter the character";
//char[] mychars;
// Constructor

public MainPage()
{
InitializeComponent();
textBox1.Text = hint_text;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
char[] str1;
if (textBox1.Text.Equals(hint_text) || textBox1.Text == string.Empty)
{
MessageBox.Show("Please enter some character");
}
else
{
string str = textBox1.Text;
str1 = str.ToCharArray();
Encoding u16LE = Encoding.Unicode;

Print_Unicode(str1, u16LE);
}
}
public void Print_Unicode(char [] st, Encoding en)
{
byte[] bytes = en.GetBytes(st);
PrintHexBytes(bytes);
}
public void PrintHexBytes(byte[] bytes)
{
if ((bytes == null) || (bytes.Length == 0))
Console.WriteLine("<none>");
else
{
mytext.Text = string.Empty;

mytext.Text = bytes[0].ToString();
}
}
}

private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
if (hint) {
textBox1.Text = string.Empty;
hint = false;
}
}
private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
if (!hint && textBox1.Text.Length==0) {
textBox1.Text = hint_text;
hint = true;
}
}
}
}

Snapshots







Observation

Observation : It is observed that each character in the virtual keyboard has been associated with some Unicode through which it is identified by the hardware.

Video Link

For Windows Phone

Download

Right Click Here And Select Save Target For Entire Download :

Display the Unicode for the key-board characters