Vb.net 更改文本框中部分文本的颜色
我只知道如何更改字符串的整个文本的颜色,而不是某个部分。如果程序包含print而不是整个文本,我希望程序更改单词print的颜色 这是我的密码:Vb.net 更改文本框中部分文本的颜色,vb.net,Vb.net,我只知道如何更改字符串的整个文本的颜色,而不是某个部分。如果程序包含print而不是整个文本,我希望程序更改单词print的颜色 这是我的密码: Public Class Form1 Private Sub codeTextBox_TextChanged(sender As Object, e As EventArgs) Handles codeTextBox.TextChanged Dim codeInput As String = codeTextBox.Text
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
这是我的解决办法
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
伙计们,我在我的电脑上测试过它真的很有效。:VBA????对于VB.NET,您可能希望查看CodeProject上的FastColor文本框,而不是重新创建轮子和马车…并且,它使用RichTextBox,而不是OP指定的文本框。
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