Introduction



Home Screen :

Name of Experiment : Display Text Feature

Exp No: WP2

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

Summary : After going through experiment no WP1, now, it's time to explore the display feature of screen by changing the Colour, Font and Font Family for the input Text.

Learning Objective : To explain the supported display feature of the mobile screen by changing the Colour, Font size and Font Family of the Text.

Target

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

Procedure

Source Code


MainPage.xaml

<!--TitlePanel contains the name of the application and page title-->
<!--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 Text Feature" 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">
<TextBlock Height="72" HorizontalAlignment="Left" FontSize="36" Margin="31,84,0,0" Name="textBlock1" VerticalAlignment="Top" Width="403" />
<TextBox Height="72" GotFocus="textBox1_GotFocus" LostFocus="textBox1_LostFocus" Margin="31,6,0,0" Name="textBox1" TextOptions.TextHintingMode="Fixed" VerticalAlignment="Top" TextChanged="textBox1_TextChanged" TextWrapping="Wrap" ></TextBox>
<Button Content="Show" Height="86" HorizontalAlignment="Left" Margin="153,207,0,0" Click="button1_Click" Name="button1" VerticalAlignment="Top" Width="174" />
</Grid>
<Grid x:Name="ContentPanel1" Grid.Row="2" Margin="12,320,12,30">
<RadioButton Content=" Text Color" Height="72" IsEnabled="{Binding ElementName=color}" HorizontalAlignment="Left" Margin="9,6,0,0" Checked="radioButton1_Checked" Name="radioButton1" VerticalAlignment="Top" />
<RadioButton Content="Font Size" Height="72" IsEnabled="{Binding ElementName=font}" HorizontalAlignment="Left" Margin="12,84,0,0" Checked="radioButton2_Checked" Name="radioButton2" VerticalAlignment="Top" />
<RadioButton Content="Font Family" Height="72" IsEnabled="{Binding ElementName=font_style}" HorizontalAlignment="Left" Margin="15,162,0,0" Checked="radioButton3_Checked" Name="radioButton3" VerticalAlignment="Top"/>
</Grid>
</Grid>

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

namespace WP2
{
public partial class MainPage : PhoneApplicationPage
{

Boolean showingHint = true;
String myHint = "Enter the text";
public MainPage()
{
InitializeComponent();
textBox1.Text = myHint;
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string str = textBox1.Text;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text.Equals(myHint)||textBox1.Text==String.Empty)
{
MessageBox.Show("Please Enter some text");
}
else
{
string str = textBox1.Text;

textBlock1.Text = str;
}
}

private void radioButton1_Checked(object sender, RoutedEventArgs e)
{ if (textBlock1.Text =="")
{
MessageBox.Show("Please enter some Text");
}
else
{
this.NavigationService.Navigate(new Uri("/color.xaml?msg=" + textBlock1.Text, UriKind.Relative));
}
}

private void radioButton2_Checked(object sender, RoutedEventArgs e)
{
if (textBlock1.Text == String.Empty)
{
MessageBox.Show("Please enter some text");
}
else
{
this.NavigationService.Navigate(new Uri("/Font.xaml?msg=" + textBlock1.Text, UriKind.Relative));
}
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var a="" ;

if (NavigationContext.QueryString.TryGetValue("msg", out a)) {
textBlock1.Text = a;
}
} private void radioButton3_Checked(object sender, RoutedEventArgs e)
{
if (textBlock1.Text == "")
{
MessageBox.Show("Please enter some text ");
}
else
{
this.NavigationService.Navigate(new Uri("/FontStyle.xaml?msg=" + textBlock1.Text, UriKind.Relative));
} }
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
if (showingHint) {
textBox1.Text = "";
showingHint = false;
}
}

private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
if (!showingHint && textBox1.Text.Length == 0) {
textBox1.Text = myHint;
showingHint = true;
}
}
}
}

color.xaml

