Ask a questionAsk a question
 

General Discussionits trivial but...

  • Wednesday, November 04, 2009 10:22 PMPilot_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    I know its trivial but I can not make it to work. The code below colors only the last value of the real variable it finds and not all of them. Any ideas what may be wrong???/

    Dim update As String = Regex.Replace(RichTextBox1.Text, pattern2, real.ToString)
    
    RichTextBox1.Text = update
    
    Dim m As Match = Regex.Match(RichTextBox1.Text, real.ToString)
    
    While (m.Success)
    
    Dim bmStr As String = m.Value
    
    m = m.NextMatch()
    
    RichTextBox1.SelectionStart = RichTextBox1.Find(bmStr)
    
    RichTextBox1.SelectionColor = Color.Coral
    
    End While
    

All Replies

  • Wednesday, November 04, 2009 10:47 PMAcamar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You should be selecting the matched text and setting the text color of the selected text.   Currently, you are setting the selection color, and when the loop finishes only the last match is selected, so only the last match is colored.
  • Wednesday, November 04, 2009 11:18 PMPilot_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you please provide an example to illustrate what you mean?
  • Thursday, November 05, 2009 12:23 AMAcamar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    The example below replaces every occurrence of control with ctrl and colors each replacement. If there are several different replacements (I can't tell from the above code whether or not the regex is changing) then this code must execute for each application of the regex, using the regex source and replace strings instead of the literals used here.

            RichTextBox1.Text = RichTextBox1.Text.Replace("control", "ctrl")
            Dim I As Integer = -1
            I = RichTextBox1.Find("ctrl", I + 1, RichTextBoxFinds.None)
            While I <> -1
                RichTextBox1.SelectionStart = I
                RichTextBox1.SelectionLength = 4
                RichTextBox1.SelectionColor = Color.Coral
                I = RichTextBox1.Find("ctrl", I + 1, RichTextBoxFinds.None)
             End While
        End Sub
    
    I would recommend working with a copy of the text box text for this purpose, and replacing the text box only when the adjustments to the copy text are complete. This will ensure that only th replaced text is colored, not other text that happened to match the replaced text.



  • Thursday, November 05, 2009 12:33 AMPilot_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Nop

    Again it highlights only the last find of the loop and not all of them...
  • Thursday, November 05, 2009 12:41 AMAcamar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    When I run it here all replacements are highlighted.  The text comes from here (starting at Remarks):

    http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectioncolor.aspx

    There are 6 occurrences and they all get highlighted.
  • Thursday, November 05, 2009 1:14 AMPilot_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I think the difference has to do with the use of regual expressions. Here is the complete code in case you can still help me

     For x As Integer = 0 To script_times.Items.Count - 1
                    If x < script_times.Items.Count - 1 Then
                        real = (script_times.Items.Item(x) + Anim_times.Items.Item(x) + 0.3)
                        'Dim pattern As String = "\s" & script_times.Items.Item(x + 1) & "]"
                        Dim pattern2 As String = "\b(?<!\.)" & script_times.Items.Item(x + 1) & "(?![.,])\b"
                        If script_times.Items.Item(x + 1) < real Then
                            If script_times.Items.Item(x) <> script_times.Items.Item(x + 1) Then
                                Dim update As String = Regex.Replace(RichTextBox1.Text, pattern2, real.ToString)
                                RichTextBox1.Text = update
                                Dim m As Match = Regex.Match(RichTextBox1.Text, real.ToString)
                                While (m.Success)
                                    Dim bmStr As String = m.Value
                                    m = m.NextMatch()
                                    RichTextBox1.SelectionStart = RichTextBox1.Find(bmStr)
                                    RichTextBox1.SelectionBackColor = Color.Yellow
                                End While
    
                                RichTextBox2.Text = RichTextBox2.Text & "Error in animation times! Switch(es):" & Space(1) & Switches.Items.Item(x + 1) & Space(1) & "Total=" & Space(1) & script_times.Items.Item(x) + Anim_times.Items.Item(x) + 0.3 & vbNewLine
                            End If
                            If script_times.Items.Item(x) = script_times.Items.Item(x + 1) Then
                                For Each line As String In RichTextBox1.Lines
                                    If line.Contains(Switches.Items.Item(x + 1)) Then
                                        'change the clock of the line to its proper value
                                        Dim new_line As String = Regex.Replace(line, pattern2, real.ToString)
                                        'We need to search and replace this line in the richbox itself
                                        RichTextBox1.Text = Replace(RichTextBox1.Text, line, new_line)
                                        RichTextBox1.SelectionStart = RichTextBox1.Find(real)
                                        'Selection length 
    
                                        RichTextBox1.SelectionColor = Color.Blue
                                        RichTextBox2.Text = RichTextBox2.Text & "Dublicated animation times! Switch(es):" & Space(1) & Switches.Items.Item(x + 1) & Space(1) & "Total=" & Space(1) & script_times.Items.Item(x) + Anim_times.Items.Item(x) + 0.3 & vbNewLine
                                    End If
                                Next
                            End If
                        End If
                    End If
                Next
  • Thursday, November 05, 2009 1:38 AMAcamar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Unfortunately that code isn't usable without knowing the text you are trying to process it against, but I can't see where in that text you have incorporated the changes I suggested - the code 

    Dim update As String = Regex.Replace(RichTextBox1.Text, pattern2, real.ToString)
    RichTextBox1.Text = update
    Dim m As Match = Regex.Match(RichTextBox1.Text, real.ToString)
    While (m.Success)
        Dim bmStr As String = m.Value
        m = m.NextMatch()
        RichTextBox1.SelectionStart = RichTextBox1.Find(bmStr)
        RichTextBox1.SelectionBackColor = Color.Yellow
    End While

    appears to be the same as the code you originally had the problem with.
  • Thursday, November 05, 2009 1:49 AMPilot_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Here is the test its been tested against

    \clock [t= 7] \SetSwitch [figure= fullBod switch= built state= start]
    \clock [t= 9.1] \SetSwitch [figure= fullBod switch= at_the state= start]

    Still it refuses to highligh everything...
  • Thursday, November 05, 2009 2:39 AMAcamar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Still it refuses to highligh everything...

    Was that before or after you modified it along the lines of my example?
  • Thursday, November 05, 2009 2:50 AMPilot_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

     

    It took me a while but here it is

    For Each item As String In real_times
    
    HighLightText(item, Color.Red, RichTextBox1)
    
    Next
    
    
    
    Private Sub HighLightText(ByVal TextToHighlight As String, ByVal HighlightColor As Color, ByVal RTBToHighlight As RichTextBox)
    
    Dim textEnd As Integer = RTBToHighlight.TextLength
    
    Dim index As Integer = 0
    
    Dim lastIndex As Integer = RTBToHighlight.Text.LastIndexOf(TextToHighlight)
    
    While index < lastIndex
    
    RTBToHighlight.Find(TextToHighlight, index, textEnd, RichTextBoxFinds.None)
    
    RTBToHighlight.SelectionColor = HighlightColor
    
    index = RTBToHighlight.Text.IndexOf(TextToHighlight, index) + 1
    
    End While
    
    End Sub