In this article we will learn how to use RichTextBox control in Silverlight 4.
RichTextBox control
The RichTextBox control that supports rich formatting, automatic line wrapping, HTML and RTF import/export, table support, images and more.
Properties : This control has the following properties.
Figure 1.
For example
Taking some control on the form. The form looks like this.
Figure 2.
XAML code
<UserControl x:Class="SilverlightApplication40.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="White">
<StackPanel Orientation="Horizontal" Canvas.Left="10" Canvas.Top="20" Height="30">
<Button x:Name="makeBold" Width="24" Height="23" Margin="10, 2, 2, 2"ToolTipService.ToolTip="Bold">
<TextBlock x:Name="boldText" Text="B" FontFamily="Arial" FontSize="14"FontWeight="Bold" HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button>
<Button x:Name="makeItalic" Width="24" Height="23" Margin="2"ToolTipService.ToolTip="Italic">
<TextBlock x:Name="italicText" Text="I" FontFamily="Arial" FontSize="14"FontStyle="Italic" HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button>
<Button x:Name="makeUnderline" Width="24" Height="23" Margin="2"ToolTipService.ToolTip="Underline">
<TextBlock x:Name="underlineText" Text="U" FontFamily="Arial" FontSize="14"TextDecorations="Underline"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button>
<ComboBox x:Name="selectFontFamily" Width="155" Height="23" Margin="10, 2, 2, 2">
<ComboBoxItem Content="Arial" FontSize="14" FontFamily="Arial" IsSelected="True" />
<ComboBoxItem Content="Arial Black" FontSize="14" FontFamily="Arial Black" />
<ComboBoxItem Content="Comic Sans MS" FontSize="14" FontFamily="Comic Sans MS" />
<ComboBoxItem Content="Courier New" FontSize="14" FontFamily="Courier New" />
<ComboBoxItem Content="Lucida Grande" FontSize="14" FontFamily="Lucida Grande" />
<ComboBoxItem Content="Lucida Sans Unicode" FontSize="14" FontFamily="Lucida Sans Unicode" />
<ComboBoxItem Content="Times New Roman" FontSize="14" FontFamily="Times New Roman" />
<ComboBoxItem Content="Trebuchet MS" FontSize="14" FontFamily="Trebuchet MS" />
<ComboBoxItem Content="Verdana" FontSize="14" FontFamily="Verdana" />
</ComboBox>
<ComboBox x:Name="selectFontSize" Width="45" Height="23" Margin="2">
<ComboBoxItem Content="8" IsSelected="True" />
<ComboBoxItem Content="9" />
<ComboBoxItem Content="10" IsSelected="True" />
<ComboBoxItem Content="11" />
<ComboBoxItem Content="12" />
<ComboBoxItem Content="14" />
<ComboBoxItem Content="16" />
<ComboBoxItem Content="18" />
<ComboBoxItem Content="20" />
<ComboBoxItem Content="22" />
<ComboBoxItem Content="24" />
<ComboBoxItem Content="26" />
<ComboBoxItem Content="28" />
<ComboBoxItem Content="36" />
<ComboBoxItem Content="48" />
<ComboBoxItem Content="72" />
</ComboBox>
</StackPanel>
<RichTextBox Height="136" Name="RichTextBox2" Width="289" Canvas.Left="27"Canvas.Top="115" />
</Canvas>
</UserControl>
Now double click on the button Bold Button and add the following code.
Private Sub makeBold_Click(ByVal sender As System.Object, ByVal e AsSystem.Windows.RoutedEventArgs) Handles makeBold.Click
If Not [String].IsNullOrEmpty(RichTextBox2.Selection.Text) Then
RichTextBox2.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold)
End If
End Sub
Now double click on the button Italic Button and add the following code.
Private Sub makeItalic_Click(ByVal sender As System.Object, ByVal e AsSystem.Windows.RoutedEventArgs) Handles makeItalic.Click
If Not [String].IsNullOrEmpty(RichTextBox2.Selection.Text) Then
RichTextBox2.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic)
End If
End Sub
Now double click on the button Underline Button and add the following code.
Private Sub makeUnderline_Click(ByVal sender As System.Object, ByVal e AsSystem.Windows.RoutedEventArgs) Handles makeUnderline.Click
If Not [String].IsNullOrEmpty(RichTextBox2.Selection.Text) Then
RichTextBox2.Selection.ApplyPropertyValue(Run.TextDecorationsProperty,TextDecorations.Underline)
End If
End Sub
Now double click on the button Font Button and add the following code.
Private Sub selectFontSize_SelectionChanged(ByVal sender As System.Object, ByVal e AsSystem.Windows.Controls.SelectionChangedEventArgs)
Handles selectFontSize.SelectionChanged
If RichTextBox2 IsNot Nothing AndAlso RichTextBox2.Selection.Text.Length > 0 Then
RichTextBox2.Selection.ApplyPropertyValue(Run.FontFamilyProperty, NewFontFamily(TryCast(selectFontSize.SelectedItem,
ComboBoxItem).Tag.ToString()))
End If
End Sub
Now save and run the application.
Figure 3.
Now select some word and click on the B Button that means Bold.
Figure 4.
Now again select and press Italic Button.
Figure 5.
Now again select and press Underline Button Button.
Figure 6.