WPF RichTextBox control in VB.NET

In this article we will learn how to use RichTextBox control in WPF.
  • 4270

In this article we will learn how to use RichTextBox control in WPF.

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

<Window x:Class="MainWindow"



    Title="MainWindow" Height="350" Width="525">


        <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=
 VerticalAlignment="Center" />


                <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=
 VerticalAlignment="Center" />


                <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" 
="Center" VerticalAlignment="Center" />



                <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 x:Name="selectFontSize" Width="45" Height="23" Margin="2">

                    <ComboBoxItem Content="8" IsSelected="True" />

                    <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" />



            <RichTextBox Height="136" Name="RichTextBox2" Width="289" Canvas.Left="20"Canvas.Top="69" >





Now double click on the button Bold Button and add the following code.


Private Sub makeBold_Click(ByVal sender As System.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles 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.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles 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.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles makeUnderline.Click

        If Not [String].IsNullOrEmpty(RichTextBox2.Selection.Text) Then


        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.ObjectByVal 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, 

        End If

    End Sub

Now run the application and test it.


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.


More Articles

© 2020 DotNetHeaven. All rights reserved.