Mousedown ->show current paragraph(cursor position)

Xancholy

Well-known member
Joined
Aug 29, 2006
Messages
143
Programming Experience
Beginner
I have a richtextbox on a form.

Users will type in text in blocks(paragraphs) separated by a blank line

When user clicks cursor into a particular paragraph...
- I need to trap the entire current paragraph where the cursor is placed


Pseudo:
On mousedown event
messagebox.show(currentPara(currentCursorPosition))


should return entire paragraph text where the cursor is.

Can someone please show me how to code this ?

Thanks for any help
 
Some logic thought and a using a few String class methods is all you need. Use GetCharIndexFromPosition method of RichTextBox to find char index of click. Searching the text only to this click index with LastIndexOf method will give start index of paragraph. Searching the text from click index with IndexOf method will give end index of paragraph. If start index is not found then it must be 0. If end index is not found then it must be the last index of text.

With a start and end index you can use the Substring method to get the paragraph. Do one more test to validate the result, see if result contains a separator string. Logically if user click in the middle of a separator string you will get an erroneous result consisting of two paragraphs, it would not be correct to return a result when user hasn't clicked a paragraph.

Here is an example function that is implemented as described:
VB.NET:
Private Function GetParagraph(ByVal text As String, ByVal separator As String, ByVal index As Integer) As String
    If text.Length = 0 Then Return Nothing
    [COLOR="Green"]'indexing[/COLOR]
    Dim startIndex As Integer = text.Remove(index).LastIndexOf(separator)
    Dim endIndex As Integer = text.IndexOf(separator, index)
    If startIndex = -1 Then startIndex = 0 Else startIndex += separator.Length
    If endIndex = -1 Then endIndex = text.Length
    [COLOR="green"]'get substring[/COLOR]
    Dim paragraph As String = text.Substring(startIndex, endIndex - startIndex)
    [COLOR="green"]'validate (if given index is in the middle of a separator a valid match is not possible)[/COLOR]
    If paragraph.Contains(separator) Then Return Nothing
    Return paragraph
End Function
Sample MouseDown call:
VB.NET:
Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles RichTextBox1.MouseDown
    Dim index As Integer = Me.RichTextBox1.GetCharIndexFromPosition(e.Location)
    Dim separator As String = vbLf & vbLf
    Dim text As String = Me.RichTextBox1.Text
    Dim paragraph As String = Me.GetParagraph(text, separator, index)
    If paragraph IsNot Nothing Then
        MsgBox(paragraph)
    End If
End Sub
 
I have a question.

When user clicks on the blank line separating paragraphs, how can I amend the code to show the para above the blank line ? Even if there are multiple blank lines separating paras ?
 
Searching for variable length separators is easiest done with Regex, for example finding matches for pattern "\n{2,})" (equiv 2 or more vbLf) will give you indexes of all paragraph separators. Add 0 and text.length to that list and you got start+end index of all paragraphs. Then you can find between which paragraph indexes the given index falls.
 
Back
Top