<!--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" TextAlignment="Right" Text="Exp No: WP2" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="ApplicationTitle" Text="Basic I/O Display" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Text Color" 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">
<CheckBox Content="Red" Foreground="Red" Height="72" HorizontalAlignment="Left" Margin="12,6,0,0" Name="checkBox1" Checked="checkBox1_Checked" VerticalAlignment="Top" Width="263" />
<CheckBox Content="Yellow" Foreground="Yellow" Height="72" HorizontalAlignment="Left" Margin="12,166,0,0" Name="checkBox2" VerticalAlignment="Top" Checked="checkBox2_Checked" Width=" 263" />
<CheckBox Content="Green" Height="72" Foreground="Green" HorizontalAlignment="Left" Margin="12,84,0,0" Name="checkBox3" VerticalAlignment="Top" Checked="checkBox3_Checked" Width="257" />
<TextBlock Height="163" TextWrapping="Wrap" FontSize="48" HorizontalAlignment="Left" Margin="38,272,0,145" Name="textBlock1" Text="TextBlock" VerticalAlignment="Center" Width="384" />
<HyperlinkButton x:Name="hp_back" Content="Go Home" Click="hp_back_Click" Margin="0,441,0,39" />
</Grid>
</Grid>


Color.xaml.cs
using System.Windows;
using Microsoft.Phone.Controls;
using System;
using System.Windows.Media;
using System.Windows.Controls;
using System.Net;


namespace WP2
{
public partial class Color : PhoneApplicationPage {
public Color()
{
InitializeComponent();
}
public static class GlobalVariables
{
public static string my_string = "";

}
private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
if (checkBox1.IsChecked == true) {
checkBox2.IsChecked = false;
checkBox3.IsChecked = false;
}
textBlock1.Text = GlobalVariables.my_string;
textBlock1.Foreground = new SolidColorBrush(Colors.Red);
}


private void checkBox2_Checked(object sender, RoutedEventArgs e)
{
if (checkBox2.IsChecked == true) {
checkBox1.IsChecked = false;
checkBox3.IsChecked = false;
}

textBlock1.Text = GlobalVariables.my_string;
textBlock1.Foreground = new SolidColorBrush(Colors.Yellow);
}
private void checkBox3_Checked(object sender, RoutedEventArgs e)
{
if (checkBox3.IsChecked == true) {
checkBox1.IsChecked = false;
checkBox2.IsChecked = false;
}
textBlock1.Text = GlobalVariables.my_string;
textBlock1.Foreground = new SolidColorBrush(Colors.Green);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
base.OnNavigatedTo(e);
var str = "";

if (NavigationContext.QueryString.TryGetValue("msg", out str)) {
GlobalVariables.my_string = str;
{
textBlock1.Text = GlobalVariables.my_string;
}
}
}
private void hp_back_Click(object sender, RoutedEventArgs e)
{
if (this.NavigationService.CanGoBack) {
textBlock1.Text = GlobalVariables.my_string;
this.NavigationService.Navigate(new Uri("/MainPage.xaml?msg="+textBlock1.Text, UriKind.Relative));
}
}

}
}



Font.xaml


<!--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: WP2" TextAlignment="Right" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="ApplicationTitle" Text="Basic I/O Display" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Font Size" 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">
<CheckBox Content="12" Height="72" HorizontalAlignment="Left" Margin="12,6,0,0" Name="checkBox1" VerticalAlignment="Top" Checked="checkBox1_Checked_1" />
<CheckBox Content="20" Height="72" HorizontalAlignment="Left" Margin="12,84,0,0" Name="checkBox2" VerticalAlignment="Top" Width="144" Checked="checkBox2_Checked_1"/>
<CheckBox Content="28" Height="72" HorizontalAlignment="Left" Margin="12,166,0,0" Name="checkBox3" VerticalAlignment="Top" Checked="checkBox3_Checked_1" />
<TextBlock Height="154" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="12,272,0,0" FontSize="36" Name="textmsg" Text="" VerticalAlignment="Top" Width="438" />
<HyperlinkButton x:Name="hp_back" Content="Go Back" Click="hp_back_Click" Margin="0,0,0,40" Height="108" VerticalAlignment="Bottom" />
</Grid>
</Grid>



Font.xaml.cs


using System;
using Microsoft.Phone.Controls;
using System.Windows.Media;

namespace WP2
{
public partial class Font : PhoneApplicationPage
{
public Font()
{
InitializeComponent();
}
public static class GlobalVariables {
public static String str="";
} protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.TryGetValue("msg", out msg))
GlobalVariables.str = msg;
textmsg.Text = GlobalVariables.str;
textmsg.Foreground = new SolidColorBrush(Colors.Orange);
}

