RichTextFind methods order of precedence ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a rule for the order of methods in the RichTextFind method?

Through experimentation I found an order of the methods when doing reverse
searches.

Specifically, when searching reverse, with matching case and for whole
words, the statement must be in this order to work correctly:
-- RichTextBoxFinds.Reverse.MatchCase.WholeWord

I thought each option was ORed together and their order wasn't important,
and I can find nothing in the Help or any of my references about this.
 
tcloud said:
Is there a rule for the order of methods in the RichTextFind method?

Through experimentation I found an order of the methods when doing reverse
searches.

My experiments produce different results :)
Specifically, when searching reverse, with matching case and for whole
words, the statement must be in this order to work correctly:
-- RichTextBoxFinds.Reverse.MatchCase.WholeWord

When I first saw this, I thought that you must be running with Option
Strict off, because I didn't believe that this was valid syntax. So I
tried it, and lo and behold it compiled! It turns out that although
(for example) RichTextBoxFinds.WholeWord prints as an integer in debug
mode, and claims to be of type Integer in the Watch and Locals windows,
it's actually (as revealed by Typename()) of type RichTextBoxFinds,
which is why it's legal to chain together enum values like this.

Semantically however, only the last in the chain is relevant. That is
to say,

RichTextBoxFinds.WholeWord.NoHighlight.None.MatchCase.WholeWord = _
RichTextBoxFinds.WholeWord

(they are both 2)

I thought each option was ORed together and their order wasn't important,
and I can find nothing in the Help or any of my references about this.

As the documentation says,

options
A bitwise combination of the RichTextBoxFinds values.

so yes, you Or them together and order is not important.

My testing: Create a form, put a RichTextBox and a Button on it. Put
this code in the Click event handler of the button:

Dim r As RichTextBox = RichTextBox1
Dim i As Integer

r.Text = "The quick BROWN brown fox jumped over the lazy
browner dog Rover."

i = r.Find("over", RichTextBoxFinds.WholeWord Or
RichTextBoxFinds.Reverse)
MsgBox("Find 'over' with Whole Word, Reverse: " & i)

i = r.Find("over", RichTextBoxFinds.Reverse)
MsgBox("Find 'over' with Reverse: " & i)

i = r.Find("over", RichTextBoxFinds.WholeWord.Reverse)
MsgBox("Find 'over' with Whole Word, Reverse CHAINED: " & i)
'same as just Reverse

i = r.Find("brown", RichTextBoxFinds.MatchCase Or
RichTextBoxFinds.Reverse Or RichTextBoxFinds.WholeWord)
MsgBox("Find 'brown' with MatchCase, Reverse, Whole Word: " &
i)

i = r.Find("brown", RichTextBoxFinds.MatchCase)
MsgBox("Find 'brown' with MatchCase: " & i)

i = r.Find("brown", RichTextBoxFinds.Reverse)
MsgBox("Find 'brown' with Reverse: " & i)

i = r.Find("brown", RichTextBoxFinds.WholeWord)
MsgBox("Find 'brown' with Whole Word: " & i)

i = r.Find("brown",
RichTextBoxFinds.MatchCase.Reverse.WholeWord)
MsgBox("Find 'brown' with MatchCase, Reverse, Whole Word
CHAINED: " & i)
'same as just Whole Word

In each case using chained enum values you will find the result is
equal to that obtained with just the last in the chain, and different
from that obtained by correctly combining the flags.
 
Larry Lard said:
My experiments produce different results :)

... [snip]
In each case using chained enum values you will find the result is
equal to that obtained with just the last in the chain, and different
from that obtained by correctly combining the flags.

Larry, thanks for the response.

I'm a beginner -- please explain what you mean by "correctly combining the
flags". I had found a short snippet in the Help which talked about the
binary (Enum) values of each selection, but nowhere could I find out how to
include the ORed values in the Find statement.

(I also wish someone would tell me how to stop this bulletin board from
putting the html tags in for quotes, ampersands, etc. ... I cannot find
anything on how to properly edit posts so the punctuation marks appear
correctly.)
 
tcloud said:
Larry Lard said:
My experiments produce different results :)

... [snip]
In each case using chained enum values you will find the result is
equal to that obtained with just the last in the chain, and different
from that obtained by correctly combining the flags.

Larry, thanks for the response.

I'm a beginner -- please explain what you mean by "correctly combining the
flags". I had found a short snippet in the Help which talked about the
binary (Enum) values of each selection, but nowhere could I find out how to
include the ORed values in the Find statement.

Because the RichTextBoxFinds type is a Flags type Enum, its values can
be combined. As you have read, each individual value has a single bit
set, so that when the values are combined with Or, multipled flags can
be set:

RichTextBoxFinds.WholeWord = 2 ' 000010
RichTextBoxFinds.MatchCase = 4 ' 000100 in binary
RichTextBoxFinds.NoHighlight = 8 ' 001000
RichTextBoxFinds.Reverse = 16 ' 010000

So for example if we combine WholeWord and MatchCase, we do 2 Or 4
which gives us 6, which is 000110 in binary, and as you see the two
flags we want are set.

As to where to put this combined value, we can use a combined value
wherever a RichTextBoxFinds is required. So for example a Find
operation with just WholeWord set would be:

i = rtxt.Find("word", RichTextBoxFinds.WholeWord)

and if we wanted also to match case we would just say

i = rtxt.Find("word", RichTextBoxFinds.WholeWord Or
RichTextBoxFinds.MatchCase)


It can I suppose appear slightly confusing that we use 'Or' when what
we mean is 'also'; just remember that we are combining binary bits, and
Or is how we do this.
(I also wish someone would tell me how to stop this bulletin board from
putting the html tags in for quotes, ampersands, etc. ... I cannot find
anything on how to properly edit posts so the punctuation marks appear
correctly.)

Sorry, I read this group through Google groups so I can't help with
that...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top