Colored Label Text Using WPF

Now, we will create an appliction for displaying the labeled text using WPF(Windows Presentation Foundation). In this application, it will display the color labeled text by clicking on the corresponding buttons.For example, we click a green button that will display the green text.

First open Visual Studio and then select File->New->Project then select Visual C# template then, select WPF App(.NET Framework) then, select the file name and choose the directory for saving the file.Then click OK.

 
 Then, from the toolbar select the textblock and name the application name then select the buttons and place it on the screen and then set the labels on the screen.

 

 
 

Then, set the label properties such as changing the background color to the corresponding color.

 

And keep the label's content as blank. And silmilarly do it for remaining labels. Then change the button properties also. Your screen will appear like this.


The xaml code will be as follows,
  1. <Window x:Class="Coloured_label_text.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Coloured_label_text" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525">  
  2.     <Grid>  
  3.         <TextBlock x:Name="tbname" HorizontalAlignment="Left" Height="22" Margin="175,25,0,0" TextWrapping="Wrap" Text="Coloured Label Text" VerticalAlignment="Top" Width="157" FontSize="14" />  
  4.         <Button x:Name="btnred" Content="Red" HorizontalAlignment="Left" Height="20" Margin="63,71,0,0" VerticalAlignment="Top" Width="79" Click="btnred_Click" />  
  5.         <Button x:Name="btnblue" Content="Blue" HorizontalAlignment="Left" Height="20" Margin="164,71,0,0" VerticalAlignment="Top" Width="79" Click="btnblue_Click" />  
  6.         <Button x:Name="btngreen" Content="Green" HorizontalAlignment="Left" Height="20" Margin="262,71,0,0" VerticalAlignment="Top" Width="79" Click="btngreen_Click" />  
  7.         <Button x:Name="btnviolet" Content="Violet" HorizontalAlignment="Left" Height="20" Margin="367,71,0,0" VerticalAlignment="Top" Width="79" Click="btnviolet_Click" />  
  8.         <Label x:Name="lblred" Content="" HorizontalAlignment="Left" Height="25" Margin="119,121,0,0" VerticalAlignment="Top" Width="111" Foreground="#FFFF0606" />  
  9.         <Label x:Name="lblblue" Content="" HorizontalAlignment="Left" Height="25" Margin="262,121,0,0" VerticalAlignment="Top" Width="111" Foreground="#FF0426FF" />  
  10.         <Label x:Name="lblgreen" Content="" HorizontalAlignment="Left" Height="25" Margin="119,166,0,0" VerticalAlignment="Top" Width="111" Foreground="#FF20FF0A" />  
  11.         <Label x:Name="lblviolet" Content="" HorizontalAlignment="Left" Height="25" Margin="262,166,0,0" VerticalAlignment="Top" Width="111" Foreground="#FF903BD6" /> </Grid>  
  12. </Window>  
The code will be like this 
 

After double clicking on the button write the code in .cs for each button and this will execute when the corresponding button is clicked,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Data;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Navigation;  
  14. using System.Windows.Shapes;  
  15. namespace Coloured_label_text {  
  16.     /// <summary>  
  17.     /// Interaction logic for MainWindow.xaml  
  18.     /// </summary>  
  19.     public partial class MainWindow: Window {  
  20.         public MainWindow() {  
  21.             InitializeComponent();  
  22.         }  
  23.         private void btnred_Click(object sender, RoutedEventArgs e) {  
  24.             lblred.Content = "You clicked Red";  
  25.             lblblue.Content = String.Empty;  
  26.             lblgreen.Content = String.Empty;  
  27.             lblviolet.Content = String.Empty;  
  28.         }  
  29.         private void btnblue_Click(object sender, RoutedEventArgs e) {  
  30.             lblred.Content = String.Empty;  
  31.             lblblue.Content = "You clicked Blue";  
  32.             lblgreen.Content = String.Empty;  
  33.             lblviolet.Content = String.Empty;  
  34.         }  
  35.         private void btngreen_Click(object sender, RoutedEventArgs e) {  
  36.             lblred.Content = String.Empty;;  
  37.             lblblue.Content = String.Empty;  
  38.             lblgreen.Content = "You clicked Green";  
  39.             lblviolet.Content = String.Empty;  
  40.         }  
  41.         private void btnviolet_Click(object sender, RoutedEventArgs e) {  
  42.             lblred.Content = String.Empty;;  
  43.             lblblue.Content = String.Empty;  
  44.             lblgreen.Content = String.Empty;  
  45.             lblviolet.Content = "You clicked Violet";  
  46.         }  
  47.     }  
  48. }  
And the screen will be,
 
Then run the application,
 
Now click on the Red button and the re text  will be displayed on screen
 
 Similarly click on other buttons also
 
 
 
Now we have created an application that displays the colored text when the corresponding button is clicked. 
G
M
T
 
Text-to-speech function is limited to 200 characters