Bold Italic Underline in .NET RichTextBox (Part 1 Visual Basic)
Bold Italic Underline in .NET RichTextBox (Part 1 Visual Basic)
Bold Italic Underline in .NET RichTextBox (Part 1 Visual Basic)
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.
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.
FontStyle
Regular Bold Italic Underline Strikeout
Indicator Property
None fontObject.Bold fontObject.Italic fontObject.Underline fontObject.Strikeout
Example
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.
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
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)
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.