我的用户界面中有一个只读的RichTextBox。我想这样做,当我用鼠标点击一行文本时,它选择或突出显示整行。只是点击了一行。
你是怎么做到的?
答案 0 :(得分: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)
然后你需要找出它结束的地方,即下一行的开头减1:
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
答案 1 :(得分:0)
只需在点击事件处理程序
中使用以下代码即可SendKeys.Send("{HOME}+{END}")