将文本的颜色从某个字符更改为richTextBox行的末尾



在我的Winform中,用户选择一个单选按钮。基于这个单选按钮,它可以从txt文件中读取某些行。然后我希望它搜索一个撇号,一旦它找到了撇号,我希望它把撇号后面一行的所有文本都变成绿色(文本是真实的代码,所以它应该像在编译器中注释时那样给文本上色(。

问题:如何确定撇号后一行的文本长度?

这是我迄今为止的代码。。。

Dim indexx As Integer = 0
Dim numOfChars As Integer
While indexx < RichTxtOut.Text.LastIndexOf("'")
numOfChars = 0
'This finds the char index of the first instance of what I am looking for. 
Dim FoundCharIndex As Integer = RichTxtOut.Find("'", indexx, RichTxtOut.TextLength, RichTextBoxFinds.None)
'this finds the line the found char resides in 
Dim LineOfFoundChar As Integer = RichTxtOut.GetLineFromCharIndex(FoundCharIndex)
Dim startCounting As Boolean = False
'count the number of characters after the apostrophe
For li As Integer = 0 To RichTxtOut.Lines(LineOfFoundChar).Count - 1
If RichTxtOut.Lines(LineOfFoundChar).Chars(li) = "'" Then
startCounting = True
End If
If startCounting Then numOfChars += 1
Next
RichTxtOut.Select(FoundCharIndex, numOfChars)
RichTxtOut.SelectionColor = Color.Green
numOfChars = 0
indexx = RichTxtOut.Text.IndexOf("'", indexx) + 1
End While

我解决了这个问题。RichTxtOut.GetLineFromCharIndex((将溢出到2行的行计数为1行。因此,溢出的行越多,就越不正确
这段代码确实有效我需要改变它将行读取到RichTextBox 中的方式

For li As Integer = Eachline + 1 To lines.Count - 1
If lines(li) = Nothing Then
ElseIf lines(li).Chars(0) = "|" Then
Exit For
Else
For lineChar As Integer = 0 To lines(li).Count - 1
RichTxtOut.Text &= lines(li).Chars(lineChar)
If lineChar = 90 Then
RichTxtOut.Text &= vbNewLine & "        →    "
ElseIf lineChar = 167 Then
RichTxtOut.Text &= vbNewLine & "        →    "
End If
Next
End If
RichTxtOut.Text &= vbNewLine
Next

最新更新