将光标/插入符号发送到 RichTextBox 中的下一段

Send cursor / caret to next paragraph in RichTextBox

我一直在尝试将 RichTextBox 中的插入符号移动到另一个段落,就像在键盘上按下 Ctrl+向下箭头一样。我使用 SendKeys.Send("^{DOWN}") 但它不一致,有时它似乎按了两次,即跳过一些行。但是当直接从键盘上按下时,移动起来很流畅,不会跳动。

如何使 SendKeys 稳定,或者有其他解决方法吗?

您可以使用:

Dim nextParagraphPos = RichTextBox1.Text.IndexOf(vbLf, RichTextBox1.SelectionStart) + 1
If nextParagraphPos > 0 Then
    RichTextBox1.SelectionStart = nextParagraphPos
Else
    RichTextBox1.SelectionStart = RichTextBox1.TextLength
End If

这使用 IndexOf() 获取下一个 Line-Feed 字符的位置,这揭示了下一段的位置。如果IndexOf() returns -1,我们设置字符串末尾的位置来模拟Ctrl+↓[=23的行为=].

您也可以用这个较短的版本替换 If 语句:

RichTextBox1.SelectionStart = 
    If(nextParagraphPos > 0, nextParagraphPos, RichTextBox1.TextLength)