我使用了这段代码(source = youtube)
它工作正常,但唯一的问题是它没有突出显示行之后的第一个字符(第0行)
我的代码:
If e.KeyCode = Keys.Space Or e.KeyCode = Keys.Back Or e.KeyCode = Keys.Enter Then
Dim selectionlength As Integer = RichTextBox1.SelectionLength
Dim selectionstart As Integer = RichTextBox1.SelectionStart
Dim letter As String = String.Empty
Do Until letter = " " Or RichTextBox1.SelectionStart = 0
RichTextBox1.SelectionStart -= 1
RichTextBox1.SelectionLength += 1
letter = RichTextBox1.Text.Substring(RichTextBox1.SelectionStart, 1)
Loop
If RichTextBox1.SelectedText = "Hello" Or RichTextBox1.SelectedText = " Hello" Then
RichTextBox1.SelectionColor = Color.HotPink
ElseIf RichTextBox1.SelectedText = "Dinesh" Or RichTextBox1.SelectedText = " Dinesh" Then
RichTextBox1.SelectionColor = Color.Peru
Else
RichTextBox1.SelectionColor = Color.White
End If
RichTextBox1.SelectionStart = selectionstart
RichTextBox1.SelectionLength = 0
RichTextBox1.SelectionColor = Color.White
End If
输出为:Screenshot
您可以看到附加屏幕截图的输出。
绿色箭头(由我绘制)指出单词 Hello , Dinesh 未突出显示。
我使用了多种技术,比如我最喜欢的Fastcolored文本框,但我想使用这个非常简单的方法,因为我的程序不是很大。
请告诉我一些我需要做出改变的事情。
您可以在vb.net或c#中搜索。
提前致谢。
这是窗口式应用程序而不是WPF
答案 0 :(得分:1)
您的问题是Do Until letter = " " Or RichTextBox1.SelectionStart = 0
使用调试器并查看值的简单步骤将很快揭示这一点。它会告诉你(使用单词Hello)当selectionstart为0时循环结束但你仍然需要再次处理该循环,因为在它回到0时你只在e上。解决方法是将其更改为Loop Until。
Do
RichTextBox1.SelectionStart -= 1
RichTextBox1.SelectionLength += 1
letter = RichTextBox1.Text.Substring(RichTextBox1.SelectionStart, 1)
Loop Until letter = " " Or letter Or = vbLf Or RichTextBox1.SelectionStart = 0
现在你遇到的下一个问题是,在第二行,你不会达到0“”但是你想在那里结束搜索所以你需要包含一个vbLf的测试(当有人点击进入时发生换行符转移到新的一行。
现在您有了一个新的停止点,然后您需要在支票中包含更改颜色
If RichTextBox1.SelectedText = "Hello" OrElse RichTextBox1.SelectedText = " Hello" OrElse RichTextBox1.SelectedText = vbLf & "Hello" Then
RichTextBox1.SelectionColor = Color.HotPink
ElseIf RichTextBox1.SelectedText = "Dinesh" OrElse RichTextBox1.SelectedText = " Dinesh" OrElse RichTextBox1.SelectedText = vbLf & "Dinesh" Then
RichTextBox1.SelectionColor = Color.Peru
Else
正如您所看到的,当您向其添加新命令和规则时,这将成为一个巨大的代码块。例如,您有标点符号问题。在以下示例中,此代码不会为Dinesh着色:
Hello is your name Dinesh?
因为?
我看到代码格式化的另一种方法是扫描用户所在的当前行与已知关键字列表,然后根据您找到的关键字的格式规则格式化该行。因为有一种搜索部分文本的方法(即在Dinesh中找到Dinesh?),这将减少代码。
编辑:另外,忘了提。如果第一个字符是Space,Enter或Backspace,则此代码将崩溃。你知道为什么吗?这是一个简单的修复,所以我会留给你解决。
答案 1 :(得分:0)
这将解决您的问题:
If e.KeyCode = Keys.Space Or e.KeyCode = Keys.Enter Then
Dim selstrt As Integer = RichTextBox1.SelectionStart
Dim charc As String = String.Empty
Dim first = RichTextBox1.GetFirstCharIndexOfCurrentLine
Do Until charc = " " Or RichTextBox1.SelectionStart = 0 OrElse RichTextBox1.SelectionStart = first
RichTextBox1.SelectionStart = RichTextBox1.SelectionStart - 1
RichTextBox1.SelectionLength = RichTextBox1.SelectionLength + 1
charc = RichTextBox1.Text.Substring(RichTextBox1.SelectionStart, 1)
Loop
If RichTextBox1.SelectedText = "Hello" Or RichTextBox1.SelectedText = " Hello" Then
RichTextBox1.SelectionColor = Color.HotPink
ElseIf RichTextBox1.SelectedText = "Dinesh" Or RichTextBox1.SelectedText = " Dinesh" Then
RichTextBox1.SelectionColor = Color.LightGreen
Else
RichTextBox1.SelectionColor = Color.White
End If
RichTextBox1.SelectionStart = selstrt
RichTextBox1.SelectionLength = 0
RichTextBox1.SelectionColor = Color.White
End If