65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 VB.NET 将多种颜色的文本附加到 RichTextBox

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.80/5 (2投票s)

2019 年 2 月 25 日

CPOL

2分钟阅读

viewsIcon

15412

这是“使用 C# 在 RichTextBox 中显示多个彩色文本”的替代方案

引言

这个用于 Windows Forms 的 VB.NET 代码片段可以将彩色文本附加到 richtextbox
每个附加的文本都有自己的颜色。

我在 VS2022 上用 2024 的新版本和 2019 的旧版本进行了测试,效果很好。

背景

我将多色文本框用在我的应用程序 checkFreeSpace 中,该程序用于检查我的某个磁盘上的可用空间是否存在坏扇区。

我想将几条红色消息与黑色注释文本交织在一起附加到 richtextbox 中。
通过遵循 C# 的原始文章,我找到了如何做到这一点。

Using the Code

两个子程序 appendColoredTextToRichTextBox 的 XML 注释说明了如何使用它们。

#Region "Colored Texts to a RichTextBox"

    ''' <summary>
    ''' Appends colored text to a rich text box. This is the version of 2024.
    ''' This subroutine is new on January 22 2024.
    ''' This subroutine will most probably function from VS2013 through VS2022.
    ''' </summary>
    ''' <param name="appendBox">
    ''' The rich text box to append to.
    ''' </param>
    ''' <param name="inText">
    ''' The text to append.
    ''' </param>
    ''' <param name="inForeColor">
    ''' The color to display the appended text in.
    ''' The default color is the current forecolor.
    ''' </param>
    ''' <param name="inBackColor">
    ''' The color to display the background of the appended text in.
    ''' The default color is the current backcolor.
    ''' </param>
    ''' <param name="insertNewLine">
    ''' True means a new line is to be inserted before appending the new text.
    ''' </param>
    ''' <param name="appendBoxFont">
    ''' If missing, the current font will be used.
    ''' Else the given font will be used.
    ''' </param>
    ''' <param name="suspendLayout">
    ''' True, which is the default,
    ''' means that applying the new layout of the richtextbox will get cached.
    ''' </param>
    ''' <param name="updateViewNow">
    ''' True means the appended text is to be seen as soon as possible.
    ''' </param>
    ''' <remarks>
    ''' Language: VB.NET VS2022, January 22 2024, EAHMK.
    ''' The original C# solution stems 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,
        Optional inForeColor As Drawing.Color = Nothing,
        Optional inBackColor As Drawing.Color = Nothing,
        Optional insertNewLine As Boolean = False,
        Optional appendBoxFont As Drawing.Font = Nothing,
        Optional suspendLayout As Boolean = True,
        Optional updateViewNow As Boolean = False) ' EAHMK 20240122
        If suspendLayout Then ' EAHMK 20240122
            Call appendBox.SuspendLayout()
        End If
        If IsNothing(appendBoxFont) Then
            appendBoxFont = appendBox.Font
        End If
        If insertNewLine Then
            Call appendBox.AppendText(text:=Environment.NewLine)
        End If
        appendBox.SelectionStart = appendBox.TextLength
        appendBox.SelectionLength = 0
        appendBox.SelectionFont = appendBoxFont
        If Not inForeColor.Equals(obj:=Drawing.Color.Empty) Then ' EAHMK 20240122
            appendBox.SelectionColor = inForeColor
        End If
        If Not inBackColor.Equals(obj:=Drawing.Color.Empty) Then ' EAHMK 20240122
            appendBox.SelectionBackColor = inBackColor
        End If
        appendBox.SelectedText = inText
        If suspendLayout Then ' EAHMK 20240122
            Call appendBox.ResumeLayout()
        End If
        If updateViewNow Then
            Call appendBox.Update()
        End If
    End Sub

    ''' <summary>
    ''' Appends colored text to a rich text box. This is the version of 2019.
    ''' The XML comments have been updated on January 22 2024.
    ''' This subroutine will most probably function from VS2013 through VS2022.
    ''' </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 as soon as possible.
    ''' </param>
    ''' <remarks>
    ''' Language: VB.NET VS2013, February 23 2019, EAHMK.
    ''' The original C# solution stems 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
        If insertNewLine Then
            Call appendBox.AppendText(Environment.NewLine)
        End If
        appendBox.SelectionStart = appendBox.TextLength
        appendBox.SelectionLength = 0
        appendBox.SelectionFont = appendBoxFont
        appendBox.SelectionColor = inColor
        appendBox.SelectedText = inText
        Call appendBox.ResumeLayout()
        If updateViewNow Then
            Call appendBox.Update()
        End If
    End Sub

#End Region ' Colored Texts to a RichTextBox 

示例

我这样调用 appendColoredTextToRichTextBox 来显示错误消息

    Private Sub showErrorMessage(ex As Exception,
        ByRef showBox As RichTextBox,
        Optional showImmediately As Boolean = False,
        Optional colorToShow As Drawing.Color = Nothing) ' EAHMK 20240122
        If colorToShow.Equals(obj:=Drawing.Color.Empty) Then
            colorToShow = Drawing.Color.Red
        End If
        Call appendColoredTextToRichTextBox(appendBox:=showBox,
                inText:=ex.Message, inColor:=colorToShow,
                insertNewLine:=True, updateViewNow:=showImmediately)
    End Sub

我这样调用 appendColoredTextToRichTextBox 来显示注释文本

    Private Sub showCommentMessage(textToShow As String,
        ByRef showBox As RichTextBox,
        Optional showImmediately As Boolean = False,
        Optional colorToShow As Drawing.Color = Nothing) ' EAHMK 20240122
        If colorToShow.Equals(obj:=Drawing.Color.Empty) Then
            colorToShow = Drawing.Color.Black
        End If
        Call appendColoredTextToRichTextBox(appendBox:=showBox,
                inText:=textToShow, inColor:=colorToShow,
                insertNewLine:=True, updateViewNow:=showImmediately)
    End Sub

使用以下代码,您可以测试 2024 和 2019 版本的组合使用。
Me.vermijdRichTextBox 替换为您自己的 rich textbox 的指针。

        Dim exampleRichTextBox As RichTextBox = Me.vermijdRichTextBox ' EAHMK 20240122
        Call appendColoredTextToRichTextBox(appendBox:=exampleRichTextBox,
                inText:="  This is darkblue text with the default backcolor  ",
                inColor:=Drawing.Color.DarkBlue)
        Call appendColoredTextToRichTextBox(appendBox:=exampleRichTextBox,
                inText:="  This is red text on a new line  ", inColor:=Drawing.Color.Red,
                insertNewLine:=True)
        Call appendColoredTextToRichTextBox(appendBox:=exampleRichTextBox,
                inText:="  This is cyan text  ", inColor:=Drawing.Color.Cyan)
        Call appendColoredTextToRichTextBox(exampleRichTextBox, "  This is a new line  ",
                inColor:=Drawing.Color.Black, insertNewLine:=True)
        exampleRichTextBox.SelectionFont = New Font(family:=
                New FontFamily("Courier New"), emSize:=13, style:=FontStyle.Underline,
                unit:=exampleRichTextBox.Font.Unit)
        Call appendColoredTextToRichTextBox(appendBox:=exampleRichTextBox,
                inText:="  This is red underlined text with a monospaced typeface on " &
                "yellow  ", inForeColor:=Drawing.Color.Red, inBackColor:=
                Drawing.Color.Yellow, appendBoxFont:=exampleRichTextBox.SelectionFont)
        Call appendColoredTextToRichTextBox(appendBox:=exampleRichTextBox, inText:=
                "  This is darkblue text with the former backcolor  ",
                inColor:=Drawing.Color.DarkBlue)
        Call appendColoredTextToRichTextBox(appendBox:=exampleRichTextBox, inText:=
                "  This is cyan on green text  ", inForeColor:=Drawing.Color.Cyan,
                inBackColor:=Drawing.Color.Green)
        Call appendColoredTextToRichTextBox(appendBox:=exampleRichTextBox, inText:=
                "  This is a new line with default colors  ", insertNewLine:=True) 

关注点

我不知道 appendColoredTextToRichTextBox 是否也适用于 Windows Presentation Foundation,因为我从未使用过 WPF。如果有人尝试过并获得了积极的结果,请回复。

更改摘要

今天,2024 年 1 月 22 日,我更新了我的文章。已进行以下更改

  • 简介背景 以及 "使用代码" 和 历史记录 中的文本已更新。
  • 已添加区域“Colored Texts to a RichTextBox”。
  • XML 注释部分“1, 2, 3 and 4 stem from ...”已更改为“The original C# solution stems from Multiple Colored Texts in RichTextBox using C# by S.Vinothkumar, 27 Jun 2009.”
  • 参数 updateViewNow 的 XML 文本已更新。
  • 所有参数现在都由参数名称限定。
  • 已添加示例块“示例”。
  • 这两个示例已从块“使用代码”移动到该块。
  • 已添加带有富文本框 exampleRichTextBox 的新示例。
  • Me.vermijdRichTextBox 替换为您自己的富文本框的指针。
  • 此更改列表已添加到块 历史记录
  • 块 History 的名称已更改为“更改摘要”。

已添加 appendColoredTextToRichTextBox 的新版本。
以下文本是关于从 2019 版本到 2024 版本的更改

  • 参数 inColor 已替换为可选参数 inForeColor
  • 已添加可选参数 inBackColor
  • 已添加可选参数 suspendLayout
  • 已删除注释 1、4、2、3 和 4。
  • 荷兰语子程序名称 foutBericht 已更改为 showErrorMessage
  • 荷兰语参数 toonMeteen 已更改为 showImmediately
  • 已删除多余的参数关键字 ByVal
  • 荷兰语参数 kleur 已更改为 colorToShow
  • 富文本框名称 Me.RichTextBox1 已更改为新参数,名为 showBox
  • 荷兰语子程序名称 commentaarTekst 已更改为 showCommentMessage
  • 荷兰语参数 tekst 已更改为 textToShow

历史

  • 2019 年 2 月 25 日:初始版本
© . All rights reserved.