its trivial but...
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
- 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.
- Can you please provide an example to illustrate what you mean?
- 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.
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.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 - Nop
Again it highlights only the last find of the loop and not all of them... - 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. - 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 - 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. - 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... Still it refuses to highligh everything...
Was that before or after you modified it along the lines of my example?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


