apply bold, italic and underline to selected text in a richtextbox using visual basic.net - part 1
DESCRIPTION
Part I - How to combine bold, italic and underline fontstyles to a selected text in a rich textboxTRANSCRIPT
How To:
Apply Bold, Italic and Underline
Formatting to Selected Text in a .NET
RichTextBox (Part 1)
© DanieldotNET DEC2013
How to programmatically apply bold, italic or underline formatting to selected text
in a RichTextBox control.
Software Version: Visual Basic 2008 Express Edition, .NET 3.5
Contents
Introduction
The RichTextBox SelectionFont Property
Style Indicator Properties
Building the Demo Application
Coding the Buttons
Running the Demo Application
A Little Problem
Introduction
When creating a simple word processing application- which of course will
make use of the RichTextBox control – one may also want to include
common text formatting capabilities like Bold, Italic and Underline. Just how
this could be done is the subject of this two-part article.
The RichTextBox SelectionFont Property
This property in the RichTextBox control allows the programmer to retrieve,
or modify, the font of the highlighted text. When the font of a highlighted
text is modified, any other text typed immediately after the selected text will
also have the font applied to the previously selected text.
Using the RichTextBox.SelectionFont Property, the font style (Bold, Italic,
etc), the font size, and the font face of highlighted text within the control can
be modified. However, the only way to change the font style of the selected
text is to create a new font object. This is because the Style property of the
Font Class is ReadOnly – meaning once a Font object is created, there is no
way to change the style of the Font. If the text is to have another font style,
a new font object with the required style must be created.
The Style Indicator Properties
Once the Font object is constructed, it is possible to get information about
the style settings of the font. This is done by using the indicator properties
defined in the font object that correspond to each style. The table below
summarizes the Font style values defined in the FontStyle enumeration. The
FontStyle Enumeration is used by the Font class to specify style information
applied to text.
FontStyle Indicator Property Example
Regular None RichTextBox
Bold fontObject.Bold RichTextBox
Italic fontObject.Italic RichTextBox
Underline fontObject.Underline RichTextBox
Strikeout fontObject.Strikeout RichTextBox
An Indicator Property is a ReadOnly Boolean property defined in the Font
class that returns true if the specified style is set, otherwise it returns false.
For example, the indicator property for style bold is fontObject.Bold. if
fontObject has style Bold, calling fontObject.Bold will return True, otherwise
it will return False. Notice that FontStyle Regular has no corresponding
Indicator Function. To check if fontObject has the Regular style, all the other
indicators should be false.
Building the Demo Application
This Demo Application allows the user apply any of the three styles Bold, Italic or
Underline to highlighted text within a RichTextBox control. It can also toggle a Font
style off/on.
1. Create a new Windows Application named BoldItalicUnderline1.
2. Put three buttons and a RichTextBox control on Form1. Your interface
should resemble the one below.
3. Next, in the Properties Window, change the properties of the
controls as follows:
Button1 Text = Bold
Button2 Text = Italic
Button3 Text = Underline
RichTextBox1 Font.Size = 14
Form1 Text = Bold Italic Underline Demo 1
The interface should look like the following image
Coding the Demo
4. Now you can go to the Code Window and make your code to look like
the following one.
Visual Basic
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
' Check if the selected text is Boldened
If RichTextBox1.SelectionFont.Bold = True Then
' Change it to Regular Font
RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
Else
' Change it to Bold Font
RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, FontStyle.Bold)
End If
' Set input focus on the RichTextBox
RichTextBox1.Focus()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button2.Click
' Check if the selected text is Italicized
If RichTextBox1.SelectionFont.Italic = True Then
' Change it to Regular font
RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
Else
' Change it to Italic font
RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, FontStyle.Italic)
End If
' Set input focus on the RichTextBox
RichTextBox1.Focus()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button3.Click
' Check if the selected text is Underlined
If RichTextBox1.SelectionFont.Underline = True Then
' Change it to Regular font
RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
Else
' Change it to Underlined font
RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, FontStyle.Underline)
End If
' Set input focus on the RichTextBox
RichTextBox1.Focus()
End Sub
End Class
The event handling methods for buttons 1, 2 and 3 are similar, so the following
explanation applies for all three methods.
When the “Bold” button is clicked, the event handler first checks if the selected font
is Bold by using the following condition in the if-statement:
Visual Basic
RichTextBox1.SelectionFont.Bold = True
If it evaluates to “True”, it means that the selected text is bold. It then removes the
Bold formatting by applying a regular font style to it as shown below:
Visual Basic
RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
On the other hand, if the selected text is not bold, it applies a bold formatting in a
way similar to the way it removed it. That is, by creating a new Font object having
the font style FontStyle.Bold
Visual Basic
RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont, FontStyle.Bold)
Running the Demo
Press the F5 key to test the demo. Type any text into the RichTextBox. Then, select
any and apply the desired font style to it by clicking any of the buttons.
The figure shows the text
“The quick brown fox”
formatted as
“The quick brown fox”
To do it, select “quick” and press
button bold
Then, select “brown” and press
button italic
Finally, select the text “fox” and
press button underline
A Little Problem…
This demo only showed how to apply a single font style formatting to selected text
in a RichTextBox. However, the above demo cannot apply more than one font style
format to the selected text. Just try selecting any text and click bold. If italic is
clicked next, it cancels the bold formatting. Part 2 of this article will discuss how to
solve this problem.