如何根据字符串的前缀为RichTextBox的文本着色?

时间:2015-11-07 06:30:33

标签: vb.net richtextbox

我想为richtextbox中的所有字符串着色。例如,如果特定字符串的前缀是

  1. Received:然后它应该是蓝色
  2. Send:然后它应该是红色的
  3. Info:然后它应该是绿色
  4. *The way I output the text in the RichTextBox is by ascending order, it means that all the newest messages will be outputted at the top of the RichTextBox, the old ones will go down.

    截图:

    enter image description here

    代码:

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim msg_info As String = Nothing
    
            If RB_Info.Checked = True Then
                msg_info = "Info: "
            End If
            If RB_Send.Checked = True Then
                msg_info = "Send: "
            End If
            If RB_Received.Checked = True Then
                msg_info = "Received: "
            End If
    
            RichTextBox1.Text = RichTextBox1.Text.Insert(0, msg_info & TextBox1.Text & ControlChars.NewLine)
        End Sub
    End Class
    

    修改:我已尝试应用此子版,但它会更改ListBox中所有项的颜色

    Sub HighlightPhrase(box As RichTextBox, phrase As String, color As Color)
            Dim pos As Integer = box.SelectionStart
            Dim s As String = box.Text
            Dim ix As Integer = 0
            While True
                Dim jx As Integer = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase)
                If jx < 0 Then
                    Exit While
                End If
                box.SelectionStart = jx
                box.SelectionLength = phrase.Length
                box.SelectionColor = color
                ix = jx + 1
            End While
            box.SelectionStart = pos
            box.SelectionLength = 0
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim msg_info As String = Nothing
    
            If RB_Info.Checked = True Then
                msg_info = "Info: "
                HighlightPhrase(RichTextBox1, "Info", Color.Blue)
            End If
            If RB_Send.Checked = True Then
                msg_info = "Send: "
                HighlightPhrase(RichTextBox1, "Send", Color.Green)
            End If
            If RB_Received.Checked = True Then
                msg_info = "Received: "
                HighlightPhrase(RichTextBox1, "Received", Color.Red)
            End If
            RichTextBox1.Text = RichTextBox1.Text.Insert(0, msg_info & TextBox1.Text & ControlChars.NewLine)
        End Sub
    

1 个答案:

答案 0 :(得分:1)

我已经编辑了我的解决方案,因此新行显示在RichTextBox的顶部。

我认为我的解决方案很难看,因为基本上它在添加新行后重新绘制每行的颜色。想象一下,如果RichTextBox有数千行。我认为你可以通过限制RTB中有多少行来限制这一点,例如100行。

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim msg_info As String = Nothing

        If RB_Info.Checked = True Then
            msg_info = "Info: "
        End If
        If RB_Send.Checked = True Then
            msg_info = "Send: "
        End If
        If RB_Received.Checked = True Then
            msg_info = "Received: "
        End If

        RichTextBox1.Text = RichTextBox1.Text.Insert(0, msg_info & TextBox1.Text & ControlChars.NewLine)
        ChangeColor()

    End Sub

    Private Sub ChangeColor()

        Dim lines = RichTextBox1.Text.Split(vbLf)
        Dim startPos As Integer, endPos As Integer = -1
        Dim myColor As Color

        For i = 0 To lines.Length - 2 ' minus 2 because the last one is empty string
            startPos = endPos + 1
            endPos = startPos + lines(i).Length
            RichTextBox1.Select(startPos, endPos)

            If lines(i).StartsWith("Info: ") Then myColor = Color.Red
            If lines(i).StartsWith("Send: ") Then myColor = Color.Blue
            If lines(i).StartsWith("Received: ") Then myColor = Color.Green

            RichTextBox1.SelectionColor = myColor
        Next

    End Sub

End Class

使用ListView的替代解决方案

您可能希望查看ListView控件。如果您不想在列表中写任何内容,我建议您使用ListView替换RichTextBox。添加ListView后,将View属性更改为List

enter image description here

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim msg_info As String = Nothing
        Dim myColor As Color

        If RB_Info.Checked = True Then
            msg_info = "Info: "
            myColor = Color.Green
        End If
        If RB_Send.Checked = True Then
            msg_info = "Send: "
            myColor = Color.Red
        End If
        If RB_Received.Checked = True Then
            msg_info = "Received: "
            myColor = Color.Blue
        End If

        Dim li = New ListViewItem()
        li.ForeColor = myColor
        li.Text = msg_info & TextBox1.Text
        ListView1.Items.Insert(0, li)

    End Sub

End Class
相关问题