Adrian
Adrian

Reputation: 2875

VB.NET, which textbox line was that you clicked on?

I'm trying to figure out if this is even possible. When someone clicks (or actually, double clicks) in my multi-line text box, I'd love to be able to figure out which of the lines[] items they double clicked on. I know I have the lines[] array as one of the properties of my windows forms textbox, and that's kind of cool, but when they double click is there anything that will tell me which index of that was under their mouse at the time?

Upvotes: 2

Views: 2152

Answers (1)

Hans Passant
Hans Passant

Reputation: 941357

Use the GetCharIndexFromPosition() method to find the character that was clicked. Then GetLineFromCharIndex() to get the line that contains that characters. Like this:

Private Sub TextBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As MouseEventArgs) Handles TextBox1.MouseDoubleClick
    Dim pos = TextBox1.GetCharIndexFromPosition(e.Location)
    Dim line = TextBox1.GetLineFromCharIndex(pos)
    Debug.Print("You double-clicked line #{0}", line + 1)
End Sub

Upvotes: 7

Related Questions