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;
}
}
}
}
|