更改文本框中文本的一部分的颜色

时间:2016-02-13 23:46:16

标签: vb.net

我只知道如何更改字符串整个文本的颜色,而不是某个部分。我希望程序改变单词的颜色" print&#34 ;,如果程序包含" print"而不是整篇文章。

这是我的代码:

Public Class Form1
    Private Sub codeTextBox_TextChanged(sender As Object, e As EventArgs) Handles codeTextBox.TextChanged
        Dim codeInput As String = codeTextBox.Text
        If codeInput.Contains("print") Then
            codeTextBox.ForeColor = Color.Blue
        End If
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

这是我的解决方案

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        If RichTextBox1.Text.EndsWith("Dim") Then
            RichTextBox1.Select(RichTextBox1.TextLength - 3, 3)
            RichTextBox1.SelectionColor = Color.Red
            RichTextBox1.Select(RichTextBox1.TextLength, RichTextBox1.TextLength)
            RichTextBox1.SelectionColor = Color.Black
        End If
    End Sub

他真的有效,我在我的电脑上测试过。 :)

答案 1 :(得分:-1)

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim text As String = RichTextBox1.Text
    text = text.Replace(TextBox1.Text, "<font color=blue> " & TextBox1.Text & " </font>")
    WebBrowser1.DocumentText = text
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub
End Class

screenshot of form

相关问题