Results 1 to 6 of 6

Thread: [RESOLVED] Repeat loop until word is not found again.

  1. #1

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Resolved [RESOLVED] Repeat loop until word is not found again.

    Hey Shmee again.

    This is a sub from my program...

    Code:
       Public Sub Highlight(ByVal r As RichTextBox)
            Try
                r.SelectAll()
                r.SelectionColor = Color.White
                r.DeselectAll()
    
                For Each strWord As String In RoomNames
                    Dim startIndex As Integer = 0
                    Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord))
                    For Each wMatch As Match In wordMatchers.Matches(r.Text)
                        With r
                            .Find(wMatch.Value, startIndex, RichTextBoxFinds.WholeWord)
                            .SelectionColor = Color.Cyan
                            .SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)
                            startIndex = .SelectionStart
                        End With
                    Next
                Next
                r.Select(r.TextLength, 0)
                r.ScrollToCaret()
            Catch
            End Try
        End Sub
    When ran this is the output..

    Name:  Untitled_zps7fc6af4a.png
Views: 380
Size:  9.2 KB

    FYI RoomNames is a List of String that holds each and every Room Name in my game.

    The problem. As you can see in the picture "The Babies Bedroom" is colored Cyan once and then ignored the second time.

    I tried copying and pasting the whole For Each strWord... and pasting it underneath and that worked perfectly. But It's bad coding as it can run into the same problem later on if the Room Name is mentioned more than twice.


    EDIT: Please ignore the green text.

    EDIT2: Just incase you're incapable of ignoring the green text this is the complete sub :P

    Code:
       Public Sub Highlight(ByVal r As RichTextBox)
            Try
                r.SelectAll()
                r.SelectionColor = Color.White
                r.DeselectAll()
    
                For Each strWord As String In ObjectList
                    Dim startIndex As Integer = 0
                    Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord))
                    For Each wMatch As Match In wordMatchers.Matches(r.Text)
                        With r
                            .Find(wMatch.Value, startIndex, RichTextBoxFinds.WholeWord)
                            .SelectionColor = Color.Lime
                            .SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)
                            startIndex = .SelectionStart
                        End With
                    Next
                Next
                For Each strWord As String In ItemList
                    Dim startIndex As Integer = 0
                    Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord))
                    For Each wMatch As Match In wordMatchers.Matches(r.Text)
                        With r
                            .Find(wMatch.Value, startIndex, RichTextBoxFinds.WholeWord)
                            .SelectionColor = Color.Yellow
                            .SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)
                            startIndex = .SelectionStart
                        End With
                    Next
                Next
                For Each strWord As String In RoomNames
                    Dim startIndex As Integer = 0
                    Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord))
                    For Each wMatch As Match In wordMatchers.Matches(r.Text)
                        With r
                            .Find(wMatch.Value, startIndex, RichTextBoxFinds.WholeWord)
                            .SelectionColor = Color.Cyan
                            .SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)
                            startIndex = .SelectionStart
                        End With
                    Next
                Next
                r.Select(r.TextLength, 0)
                r.ScrollToCaret()
            Catch
            End Try
        End Sub
    Last edited by Grags; Feb 7th, 2015 at 09:52 AM.
    The glass is half full if you're filling it, and half empty if you're emptying it.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Repeat loop until word is not found again.

    Instead of .Find, use .SelectionStart with [Match].Index and .SelectionLength with [Match].Value.Length

  3. #3

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Repeat loop until word is not found again.

    Quote Originally Posted by .paul. View Post
    Instead of .Find, use .SelectionStart with [Match].Index and .SelectionLength with [Match].Value.Length
    Would you elaborate please?
    The glass is half full if you're filling it, and half empty if you're emptying it.

  4. #4
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Repeat loop until word is not found again.

    Here's an example of doing it a better way (and taking on board what Paul said too).

    VB Code:
    1. Public Sub Highlight(ByVal r As RichTextBox, ByVal MyList As List(Of String), ByVal MyColor As Color)
    2.     'r.SelectAll()
    3.     'r.SelectionColor = Color.White
    4.     'r.DeselectAll()
    5.     For Each strWord As String In MyList
    6.         'Dim startIndex As Integer = 0
    7.         Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord))
    8.         For Each wMatch As Match In wordMatchers.Matches(r.Text)
    9.             With r
    10.                 '.Find(wMatch.Value, startIndex, RichTextBoxFinds.WholeWord)
    11.                 .SelectionStart = wMatch.Index
    12.                 .SelectionLength = wMatch.Value.Length
    13.                 .SelectionColor = MyColor
    14.                 '.SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)
    15.                 'startIndex = .SelectionStart
    16.             End With
    17.         Next
    18.     Next
    19.     r.Select(r.TextLength, 0)
    20.     r.ScrollToCaret()
    21. End Sub
    22.  
    23. Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    24.  
    25.     RichTextBox2.Text = "a adhsj s abc adjhu abc defh opq cde xyz cde"
    26.  
    27.     Dim RoomNames As New List(Of String)()
    28.     RoomNames.Add("abc")
    29.     RoomNames.Add("cde")
    30.  
    31.     Dim OtherStrings As New List(Of String)()
    32.     OtherStrings.Add("xyz")
    33.     OtherStrings.Add("opq")
    34.  
    35.     ' Clear any existing selections first
    36.     RichTextBox2.SelectAll()
    37.     RichTextBox2.SelectionColor = Color.Black
    38.     RichTextBox2.DeselectAll()
    39.  
    40.     Highlight(RichTextBox2, RoomNames, Color.Blue)
    41.     Highlight(RichTextBox2, OtherStrings, Color.Red)
    42.  
    43. End Sub
    Last edited by Bulldog; Feb 7th, 2015 at 11:56 AM.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Repeat loop until word is not found again.

    Code:
    Public Sub Highlight(ByVal r As RichTextBox)
        
        r.SelectAll()
        r.SelectionColor = Color.White
        r.DeselectAll()
    
        For Each strWord As String In RoomNames
            Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord))
            For Each wMatch As Match In wordMatchers.Matches(r.Text)
                With r
                    .SelectionStart = wMatch.Index
                    .SelectionLength = wMatch.Length
                    .SelectionColor = Color.Cyan
                    .SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)
                End With
            Next
        Next
        r.Select(r.TextLength, 0)
        r.ScrollToCaret()
    End Sub

  6. #6

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Repeat loop until word is not found again.

    Quote Originally Posted by Bulldog View Post
    Here's an example of doing it a better way (and taking on board what Paul said too).

    VB Code:
    1. Public Sub Highlight(ByVal r As RichTextBox, ByVal MyList As List(Of String), ByVal MyColor As Color)
    2.     'r.SelectAll()
    3.     'r.SelectionColor = Color.White
    4.     'r.DeselectAll()
    5.     For Each strWord As String In MyList
    6.         'Dim startIndex As Integer = 0
    7.         Dim wordMatchers As New Regex(String.Format("\b{0}(\b\S|\b)", strWord))
    8.         For Each wMatch As Match In wordMatchers.Matches(r.Text)
    9.             With r
    10.                 '.Find(wMatch.Value, startIndex, RichTextBoxFinds.WholeWord)
    11.                 .SelectionStart = wMatch.Index
    12.                 .SelectionLength = wMatch.Value.Length
    13.                 .SelectionColor = MyColor
    14.                 '.SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)
    15.                 'startIndex = .SelectionStart
    16.             End With
    17.         Next
    18.     Next
    19.     r.Select(r.TextLength, 0)
    20.     r.ScrollToCaret()
    21. End Sub
    22.  
    23. Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    24.  
    25.     RichTextBox2.Text = "a adhsj s abc adjhu abc defh opq cde xyz cde"
    26.  
    27.     Dim RoomNames As New List(Of String)()
    28.     RoomNames.Add("abc")
    29.     RoomNames.Add("cde")
    30.  
    31.     Dim OtherStrings As New List(Of String)()
    32.     OtherStrings.Add("xyz")
    33.     OtherStrings.Add("opq")
    34.  
    35.     ' Clear any existing selections first
    36.     RichTextBox2.SelectAll()
    37.     RichTextBox2.SelectionColor = Color.Black
    38.     RichTextBox2.DeselectAll()
    39.  
    40.     Highlight(RichTextBox2, RoomNames, Color.Blue)
    41.     Highlight(RichTextBox2, OtherStrings, Color.Red)
    42.  
    43. End Sub
    Awesome! Thanks a lot. Sorry for the late reply, but I've been busy working etc. But I've finally managed to find some time. And this is perfect. Thanks again.

    Thanks .Paul.

    +Rep to both.

    EDIT: Sorry the system won't allow me to rep either of you. Such a shame, as your work deserves some kind of reward. I will owe you one. Many thanks to both.
    The glass is half full if you're filling it, and half empty if you're emptying it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width