RTB.Find returns -1

pasensyoso_manigbas

Well-known member
Joined
May 19, 2006
Messages
64
Programming Experience
Beginner
Hi mods,


Just got a couple of question. I have this input

this is a sample case scenario:

Location : aaaaaaaaa bbbbbbbbbbbbbb ccccccccccc dddddddddddddd eeeee ->> 1st line

vbcrlf & vbtab & vbtab & ggggggggg hhhhhhhhh iiiiiiiiiiiiiiiiiiiiiiii ->> 2nd line

If i used

VB.NET:
RTB.Selection=RTB.Find(Location)  ->>returns -1
RTB.SelectionColor=Color.Blue

if I used

VB.NET:
RTB.Find(Location)
RTB.SelectionColor=Color.Blue   ->> nothing happens

Just need to get the correct logic for this one. Please help.
 
Last edited:

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
This changes the text color to blue:

VB.NET:
 Dim locate As String = <your string>
        If RichTextBox1.SelectedText = locate Then
            RichTextBox1.SelectionColor = Color.Blue
        End If
'or
  If RichTextBox1.SelectionLength > 0 Then
            RichTextBox1.SelectionColor = Color.Blue
        End If
 

pasensyoso_manigbas

Well-known member
Joined
May 19, 2006
Messages
64
Programming Experience
Beginner
@newguy

What I've been up to is, when the line exceeds the 1st line, the formatting is void. It didn't return any. I have a function that breaks long phrases into multiple lines. I didn't use any wordwrapping or multiline property for that. Its a function I created to break a line at a desired length. I need to do this to control the formatting of the next lines. The sample output is the one I've posted in "Location" variable.

At first, before I created my own function to break a phrase and just let the RTB do the wordwrapping, it works perfectly fine. But when I used my own function, the problem occurs when it encounters the 2nd line and so on. No probs when a phrase fits the 1st line.

I guess it didn't recognize the vbcrlf and the vbtab during the selection.
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
RichTextBox uses only line feeds (vbLf) for line breaks, this should not come as a surprise to you as it is both documented and otherwise easy to find out.
 

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
Masensyoso_Manigbas, sorry I didn't realize your delima, it appeared like you were missing the syntax to change the text color when selected, maybe there wasn't enough info 1st post.
 

pasensyoso_manigbas

Well-known member
Joined
May 19, 2006
Messages
64
Programming Experience
Beginner
@john

Yeah, I indeed change vbcrlf to vbLf. Now this is what happens. I revised my code into something like

VB.NET:
RTB.SelectionStart=RTB.Text.IndexOf(Location)

but it only format the string before the vbLf. The string after the vbLf is still unchanged.

What's wrong?

I did not use the RTB.Find anymore as it returns

InvalidArguments=Value of '-1' is not valid ....



@newguy

Sorry about that. My fault. I'm not stating in details.
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
Find method returns the index where found or -1 if the search string is not found says the help.
but it only format the string before the vbLf
You haven't set SelectionLength, have you? Alternative is to use the Select method to set selection start and length.
 

pasensyoso_manigbas

Well-known member
Joined
May 19, 2006
Messages
64
Programming Experience
Beginner
You mean like this?

VB.NET:
RTB.SelectionStart = RTB.Text.IndexOf(location, RTB.SelectionStart, RTB.SelectedText.Length)

or

VB.NET:
RTB.SelectionStart = RTB.Text.IndexOf(location, RTB.SelectionStart + RTB.SelectedText.Length)

both statement still returns InvalidArguments = Value of '-1' ...

I did something like this also

VB.NET:
RTB.Find(location, (RTB.SelectionStart + RTB.SelectedText.Length), _
                            RichTextBoxFinds.WholeWord)

but it just gives me nothing.

RTB.Text.IndexOf() works, but only until it encountered the vbLf, then the rest of the text after the vbLf remains unchanged.
 
Last edited:

pasensyoso_manigbas

Well-known member
Joined
May 19, 2006
Messages
64
Programming Experience
Beginner
@john

I figured it out already. Thanks for the help. I really appreciate it.

Thanks you so much! Reading you hints over and over really extracts the right way to do things. Thanks a bunch once again.
 
Top Bottom