Solved

vb.net select line in richtextbox

Posted on 2006-10-30
6
1,636 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:iscontact
  • 4
6 Comments
 
LVL 10

Expert Comment

by:gangwisch
ID: 17835387
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
0
 

Author Comment

by:iscontact
ID: 17835457
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.
0
 

Author Comment

by:iscontact
ID: 17835548
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 17835781
Try this out...

Public Class TestForm

    Private myRtbHandler As RtbHandler

    Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myRtbHandler = New RtbHandler(Me.txtNotes)
    End Sub

    Private Class RtbHandler
        Inherits NativeWindow

        Private RTB As RichTextBox
        Private Const WM_LBUTTONDBLCLK As Integer = &H203

        Public Event RtbDoubleClicked(ByVal RTB As RichTextBox)

        Public Sub New(ByVal RTB As RichTextBox)
            If Not (RTB Is Nothing) Then
                Me.RTB = RTB
                Me.AssignHandle(RTB.Handle)
            End If
        End Sub

        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            Select Case m.Msg
                Case WM_LBUTTONDBLCLK
                    RTB.Focus()
                    RTB.SelectionLength = 0
                    RTB.SelectionStart = RTB.GetCharIndexFromPosition(RTB.PointToClient(Cursor.Position))
                    Application.DoEvents()
                    SendKeys.Send("{Home}")
                    Application.DoEvents()
                    SendKeys.Send("+{End}")
                    Exit Sub

            End Select

            MyBase.WndProc(m)
        End Sub

    End Class

End Class
0
 

Author Comment

by:iscontact
ID: 17836388
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?
0
 

Author Comment

by:iscontact
ID: 17836595
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now