Solved

vb.net select line in richtextbox

Posted on 2006-10-30
6
1,626 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

17 Experts available now in Live!

Get 1:1 Help Now