Selecting a line in a richtextbox

  • Thread starter Thread starter takapenkki
  • Start date Start date
T

takapenkki

Guest
I need the user of my program to be able to select a whole line in a richtext box by clicking on that line. By this I mean that in a multiline richtextbox, clicking in any place would result in the line that was clicked on being selected. How can I do this?
 
As a quick bodge, this seems to work:

Visual Basic:
    Private Sub rtb_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rtb.MouseUp
        Dim intSelPos As Integer
        Dim intSelStart, intSelLength As Integer

        intSelPos = rtb.SelectionStart
        intSelStart = rtb.Text.LastIndexOf(ControlChars.Lf, intSelPos)
        If intSelStart = -1 Then intSelStart = 0
        intSelLength = rtb.Text.IndexOf(ControlChars.Lf, intSelStart + 1) - intSelStart
        If intSelLength < 0 Then intSelLength = rtb.Text.Length - intSelStart

        If intSelStart = 0 Then
            intSelStart = -1
            intSelLength += 1
        End If
        rtb.SelectionStart = intSelStart + 1
        rtb.SelectionLength = intSelLength - 1
    End Sub
 
Back
Top