Click here to Skip to main content
14,984,060 members
Articles / Programming Languages / XML
Alternative
Article
Posted 25 Feb 2019

Tagged as

Stats

7.2K views
2 bookmarked

Append Multiple Colored Texts to a RichTextBox using VB.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Feb 2019CPOL
This is an alternative for "Multiple Colored Texts in RichTextBox using C#"

Introduction

This VB.NET VS2013 code snippet can append colored text to a rich text box. Each appended text can get its own color.

Background

I want to use the multiple colored text box in an application I am writing for checking the free space on a disk for bad sectors.

Today, I tried a lot of code examples on the Internet to append several red messages intertwined with black comment texts into a rich text box. This took me much longer than I hoped it would, probably because I missed the use of the word font everywhere. By following the original article for C#, I finally found a working solution.

Using the Code

The XML comments of subroutine appendColoredTextToRichTextBox tells how to use it.

VB.NET
''' <summary>
''' Appends colored text to a rich text box.
''' </summary>
''' <param name="appendBox">
''' The rich text box to append to.
''' </param>
''' <param name="inText">
''' The text to append.
''' </param>
''' <param name="inColor">
''' The color to display the appended text in.
''' </param>
''' <param name="insertNewLine">
''' True means a new line is to be inserted
''' before appending the new text.
''' </param>
''' <param name="updateViewNow">
''' True means the appended text is to be seen immediately.
''' </param>
''' <remarks>
''' Language: VB.NET VS2013, February 23 2019, EAHMK.
''' 1, 2, 3 and 4 stem from
''' Multiple Colored Texts in RichTextBox using C#
''' by S.Vinothkumar, 27 Jun 2009.
''' </remarks>
Public Sub appendColoredTextToRichTextBox( _
        appendBox As Windows.Forms.RichTextBox, _
        inText As String, _
        inColor As Drawing.Color, _
        Optional insertNewLine As Boolean = False, _
        Optional updateViewNow As Boolean = False)
    Call appendBox.SuspendLayout()
    Dim appendBoxFont As Drawing.Font = appendBox.Font ' 1
    If insertNewLine Then
        Call appendBox.AppendText(Environment.NewLine) ' 4
    End If
    appendBox.SelectionStart = appendBox.TextLength
    appendBox.SelectionLength = 0
    appendBox.SelectionFont = appendBoxFont ' 2
    appendBox.SelectionColor = inColor ' 3
    appendBox.SelectedText = inText ' 4
    Call appendBox.ResumeLayout()
    If updateViewNow Then
        Call appendBox.Update()
    End If
End Sub

I call appendColoredTextToRichTextBox for error messages like this:

VB.NET
Private Sub foutBericht(ex As Exception, _
        Optional toonMeteen As Boolean = False, _
        Optional ByVal kleur As Drawing.Color = Nothing)
    If kleur.Equals(Drawing.Color.Empty) Then
        kleur = Drawing.Color.Red
    End If
    Call appendColoredTextToRichTextBox( _
        Me.RichTextBox1, ex.Message, kleur, True, toonMeteen)
End Sub

I call appendColoredTextToRichTextBox for comment texts like this:

VB.NET
Private Sub commentaarTekst(tekst As String, _
        Optional toonMeteen As Boolean = False, _
        Optional ByVal kleur As Drawing.Color = Nothing)
    If kleur.Equals(Drawing.Color.Empty) Then
        kleur = Drawing.Color.Black
    End If
    Call appendColoredTextToRichTextBox( _
        Me.RichTextBox1, tekst, kleur, True, toonMeteen)
End Sub

Points of Interest

Move the following code block 4 lines down and see how all colors will become the same.

VB.NET
If insertNewLine Then
    Call appendBox.AppendText(Environment.NewLine) ' 4
End If

History

I hope to post improvements and additions to this post.

I might turn my application for checking the free space on a disk for bad sectors into a web application.

History

  • 25th February, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

EAHMK
Retired
Netherlands Netherlands
No Biography provided

Comments and Discussions

 
QuestionColoredListBox Pin
Heliwave26-Feb-19 19:33
MemberHeliwave26-Feb-19 19:33 
QuestionIs this WinForm or WPF? Pin
Dong Xie26-Feb-19 1:48
MemberDong Xie26-Feb-19 1:48 
PraiseRe: Is this WinForm or WPF? Pin
vbjay.net26-Feb-19 3:43
Membervbjay.net26-Feb-19 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.