private void checkBox1_Checked_1(object sender, System.Windows.RoutedEventArgs e)
{
if (checkBox1.IsChecked == true) {
checkBox2.IsChecked = false;
checkBox3.IsChecked = false;
}
textmsg.Text = GlobalVariables.str;
textmsg.FontSize = 12;
}
private void checkBox2_Checked_1(object sender, System.Windows.RoutedEventArgs e)
{
if (checkBox2.IsChecked == true) {
checkBox1.IsChecked = false;
checkBox3.IsChecked = false;
}
textmsg.Text = GlobalVariables.str;
textmsg.FontSize = 20;
}

private void checkBox3_Checked_1(object sender, System.Windows.RoutedEventArgs e) {
if (checkBox3.IsChecked == true) {
checkBox1.IsChecked = false;
checkBox2.IsChecked = false;
}
textmsg.Text = GlobalVariables.str;
textmsg.FontSize = 28;
}
private void hp_back_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (this.NavigationService.CanGoBack) {
this.NavigationService.Navigate(new Uri("/MainPage.xaml?msg="+textmsg.Text, UriKind.Relative));
}
}
}
}

FontStyle.xaml

<!--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" TextAlignment="Right" Text=" Exp No:WP2" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="ApplicationTitle" Text="Basic I/O Display" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Font Family" 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">
<CheckBox Content="Arial" IsChecked="False" Height="72" HorizontalAlignment="Left"
Margin="12,60,0,0" Name="checkBox1" Checked="checkBox1_Checked" VerticalAlignment="Top" />
<CheckBox Content="Times New Roman" Height="72" HorizontalAlignment="Left" Margin="12,120,0,0" Name="checkBox2" Checked="checkBox2_Checked" VerticalAlignment="Top" />
<CheckBox Content="Verdana" Height="72" HorizontalAlignment="Left" Margin="12,180,0,0" Name="checkBox3" Checked="checkBox3_Checked" VerticalAlignment="Top" />
<TextBlock Height="125" HorizontalAlignment="Left" FontSize="40" TextWrapping="Wrap" Margin="37,326,0,0" Name="textBlock1" VerticalAlignment="Top" Width="378" />
<HyperlinkButton x:Name="hp_home" Content="Go Home" Click="hp_home_Click" Margin="0,451,-8,6" />
</Grid>
</Grid>

FontStyle.xaml.cs
using Microsoft.Phone.Controls;
using System.Windows;
using System;
using System.Windows.Controls;
using System.Windows.Media;

namespace WP2
{
public partial class FontStyle : PhoneApplicationPage
{
public FontStyle()
{
InitializeComponent();
}
public static class GlobalVariables
{
public static string mystring = "";
}
private void hp_home_Click(object sender, RoutedEventArgs e)
{
if (this.NavigationService.CanGoBack) {
this.NavigationService.Navigate(new Uri("/MainPage.xaml?msg=" + textBlock1.Text, UriKind.Relative));
}
}

private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
if (checkBox1.IsChecked==true) {
checkBox2.IsChecked= false;
checkBox3.IsChecked = false;

}
textBlock1.Text = GlobalVariables.mystring;
textBlock1.FontFamily = new FontFamily("Arial");
}

private void checkBox2_Checked(object sender, RoutedEventArgs e)
{
if (checkBox2.IsChecked == true) {
checkBox1.IsChecked = false;
checkBox3.IsChecked = false;
}
textBlock1.Text = GlobalVariables.mystring;
textBlock1.FontFamily = new FontFamily("Times New Roman");
}

private void checkBox3_Checked(object sender, RoutedEventArgs e)
{
if (checkBox3.IsChecked == true) {
checkBox1.IsChecked = false;
checkBox2.IsChecked = false;
}
textBlock1.Text = GlobalVariables.mystring;
textBlock1.FontFamily = new FontFamily("Verdana");
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string str = "";
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.TryGetValue("msg", out str)) { GlobalVariables.mystring = str;
textBlock1.Text = GlobalVariables.mystring;
}
}
}
}

Snapshots








Observation

Observation : This experiment on "Display Text Feature" for Text can be used to integrate in vast variety of Project.

Video Link

For Windows Phone

Download

Right Click Here And Select Save Target For Entire Download :

Display Text Feature