Question Help needed with RichTextBoxFinds.WholeWord and MatchCase

vb_noob

Member
Joined
Mar 28, 2011
Messages
6
Programming Experience
Beginner
Hi there,

First of all, I'm quite new to VB.NET so would like to request for simple explanations, and apologies if my question is beginner-like.

Here's the situation:
I've got a form that has
- RichTextBox
- OpenButton (to load RTF file)
- SaveButton (to save RTF file after editing)
- Textbox (for search phrase)
- SearchButton
- ExitButton
- 2 RadioButtons (WholeWord and MatchCase search)

All of them are working just the way I want them to EXCEPT search features; I don't know how to write them properly.

At the moment, they search and hightlight ONLY the first word they encounter.
E.g. say I search for word "the" in the document, only the first "the" it encounters is highlighted. The rest is just untouched.

I understand this is because I've only got few lines of simple code as follows
VB.NET:
If rtfText.Find(txtSearch.Text, RichTextBoxFinds.WholeWord) = -1 Then
    MessageBox.Show("No results returned. Please try again.", "No matching results.", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
I did try a few other snippets I found on the Internet including While loop but they didn't work as I wanted them to, and I couldn't understand them either.

What I want is, which ever search option is used, when the matchig result is found; it will highlight that word and when I click it again it will move onto another word. If I keep clicking, it will highlight the same words (but located in different areas) throughout the whole document and at the end, it will send back to the first matching word.
Just like Adobe Reader search.

Is it possible? Please help me out and I would be most appreciated if you could provide me a simple solution. This is not for professional use so as long as it works, I'd be happy :)

Thank you very much.

Regards,
vb_noob

EDIT: I've just realised that this thread is probably under the wrong location. I misread "VS.NET General Discussion" with "VB.NET General Discussion".
So, anyone who's in charged, please feel free to re-locate this thread. Sorry about this, folks.
 
Last edited:
Find only finds the first occurrence. If you want to find multiple occurrences then you have to call Find multiple times. Find has several overloads, some of which allow you to specify the range of characters to search. You need to use that one. Once you find one occurrence, you need to search again, but from where you found the first occurrence rather than from the beginning. You need to keep doing that until there are no occurrences left. That's why you need to use a loop. You keep calling Find over and over until it fails to find a match.
 
Find only finds the first occurrence. If you want to find multiple occurrences then you have to call Find multiple times. Find has several overloads, some of which allow you to specify the range of characters to search. You need to use that one. Once you find one occurrence, you need to search again, but from where you found the first occurrence rather than from the beginning. You need to keep doing that until there are no occurrences left. That's why you need to use a loop. You keep calling Find over and over until it fails to find a match

Okay, I get the idea but coding still remains as a problem cause I don't have a clue.
From what you've told me, I'm guessing I'll have to use While loop, right? I don't know about the Find overloads you're talking about but I've seen othe people use something like this. (Provided that RichTextBox1 is the name.)
RichTextBox1.SelectionStart = 0
RichTextBox1.SelectionLength = 0

Are these what you mean? If it's possible, could you please help me out with some coding?
Thank you very much.

Regards,
vb_noobs
 
Forget the code for now. As you should always do, solve the problem first, then write the code. Pick up a pen and paper and write down the steps you need to perform to get the job done. Refine those steps, then refine them again, and again, until they are the most basic steps possible. Then, write pseudo-code to implement those steps. Once you've gone that far, writing the VB code will be easy. If you have any issues, post here and tell us what you've done and where you're stuck and we can help.
 
You don't need a loop if you're going to make one search at each button click, you only need to hold the index value of last result and use that next time to specify from where to start searching.
 
Back
Top