Results 1 to 2 of 2

Thread: functions highlights the letter F and U

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    functions highlights the letter F and U

    These two functions highlights the letter F and U.
    highlight_F()
    highlight_U()

    They work perfectly individually. When I run them in sequence, I get the following inconsistent results. What is wrong with my code?

    $30.85 FU <-- U gets highlighted blue (correct)

    | $30.80 F | $30.85 FU | $30.85 F | $30.90 F | $31.00 F | $31.00 F | $31.50 F | $31.50 F | $31.50 F | $31.50 F | $22.48 +$4.99 (27.47) | $24.19 +$4.99 (29.18) | $25.00 +$4.99 (29.99) | $25.00 +$4.99 (29.99) | $25.60 +$4.99 (30.59) | $26.00 +$4.62 (30.62) | $27.00 +$4.99 (31.99) | $28.00 +$4.99 (32.99) | $29.99 +$4.62 (34.61) | $29.99 +$4.99 (34.98)

    $37.99 F <-- F gets highlighted blue (incorrect)

    | $33.50 F | $30.98 +$4.99 (35.97) | $30.99 +$4.99 (35.98) | $31.99 +$4.60 (36.59) | $37.99 F | $39.99 +$4.99 (44.98) | $25.99 +$5.99 (31.98) | $28.99 +$4.57 (33.56) | $29.99 +$4.99 (34.98) | $30.00 +$4.99 (34.99) | $32.23 +$4.99 (37.22) | $34.99 +$4.99 (39.98) | $37.95 +$4.99 (42.94) | $38.00 +$4.99 (42.99) | $39.99 +$4.99 (44.98) | $40.00 +$4.99 (44.99)

    Public Function highlight_F()
    RichTextBox1.SelectAll()
    RichTextBox1.SelectionColor = Color.Black
    RichTextBox1.SelectionBackColor = Color.White
    RichTextBox1.DeselectAll()

    Dim FoundAtPosition As Integer
    FoundAtPosition = RichTextBox1.Find("F")
    RichTextBox1.SelectionColor = Color.Red
    RichTextBox1.SelectionFont = New Font("Arial", 12, FontStyle.Bold)

    Do Until FoundAtPosition < 0
    RichTextBox1.SelectionColor = Color.Red
    RichTextBox1.SelectionFont = New Font("Arial", 12, FontStyle.Bold)

    If FoundAtPosition + 1 < RichTextBox1.Text.Length Then
    FoundAtPosition = RichTextBox1.Find("F", FoundAtPosition + 1, RichTextBoxFinds.None)
    Else
    FoundAtPosition = -1
    End If
    Loop

    End Function

    Public Function highlight_U()

    Dim FoundAtPosition As Integer
    FoundAtPosition = RichTextBox1.Find("U")
    RichTextBox1.SelectionColor = Color.Blue
    RichTextBox1.SelectionFont = New Font("Arial", 14, FontStyle.Bold)

    Do Until FoundAtPosition < 0
    RichTextBox1.SelectionColor = Color.Blue
    RichTextBox1.SelectionFont = New Font("Arial", 14, FontStyle.Bold)

    If FoundAtPosition + 1 < RichTextBox1.Text.Length Then
    FoundAtPosition = RichTextBox1.Find("U", FoundAtPosition + 1, RichTextBoxFinds.None)
    Else
    FoundAtPosition = -1
    End If
    Loop

    End Function

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: functions highlights the letter F and U

    When you post, could you please wrap any code in [CODE]Your code goes here[/CODE] tags or [HIGHLIGHT=VB.NET]Your code goes here[/HIGHLIGHT] tags. It makes it much easier to read and therefore more likely that someone will read it and answer your questions.

    You had me confused there. I couldn't replicate your problem until I realised that the text example you supplied are four separate examples, not a single block of text from the rtb.

    The problem is that the highlight methods leave the rtb's SelectionStart, SelectionLength and SelectionColor properties set to the last highlighted letter. So for $37.99 F they have the values 7, 1, Color.Red, i.e. all pointing to the F. This would not normally be a problem, as they will be cleared when you click in the rtb.

    However, you immediately call the highlight_U function. The first four lines of code in that are:
    Code:
    Dim FoundAtPosition As Integer
    FoundAtPosition = RichTextBox1.Find("U")
    RichTextBox1.SelectionColor = Color.Blue
    RichTextBox1.SelectionFont = New Font("Arial", 14, FontStyle.Bold)
    As there is no "U", the Find method will return -1 and apparently not change the values of the SelectionStart, SelectionLength and SelectionColor properties. So your code will then set the "F"'s colour to Blue, as it is the text that the above properties are still referring to.

    A simple solution is to change
    Code:
    Dim FoundAtPosition As Integer
    FoundAtPosition = RichTextBox1.Find("U")
    RichTextBox1.SelectionColor = Color.Blue
    RichTextBox1.SelectionFont = New Font("Arial", 14, FontStyle.Bold)
    in both methods to (using ("F") where appropriate, of course)
    Code:
    Dim FoundAtPosition As Integer
    FoundAtPosition = RichTextBox1.Find("U")

    That said, I'd recommend you change your code somewhat. You are using Functions but never return a value, so you should instead be using Subs. You also have a lot of repeated code that could be tidied up and made more versatile. I'd suggest something along the lines of:
    Code:
    Private Sub ResetTextColour()
        RichTextBox1.SelectAll()
        RichTextBox1.SelectionColor = Color.Black
        RichTextBox1.SelectionBackColor = Color.White
        RichTextBox1.DeselectAll()
    End Sub
    
    Private Sub Highlight(searchText As String, highlightFont As Font, highlightColour As Color)
        Dim foundAtPosition As Integer = -1
        Dim startAtPosition As Integer = 0
    
        foundAtPosition = RichTextBox1.Find(searchText, RichTextBoxFinds.None)
    
        Do Until startAtPosition > foundAtPosition
            RichTextBox1.SelectionColor = highlightColour
            RichTextBox1.SelectionFont = highlightFont
    
            startAtPosition = foundAtPosition + searchText.Length
            foundAtPosition = RichTextBox1.Find(searchText, startAtPosition, RichTextBoxFinds.None)
        Loop
    
    End Sub
    which you would use as:
    Code:
    ResetTextColour()
    Highlight("F", New Font("Arial", 14, FontStyle.Bold), Color.Red)
    Highlight("U", New Font("Arial", 14, FontStyle.Bold), Color.Blue)
    Note I set the access levels for the methods to Private. If you actually need Public access, then don't forget to change the modifiers back to Public.
    Last edited by Inferrd; Oct 8th, 2013 at 05:01 PM.

Tags for this Thread

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