vb.net 富文本框中的简单文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1683779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
simple text color in rich text box
提问by The Digital Ninja
I can find a million examples of doing reg ex to apply syntax highlighting to a rich text box. but what i need it just a simple way to add in a word of a diffrent color.
我可以找到一百万个使用 reg ex 将语法突出显示应用于富文本框的示例。但我需要的只是一种简单的方法来添加不同颜色的单词。
What would the code be to just put the words "Hello World" into a textbox and have Hello be red and World be green?
将“Hello World”字样放入文本框中并让 Hello 为红色而 World 为绿色的代码是什么?
This code doesnt work.
此代码不起作用。
this.richTextBox1.SelectionColor = Color.Red
this.richTextBox1.text += "Test"
回答by SLaks
Select the text after you put it in and then change the color.
放入后选择文本,然后更改颜色。
For example:
例如:
richTextBox1.Text += "Test"
richTextBox1.Select(richTextBox1.TextLength - 4, 4)
richTextBox1.SelectionColor = Color.Red
回答by Meta-Knight
This code adds text "Hello" in red color and "World" in green to the RichTextBox.
此代码将红色文本“Hello”和绿色文本“World”添加到 RichTextBox。
RichTextBox1.SelectionColor = Color.Red
RichTextBox1.SelectedText = "Hello "
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.SelectedText = "World"
回答by ChaosCoder
Ive worked with it in VB6 and i think its the same: You must select the text and then apply
我在 VB6 中使用过它,我认为它是一样的:您必须选择文本然后应用
this.richTextBox1.SelectionColor = Color.Red
The added text always appears in the defaut color, you must select it and then change its color:
添加的文本始终以默认颜色显示,您必须选择它,然后更改其颜色:
this.richTextBox1.text="Hello world!"
this.richTextBox1.selstart=0
this.richTextBox1.sellength=5
this.richTextBox1.SelectionColor = Color.Red
As i dont use vb.net, you must check the spelling but i think thats the key. The code i wrote is supposed to print "Hello" in red and "World!" in black.
由于我不使用 vb.net,您必须检查拼写,但我认为这是关键。我写的代码应该用红色打印“你好”和“世界!” 黑色。
回答by L1amm
Try this
尝试这个
RichTextBox2.SelectionLength = 0
RichTextBox1.SelectionStart = 0
' We deselect everything first in case the user has something selected.
RichTextBox1.SelectionColor = Color.Red
RichTextBox1.SelectedText = "Hello "
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.SelectedText = "World "
This will add it to the start of the textbox. I think you could also make SelectionStart = RichTextBox1.TextLength which would put it at the end instead of the start.
这会将其添加到文本框的开头。我想你也可以让 SelectionStart = RichTextBox1.TextLength 把它放在最后而不是开始。
回答by Ronan
The code doesn't work:
代码不起作用:
this.richTextBox1.SelectionColor = Color.Red
this.richTextBox1.text += "Test"
Change the second line to this:
将第二行更改为:
this.richTextBox1.SelectionColor = Color.Red
this.richTextBox1.selectedtext = "Test"
回答by Austin Thompson
Try this
尝试这个
Sub colorWord(ByVal word As String, ByVal color As Color) ' by im4dbr0
For i As Integer = 0 To RichTextBox1.TextLength
Try
If RichTextBox1.Text.ElementAt(i).ToString = word.ElementAt(0).ToString Then
Dim found As Boolean = False
For j As Integer = 1 To word.Count - 1
If RichTextBox1.Text.ElementAt(i + j) = word.ElementAt(j) Then
found = True
Else
found = False
Exit For
End If
Next
If found = True Then
RichTextBox1.Select(i, word.Length)
RichTextBox1.SelectionColor = color
End If
End If
Catch ex As Exception
Continue For
End Try
Next
For multiple words use loop
对于多个单词使用循环
Dim Words As New List(Of String)
Words.Add("Its")
Words.Add("That")
Words.Add("Simple")
For i As Integer = 0 To Words.Count - 1
colorWord(Words.Item(i), Color.Red)
Next