【问题标题】:Send cursor / caret to next paragraph in RichTextBox将光标/插入符号发送到 RichTextBox 中的下一段
【发布时间】:2020-12-24 15:04:37
【问题描述】:

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

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

【问题讨论】:

    标签: vb.net winforms richtextbox


    【解决方案1】:

    你可以使用:

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

    这使用IndexOf() 来获取下一个换行符的位置,它揭示了下一段的位置。如果IndexOf()返回-1,我们将位置设置在字符串的末尾,以模拟Ctrl+的行为。

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

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

    【讨论】:

    • @yinkajewole 不是。 IIf() 仅用于向后兼容。您应该始终使用If(),因为它使用短路评估(而IIf()evaluates both sides)。您可以在以下帖子中了解更多关于 If()IIf() 之间区别的信息:123
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 2022-08-15
    • 2012-05-09
    • 1970-01-01
    相关资源
    最近更新 更多