I have two RichTextBoxes in my vb.net application (rtf1 & rtf2). rtf1 is loaded with the external data. Mostly It contains para split into different sections. The paragraph is a dynamic data, hence the no. of words keep on changing. Now my question is that, I want to select a para from the cluster, copy it and paste it into rtf2.

Manually I did as,

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
End Sub

Then I added brackets to the para contents as:

[para content goes here is a multiline]

    Rtf1.Select(Index, Length)

Here only i could find the starting position and length. without knowing the ending postion, i couldnot specify the number of characters. So I would be ok if anyone suggest me how to select "para content goes here is a multiline" only from the [para content goes here is a multiline]?

Actually the contents of Rtf1 is in the .txt format. But I knew only


so, if i can select the para in the .txt file before reading it into rtf1, it will be more useful. Thanks for your solution in advance

Recommended Answers

All 7 Replies

If your paragraphs are seperated by Crlf's, you can count the number of Clrf's between two sentences to determin a new paragraph.

You will have to read the character before the current character and possibly the character before that too.

You will want to do this by reading the file in character by character.

Its fine that i can find the word in a file. But the problem is how to select only certain words out of it. Also as said before, the data is dynamic with unpredictable number of words in it.

By the way what is Crlf's and Clrf's?

Carrige return line feeds. It reperesents the new line character, aka the enter button.

You will have to read the data in the file one char at a time and pull in what you will need.

There are some useful constants defined in VB. Three of them are vbCr, vbLf and vbCrLf where Cr = carriage return and Lf = line feed. For the following example I created a RichTextBox control then pasted in a few paragraphs. Double clicking on any paragraph will select that paragraph. It does this by starting at the current cursor position (set when you click in the control), scanning backwards for the previous vbLf (or start of text), then scanning forward for the next vbLf (there will always be one because I add one to the end of a local copy of the text.

    Private Sub RichTextBox1_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDoubleClick

        'scan backwords from the cursor position to either the start of text or the start
        'of a paragraph (vbLf)

        Dim cp As Integer = RichTextBox1.SelectionStart     'current cursor position
        Dim ss As Integer = cp                              'selection start        
        Dim se As Integer = cp                              'selection end          

        Dim text As String = RichTextBox1.Text & vbLf       'note the added vbLf

        'find the previous and next line feed characters relative to the cursor position

        ss = InStrRev(text, vbLf, cp) + 1
        se = InStr(Mid(text, ss), vbLf) + ss

        'select the paragraph

        RichTextBox1.SelectionStart = ss - 1
        RichTextBox1.SelectionLength = se - ss

    End Sub

@Reverend Jim: The code seems to be working for me.. but in my application rtf1(RichTextBox) is hidden and so i can't able to point out or work with the cursors. rtf1 is hidden with the contents extracted from xyx.txt file on loading the application. Once the application loads, I need the specific content inside the square brackets ( [] ) alone to be copied to the rtf2 (RichTextBox1). The user doesnot know the alignment of xyx.txt. He sees only the rtf2 text box with the needed contents.

If I understand you, you want to select everything between "[" and "]". If that is the case then you can select just that text by

Dim ss As Integer = InStr(RichTextBox1.Text, "[")
Dim se As Integer = InStr(RichTextBox1.Text, "]")

RichTextBox1.SelectionStart = ss
RichTextBox1.SelectionLength = se - ss - 1


But for this to work you must have "[" and "]" in the text. If you don't it will fail.

Reverend Jim:

Dim ss As Integer = InStr(RichTextBox1.Text, "[")
Dim se As Integer = InStr(RichTextBox1.Text, "]")
RichTextBox1.SelectionStart = ss
RichTextBox1.SelectionLength = se - ss - 1

Wow..! This is what I exactly needed. Multiple thanks Jim.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.