Results 1 to 2 of 2

Thread: [2008] Help with my new Syntax Highlighted RTB

  1. #1

    Thread Starter
    Addicted Member kaisellgren's Avatar
    Join Date
    Jan 2006
    Posts
    149

    [2008] Help with my new Syntax Highlighted RTB

    Hi,

    I have a RichTextBox1 and this code:

    VB Code:
    1. Public ColorKeyword As Color = ColorTranslator.FromHtml("#a325a3")
    2.  
    3.     Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
    4.  
    5.         Dim Keywords As New List(Of String)
    6.         Keywords.Add("echo")
    7.         Keywords.Add("print")
    8.  
    9.         If RichTextBox1.Text.Length > 0 Then
    10.             Dim selectStart As Integer = RichTextBox1.SelectionStart
    11.             RichTextBox1.Select(0, RichTextBox1.Text.Length)
    12.             RichTextBox1.SelectionColor = Color.Black
    13.             RichTextBox1.DeselectAll()
    14.             For Each oneWord As String In Keywords
    15.                 Dim pos As Integer = 0
    16.                 Do While RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
    17.                     pos = RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
    18.                     RichTextBox1.Select(pos, oneWord.Length)
    19.                     RichTextBox1.SelectionColor = ColorKeyword
    20.                     pos += 1
    21.                 Loop
    22.             Next
    23.             RichTextBox1.SelectionLength = 0
    24.             RichTextBox1.SelectionStart = selectStart
    25.         End If
    26.  
    27.     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!

  2. #2
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    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.
    Last edited by Lord Orwell; Aug 30th, 2008 at 07:49 AM.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

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