Solved

vb.net select line in richtextbox

Posted on 2006-10-30
6
1,684 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 86

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

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

Suggested Solutions

Title # Comments Views Activity
XML error not understood 5 33
Asp.Net Session Question 2 49
VB Catch Statement in Event Viewer. 2 20
Make borderless form movable by user 2 27
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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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