Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb.net select line in richtextbox

Posted on 2006-10-30
6
Medium Priority
?
1,787 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

963 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