[RESOLVED] Problem with Syntax Highlighting?
Hey guys! This is my first post on the forum so please excuse me if I do something wrong. I've posted this on many forums, however, no one has seemed to give me an answer. So hopefully you guys can help.
Here is my existing Syntax Highlighting code :
Code:
Dim listbox1 As New ListBox
listbox1.Items.Add("<br/>")
Dim int As Integer = 0
Dim line As String = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart)
For Each item In listbox1.Items
If RichTextBox1.Lines(line).Contains(item) Then
Dim instance As Integer = RichTextBox1.Find(item, RichTextBox1.SelectionStart - CInt(item.ToString.Length), RichTextBox1.SelectionStart)
If instance > 0 Then
RichTextBox1.SelectionStart = RichTextBox1.SelectionStart - CInt(item.ToString.Length)
RichTextBox1.SelectionLength = item.ToString.Length
RichTextBox1.SelectionColor = Color.Blue
End If
End If
Next
RichTextBox1.SelectionStart = ss
RichTextBox1.SelectionLength = sl
RichTextBox1.SelectionColor = Color.Black
The problem I'm having, is this code also colors around 4 characters after the instance. This confuses me. Any help would be appreciated!
Re: Problem with Syntax Highlighting?
Hi, I have just tested your code, and i'm not sure what you want it to do.
At the moment it just changes the colour of the 4 characters directly before the caret position in your RichTextBox.
I'm guessing you are trying to highlight the next occurence of the <br/> tag by searching from the caret position onwards?
Or am I totally off base here?
Re: Problem with Syntax Highlighting?
You're correct, I want it to search for the <br/> command as the user types, so everything from the caret position onward.
Re: Problem with Syntax Highlighting?
I know it's bad to bump, but I really need this problem fixed.
Re: Problem with Syntax Highlighting?
Hi, Sorry to leave you hanging, I lost track of my posts.. anyhoo...
Going off what you have said - this would assume that the user is typing into the richtextbox, but not at the end of the text. So if the textbox contained the following text already:
"This is a text string ending with </br>."
And the user started typing at the beggining (before the word "This") then you would have to search forwards in the string in order to highlight <br/>. If this is correct and what you are trying to do then you could do this:
VB Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim searchString As String = "<br/>" 'Set the searchString
Dim cursorPos As Integer = RichTextBox1.SelectionStart 'Get the position of the Textbox Caret
Dim TextToSearch As String = RichTextBox1.Text.Substring(cursorPos) 'Get a substring (Caret position onwards) to search
If TextToSearch < searchString Then Exit Sub 'The string cannot possibly contain <br/> as it is too short, so we can exit the routine
For i As Integer = 0 To TextToSearch.Length - searchString.Length
'See if the TextToSearech contains the first letter of the searchString e.g. "<"
If TextToSearch.Chars(i) = searchString.Chars(0) Then
'If it does then we can try and match the rest of the characters in the searchstring
Dim iX As Integer = i
Dim isMatch As Boolean = True
For Each c As Char In searchString
If TextToSearch.Chars(iX) <> c Then
'If any of the other letters do not match, then we can drop into the outer For-Loop to look for the next "<"
isMatch = False
Exit For
Else : iX += 1
End If
Next
'Otherwise if all the letters match, we will end up here
If isMatch = True Then
'So now you can select the text using the index of "<" which is (i) and the cursorpos (because we grabbed a searchstring to do the search)
RichTextBox1.SelectionStart = i + cursorPos
RichTextBox1.SelectionLength = Len(searchString)
RichTextBox1.SelectionColor = Color.Blue
Exit For
End If
End If
Next
End Sub
However if you are actually trying to Highlight the word <br/> as the user types it in, then you would need to look backwards in the string like this:
VB Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim searchString As String = "<br/>" 'Set the searchString
Dim cursorPos As Integer = RichTextBox1.SelectionStart 'Get the position of the Textbox Caret
If cursorPos < searchString.Length Then Exit Sub 'If the text before the searchString is not long enough to contain it then skip the routine
Dim TextToSearch As String = RichTextBox1.Text.Substring(cursorPos - Len(searchString), Len(searchString)) 'Get the 5 letters before the Caret
'If TextToSearch matches the searchstring then we know the user has just finished typing it
If TextToSearch = searchString Then
'Do your highlighting
RichTextBox1.SelectionStart = cursorPos - Len(searchString)
RichTextBox1.SelectionLength = Len(searchString)
RichTextBox1.SelectionColor = Color.Blue
End If
End Sub
Re: Problem with Syntax Highlighting?
Ah okay! Thanks alot for posting the 2 code snippets! Both of them will help me tremendously.
I figured out what I was doing wrong in my first code. So I have replaced that specific code with the second one you posted, except I modified it to support multiple words at once. Seen here :
Code:
Dim listbox As New ListBox
listbox.Items.Add("<br/>")
For Each item In listbox.Items
Dim searchString As String = item 'Set the searchString
Dim cursorPos As Integer = RichTextBox1.SelectionStart 'Get the position of the Textbox Caret
If cursorPos < searchString.Length Then Exit Sub 'If the text before the searchString is not long enough to contain it then skip the routine
Dim TextToSearch As String = RichTextBox1.Text.Substring(cursorPos - Len(searchString), Len(searchString)) 'Get the 5 letters before the Caret
'If TextToSearch matches the searchstring then we know the user has just finished typing it
If TextToSearch = searchString Then
'Do your highlighting
RichTextBox1.SelectionStart = cursorPos - Len(searchString)
RichTextBox1.SelectionLength = Len(searchString)
RichTextBox1.SelectionColor = Color.Blue
End If
RichTextBox1.SelectionStart = ss
RichTextBox1.SelectionLength = sl
RichTextBox1.SelectionColor = Color.Black
Next
But thanks for your help!