0

我的用户界面中有一个只读的 RichTextBox。我想这样做,以便当我用鼠标单击一行文本时,它会选择或突出显示整行。只是被点击的那一行。

你怎么做到这一点?

4

2 回答 2

3

RichTextBox 有你需要的所有方法,你只需要其中的多个。首先,您需要将鼠标位置映射到字符索引:

Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim box = DirectCast(sender, RichTextBox)
    Dim index = box.GetCharIndexFromPosition(e.Location)

然后您需要将字符索引映射到一行:

    Dim line = box.GetLineFromCharIndex(index)

然后您需要找出该行的开始位置:

    Dim lineStart = box.GetFirstCharIndexFromLine(line)

然后你需要找出它在哪里结束,也就是下一行的开始减一:

    Dim lineEnd = box.GetFirstCharIndexFromLine(line + 1) - 1

然后你需要做出选择:

    box.SelectionStart = lineStart
    box.SelectionLength = lineEnd - lineStart

总结:

Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles RichTextBox1.MouseDown
    Dim box = DirectCast(sender, RichTextBox)
    Dim index = box.GetCharIndexFromPosition(e.Location)
    Dim line = box.GetLineFromCharIndex(index)
    Dim lineStart = box.GetFirstCharIndexFromLine(line)
    Dim lineEnd = box.GetFirstCharIndexFromLine(line + 1) - 1
    box.SelectionStart = lineStart
    box.SelectionLength = lineEnd - lineStart
End Sub
于 2012-05-24T23:18:40.877 回答
0

只需在单击事件处理程序中使用以下代码

SendKeys.Send("{HOME}+{END}")
于 2012-05-24T22:42:08.113 回答