[2008] Help with my new Syntax Highlighted RTB
Hi,
I have a RichTextBox1 and this code:
VB Code:
Public ColorKeyword As Color = ColorTranslator.FromHtml("#a325a3")
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim Keywords As New List(Of String)
Keywords.Add("echo")
Keywords.Add("print")
If RichTextBox1.Text.Length > 0 Then
Dim selectStart As Integer = RichTextBox1.SelectionStart
RichTextBox1.Select(0, RichTextBox1.Text.Length)
RichTextBox1.SelectionColor = Color.Black
RichTextBox1.DeselectAll()
For Each oneWord As String In Keywords
Dim pos As Integer = 0
Do While RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
pos = RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
RichTextBox1.Select(pos, oneWord.Length)
RichTextBox1.SelectionColor = ColorKeyword
pos += 1
Loop
Next
RichTextBox1.SelectionLength = 0
RichTextBox1.SelectionStart = selectStart
End If
End Sub
The code highlights nicely, but there is a problem.
Try the above code and you will notice how much it flickers/flashes. How would I avoid the flickering? Also, if I paste a big text like 10 000 lines and if I then go to the end of the document and start writing, everytime I write a new character it jumps to the start of the document.
Any help? I will give credits!
Re: [2008] Help with my new Syntax Highlighted RTB
you are going to have to start over completely. The reason you are having the flickering is you are modifying everything with the selection. What you are going to have to do is modify the underlying rtf code. Frankly i find it easier to work with the web browser control, although this may not be an option for you if you are wanting to be able to edit it with a cursor. If you have a separate field to enter data on (such as in a chat window) then i would say go with the web browser control.
Reason: Every color you use in a richtextbox and every font has to be listed in a specific order in the top of the document, then you refer to them by position on the list! The web browser control, if you want to change color to red, you simply insert the appropriate font color= in the appropriate spot.