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,
- <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">
- <Grid>
- <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" />
- <Button x:Name="btnred" Content="Red" HorizontalAlignment="Left" Height="20" Margin="63,71,0,0" VerticalAlignment="Top" Width="79" Click="btnred_Click" />
- <Button x:Name="btnblue" Content="Blue" HorizontalAlignment="Left" Height="20" Margin="164,71,0,0" VerticalAlignment="Top" Width="79" Click="btnblue_Click" />
- <Button x:Name="btngreen" Content="Green" HorizontalAlignment="Left" Height="20" Margin="262,71,0,0" VerticalAlignment="Top" Width="79" Click="btngreen_Click" />
- <Button x:Name="btnviolet" Content="Violet" HorizontalAlignment="Left" Height="20" Margin="367,71,0,0" VerticalAlignment="Top" Width="79" Click="btnviolet_Click" />
- <Label x:Name="lblred" Content="" HorizontalAlignment="Left" Height="25" Margin="119,121,0,0" VerticalAlignment="Top" Width="111" Foreground="#FFFF0606" />
- <Label x:Name="lblblue" Content="" HorizontalAlignment="Left" Height="25" Margin="262,121,0,0" VerticalAlignment="Top" Width="111" Foreground="#FF0426FF" />
- <Label x:Name="lblgreen" Content="" HorizontalAlignment="Left" Height="25" Margin="119,166,0,0" VerticalAlignment="Top" Width="111" Foreground="#FF20FF0A" />
- <Label x:Name="lblviolet" Content="" HorizontalAlignment="Left" Height="25" Margin="262,166,0,0" VerticalAlignment="Top" Width="111" Foreground="#FF903BD6" /> </Grid>
- </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,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace Coloured_label_text {
-
-
-
- public partial class MainWindow: Window {
- public MainWindow() {
- InitializeComponent();
- }
- private void btnred_Click(object sender, RoutedEventArgs e) {
- lblred.Content = "You clicked Red";
- lblblue.Content = String.Empty;
- lblgreen.Content = String.Empty;
- lblviolet.Content = String.Empty;
- }
- private void btnblue_Click(object sender, RoutedEventArgs e) {
- lblred.Content = String.Empty;
- lblblue.Content = "You clicked Blue";
- lblgreen.Content = String.Empty;
- lblviolet.Content = String.Empty;
- }
- private void btngreen_Click(object sender, RoutedEventArgs e) {
- lblred.Content = String.Empty;;
- lblblue.Content = String.Empty;
- lblgreen.Content = "You clicked Green";
- lblviolet.Content = String.Empty;
- }
- private void btnviolet_Click(object sender, RoutedEventArgs e) {
- lblred.Content = String.Empty;;
- lblblue.Content = String.Empty;
- lblgreen.Content = String.Empty;
- lblviolet.Content = "You clicked Violet";
- }
- }
- }
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.
| | | | | | | | | |
Text-to-speech function is limited to 200 characters