ARTICLE

RichTextBox control in WPF using VB.NET

Posted by Rohatash Kumar Articles | WPF using VB.NET January 12, 2011
In this article we will learn how to use RichTextBox control in WPF.
 
Reader Level:

HTML clipboard

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.

rtb1.gif

Figure 1.

For example

Taking some control on the form. The form looks like this.

rtb2.gif

Figure 2.

XAML code

<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

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

    <Grid>

        <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="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="20" Canvas.Top="69" >

                </RichTextBox></Canvas>

    </Grid>

</Window>

 

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

 

Private Sub makeBold_Click(ByVal sender As System.Object, ByVal e As System.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 As System.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 As System.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 As System.Windows.Controls.SelectionChangedEventArgs) Handles selectFontSize.SelectionChanged

        If RichTextBox2 IsNot Nothing AndAlso RichTextBox2.Selection.Text.Length > 0 Then

            RichTextBox2.Selection.ApplyPropertyValue(Run.FontFamilyProperty, New FontFamily(TryCast(selectFontSize.SelectedItem, ComboBoxItem).Tag.ToString()))

        End If

    End Sub

 

Now run the application and test it.

rtb3.gif

Figure 3.

Now select some word and click on the B Button that means Bold.

rtb4.gif

Figure 4.

Now again select and press Italic Button.

rtb5.gif

Figure 5.

Now again select and press Underline Button Button.

rtb6.gif

Figure 6.

Login to add your contents and source code to this article
share this article :
post comment
 

I want to Save and load the context with formatting as bold,italic and underline and show in message box as same formatting as bold,italic and underline.If you any answer pls mail me. Mail id: girivenkat27@gmail.com Thanks and regards Venkatgiri 9769278122

Posted by Venkatgiri Sridharan Feb 10, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Diagram
Become a Sponsor