0

I'm using a RichTextBox find function. It works fine until I stick it in a While loop. Then it won't do anything.

This code works:

Dim startTEXT As Integer = 0
Dim endTEXT As Integer = RichTextBox1.Text.LastIndexOf(tword)
While startTEXT < endTEXT
    RichTextBox1.Find(tword, startTEXT, RichTextBox1.TextLength, RichTextBoxFinds.WholeWord)
    RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily, RichTextBox1.SelectionFont.Size, FontStyle.Underline)
    RichTextBox1.SelectionColor = Color.Red
    startTEXT = RichTextBox1.Text.IndexOf(tword, startTEXT) + 1
End While

This code does not:

For Each tword In ListBox1.Items
     Dim startTEXT As Integer = 0
     Dim endTEXT As Integer = RichTextBox1.Text.LastIndexOf(tword)
     While startTEXT < endTEXT
        RichTextBox1.Find(tword, startTEXT, RichTextBox1.TextLength, RichTextBoxFinds.WholeWord)
        RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily, RichTextBox1.SelectionFont.Size, FontStyle.Underline)
        RichTextBox1.SelectionColor = Color.Red
        startTEXT = RichTextBox1.Text.IndexOf(tword, startTEXT) + 1
    End While
Next

What is preventing the find function from working? Using VB.NET 2010.

22
  • Are you sure that Find doesn't work, or is it that the early iterations are overwritten by the later ones? Commented Jun 26, 2022 at 6:25
  • 2
    First thing, set Option Strict On, then declare the Font outside the loops as, e.g., dim selFont = New Font(RichTextBox1.Font, FontStyle.Underline) and set it either in the loop or outside the loop: RichTextBox1.SelectionFont = selFont (since it appears the Font doesn't change, you can set it once right after you have declared it, outside the loops) -- You may want to replace this procedure with something like this: Why can't I change the color of repeated words in a RichTextBox?, it's much easier to handle (and debug).
    – Jimi
    Commented Jun 26, 2022 at 6:37
  • 4
    As a note, if you're actually using Visual Studio 2010, you should (really) consider updating it (if it's up to you, of course). Visual Studio 2022 Community Edition is free software.
    – Jimi
    Commented Jun 26, 2022 at 6:44
  • 1
    Set Option Strict On. That's not really an option, it's a necessity. That option set to OFF is meant to ease the port of VB6 app to VB.Net and exists for this reason only. In a VB.Net app, this is just trouble and trickery and source of problems hard to debug, not mentioning unexplicable run-time exceptions and misbehaviors. The fact that some people abuse it is not an excuse for anyone.
    – Jimi
    Commented Jun 26, 2022 at 20:21
  • 1
    There's no Regex option like the Find option WholeWord: are you kidding me? The equivalent is \b, as in: dim pattern = String.Concat(ListBox1.Items.Cast(Of String).Select(Function(w) "\b" & w & "\b|")). As you can see, RegexOptions.IgnoreCase is already set in that code, as a Regex Option. But this can be set with a token, too. -- The method above uses LINQ's Select(), which is usually imported by default. If it's not, add Imports System.Linq. But you can build the pattern string with a standard loop instead. With a Regex, the search procedure can become very flexible.
    – Jimi
    Commented Jun 27, 2022 at 9:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.