Link to home
Start Free TrialLog in
Avatar of iscontact
iscontact

asked on

vb.net select line in richtextbox

I need to be able to select all of the text in a richtextbox on the line that the user doubleclicks.  However, thus far I have been unable to determine exactly how to do this.

I have experimented with the following code:

        Dim start As Integer = txtNotes.SelectionStart
        Dim len As Integer = txtNotes.SelectionLength

        txtNotes.Select(start, len)

This is only selecting the word that the user clicks on, and not the entire line as is needed.

For reference, I am working in vb.net 2005.
Avatar of gangwisch
gangwisch

dim selectlinenumber as integer=2
dim lines() as string=split(txtnotes.text,vbcrlf)
txtNotes.SelectionStart=getselectionstart(lines,selectlinenumber)
txtNotes.SelectionLength=lines(selectlinenumber-1).length

function getselectionstart(lines() as string,linenumber as integer) as integer
dim total as integer=0
for i as integer=0 to linenumber-1
total+=lines(i).length
next
return total
end function
Avatar of iscontact

ASKER

Here is what I have based on your suggestion:

   Private Sub txtNotes_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNotes.DoubleClick
        Dim selectlinenumber As Integer = 2
        Dim lines() As String = Split(txtNotes.Text, vbCrLf)
        txtNotes.SelectionStart = getselectionstart(lines, selectlinenumber)
        txtNotes.SelectionLength = lines(selectlinenumber - 1).Length
    End Sub

    Function getselectionstart(ByVal lines() As String, ByVal linenumber As Integer) As Integer
        Dim total As Integer = 0
        For i As Integer = 0 To linenumber - 1
            total += lines(i).length
        Next
        Return total
    End Function

However, I receive an error stating that the index (i) in the function is outside the bounds of the array.
I have noticed, upon experimentation, that the problem seems to be data related with regards to this method.  The issue is that there is indeed a line break between the items in the database (which is SQL server).  However, the data was actually entered in the database by a VB6 program so I do not know what line break character to look for in the data.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent suggestion Idle.  I have one more items in relation to this that needs to be addressed, however.  Once the line has been selected, how do I retrieve the value of the text selected?
With Idle's example, I have been able to answer my last question, I just needed to work through it a little closer.  Thanks for the help.