5
Most read
6
Most read
7
Most read
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.

More Related Content

PPTX
Database keys
PPT
Active x control
PPT
Reliable data transfer CN - prashant odhavani- 160920107003
PPTX
Logistic regression with SPSS examples
PPTX
Two pass Assembler
PPTX
1.7. eqivalence of nfa and dfa
PPTX
Network layer - design Issues
PDF
Phases of Compiler
Database keys
Active x control
Reliable data transfer CN - prashant odhavani- 160920107003
Logistic regression with SPSS examples
Two pass Assembler
1.7. eqivalence of nfa and dfa
Network layer - design Issues
Phases of Compiler

What's hot (20)

PPTX
Non- Deterministic Algorithms
PPTX
Looping statements
PPT
Manchester Encoding
PDF
Recursive algorithms
PPTX
SQL Data types and Constarints.pptx
PPT
Visual Basic menu
PPTX
Macro Processor
PPTX
Data Link Control
PPTX
HDLC and Point to point protocol
PPTX
Switch Case in C Programming
PDF
CSMA /CD PPT ON SLIDESHARE
PPT
Hamming codes
PPT
Line drawing algo.
PPTX
Testing of hypothesis - large sample test
PPTX
Methods in java
PPTX
Ch 4 linker loader
PDF
Xml schema
PPTX
Protocols of noiseless
PPT
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
PPTX
Computer graphics basic transformation
Non- Deterministic Algorithms
Looping statements
Manchester Encoding
Recursive algorithms
SQL Data types and Constarints.pptx
Visual Basic menu
Macro Processor
Data Link Control
HDLC and Point to point protocol
Switch Case in C Programming
CSMA /CD PPT ON SLIDESHARE
Hamming codes
Line drawing algo.
Testing of hypothesis - large sample test
Methods in java
Ch 4 linker loader
Xml schema
Protocols of noiseless
fdocuments.in_branch-and-bound-design-and-analysis-of-alogorithm.ppt
Computer graphics basic transformation
Ad

Similar to Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visual Basic.Net - Part 1 (20)

PDF
Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visu...
PPTX
Tugas testing
PPTX
Getting started with the visual basic editor
PPTX
ch 3 of C# programming in advanced programming
PPT
visual programming GDI presentation powerpoint
PPT
Visual programming Chapter 3: GUI (Graphical User Interface)
DOCX
Visual C# 2010
PPSX
PDF
Paste Only Plain Text in RichTextBox Control using Visual Basic.Net
PPTX
Vs c# lecture1
DOCX
The visual studio start page is shown in the figure below
PDF
Unit IV-Checkboxes and Radio Buttons in VB.Net in VB.NET
PDF
Windows Forms For Beginners Part - 3
DOCX
PPTX
Vp lecture1 ararat
PPTX
Windowforms controls c#
PPT
Textbox n label
PDF
Vb%20 tutorial
Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visu...
Tugas testing
Getting started with the visual basic editor
ch 3 of C# programming in advanced programming
visual programming GDI presentation powerpoint
Visual programming Chapter 3: GUI (Graphical User Interface)
Visual C# 2010
Paste Only Plain Text in RichTextBox Control using Visual Basic.Net
Vs c# lecture1
The visual studio start page is shown in the figure below
Unit IV-Checkboxes and Radio Buttons in VB.Net in VB.NET
Windows Forms For Beginners Part - 3
Vp lecture1 ararat
Windowforms controls c#
Textbox n label
Vb%20 tutorial
Ad

Recently uploaded (20)

PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PPTX
Download Adobe Photoshop Crack 2025 Free
PDF
Visual explanation of Dijkstra's Algorithm using Python
PPTX
"Secure File Sharing Solutions on AWS".pptx
PDF
BoxLang Dynamic AWS Lambda - Japan Edition
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PPTX
Computer Software - Technology and Livelihood Education
PDF
Guide to Food Delivery App Development.pdf
PDF
novaPDF Pro 11.9.482 Crack + License Key [Latest 2025]
PDF
Autodesk AutoCAD Crack Free Download 2025
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PPTX
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
PDF
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PPTX
MLforCyber_MLDataSetsandFeatures_Presentation.pptx
PPTX
Tech Workshop Escape Room Tech Workshop
PPTX
Airline CRS | Airline CRS Systems | CRS System
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Download Adobe Photoshop Crack 2025 Free
Visual explanation of Dijkstra's Algorithm using Python
"Secure File Sharing Solutions on AWS".pptx
BoxLang Dynamic AWS Lambda - Japan Edition
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
Computer Software - Technology and Livelihood Education
Guide to Food Delivery App Development.pdf
novaPDF Pro 11.9.482 Crack + License Key [Latest 2025]
Autodesk AutoCAD Crack Free Download 2025
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
How Tridens DevSecOps Ensures Compliance, Security, and Agility
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
Weekly report ppt - harsh dattuprasad patel.pptx
MLforCyber_MLDataSetsandFeatures_Presentation.pptx
Tech Workshop Escape Room Tech Workshop
Airline CRS | Airline CRS Systems | CRS System

Apply Bold, Italic and Underline to Selected Text in a RichtextBox using Visual Basic.Net - Part 1

  • 1. 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
  • 2. 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. 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
  • 4. Coding the Demo 4. Now you can go to the Code Window and make your code to look like the following one.
  • 5. 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
  • 6. 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
  • 7. 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.