Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dynamic textcolor change in RichTextBox in visual C#

Dynamic textcolor change in RichTextBox in visual C#

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Blekk
    wrote on last edited by
    #1

    Hi, I am wanting to create an effect like that in FrontPage and DreamWeaver (and many other code IDEs). For example, I am using DreamWeaver and I type this automatically turns blue to show it is code. This is what I would like to do. I already have something going, but it only does it when the file is first loaded, not while it is being edited, so I need to put it in a loop or something like that, also another problem was that all subsequent text after the last string () was also blue. Here are my two functions so far, CheckAllText() runs in the Load function of the Form. public void TextColor(string text) { int iReturnValue = richTextBoxZedit.Find(text); if (iReturnValue >= 0) { richTextBoxZedit.SelectionColor = Color.Blue; } } public void CheckAllText() { string[] HTMLArray = new string[4] { "", "", "", "" }; for (int i = 0; i < HTMLArray.Length; i++) { TextColor(HTMLArray[i]); } } Any suggestions? Thanks in advance.

    L 1 Reply Last reply
    0
    • B Blekk

      Hi, I am wanting to create an effect like that in FrontPage and DreamWeaver (and many other code IDEs). For example, I am using DreamWeaver and I type this automatically turns blue to show it is code. This is what I would like to do. I already have something going, but it only does it when the file is first loaded, not while it is being edited, so I need to put it in a loop or something like that, also another problem was that all subsequent text after the last string () was also blue. Here are my two functions so far, CheckAllText() runs in the Load function of the Form. public void TextColor(string text) { int iReturnValue = richTextBoxZedit.Find(text); if (iReturnValue >= 0) { richTextBoxZedit.SelectionColor = Color.Blue; } } public void CheckAllText() { string[] HTMLArray = new string[4] { "", "", "", "" }; for (int i = 0; i < HTMLArray.Length; i++) { TextColor(HTMLArray[i]); } } Any suggestions? Thanks in advance.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, three comments: - there are discussion threads about such things all the time, often they have the following keywords: RichTextBox Syntax Coloring; and there are some article on it too on CP = the more data and intelligence you add to RTB, it gets slower and slower - IMO if you want to come up with a line-oriented editor with some fancy features and capable of dealing with 1000+ lines of text, yiu should not use RTB, but just use a Panel, do all the text handling and the painting yourself. That solution scales well, i.e. you can make it deal with 1,000,000+ lines and still behave well. Example: I do most of the syntax coloring analysis in the paint handler, and only for the lines that are visible (that's no more than 100 on today's monitors). If you expect to handle no more than say 300 lines of text, you're on a good track; ptherwise you may have to rethink it. :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      B 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, three comments: - there are discussion threads about such things all the time, often they have the following keywords: RichTextBox Syntax Coloring; and there are some article on it too on CP = the more data and intelligence you add to RTB, it gets slower and slower - IMO if you want to come up with a line-oriented editor with some fancy features and capable of dealing with 1000+ lines of text, yiu should not use RTB, but just use a Panel, do all the text handling and the painting yourself. That solution scales well, i.e. you can make it deal with 1,000,000+ lines and still behave well. Example: I do most of the syntax coloring analysis in the paint handler, and only for the lines that are visible (that's no more than 100 on today's monitors). If you expect to handle no more than say 300 lines of text, you're on a good track; ptherwise you may have to rethink it. :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        B Offline
        B Offline
        Blekk
        wrote on last edited by
        #3

        Ok thanks, I am very early on in my learning of c# and so wouldn't really know how to go about painting the text myself etc. Any tutorials? Thanks.

        L 1 Reply Last reply
        0
        • B Blekk

          Ok thanks, I am very early on in my learning of c# and so wouldn't really know how to go about painting the text myself etc. Any tutorials? Thanks.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, since you are new to C#, I do not recommend you dive into GDI+ with all the painting stuff immediately. I do recommend you read and work your way through an introductory book on C# though (possibly skipping some of the chapters you dont immediately need, e.g. GDI+, databases, networking). If you dont need to handle very large files, then probably the RichTextBox approach is best for you. It has its quirks, but you will learn from trying, and possibly asking here. If on the other hand you do get curious, dont forget to search on CodeProject. Searching "paint text" in the search box above, resulted in many articles, including this lenghty book chapter.[^] Enjoy C# !

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          B 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, since you are new to C#, I do not recommend you dive into GDI+ with all the painting stuff immediately. I do recommend you read and work your way through an introductory book on C# though (possibly skipping some of the chapters you dont immediately need, e.g. GDI+, databases, networking). If you dont need to handle very large files, then probably the RichTextBox approach is best for you. It has its quirks, but you will learn from trying, and possibly asking here. If on the other hand you do get curious, dont forget to search on CodeProject. Searching "paint text" in the search box above, resulted in many articles, including this lenghty book chapter.[^] Enjoy C# !

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            B Offline
            B Offline
            Blekk
            wrote on last edited by
            #5

            Ok thanks, I'll have a look at that chapter later. I have already learnt lots on ado.net and database work in C# and so now want to move into something different. When I said I was just beginning, I meant beginning GDI stuff, not C#. But thankyou for the links.

            L 1 Reply Last reply
            0
            • B Blekk

              Ok thanks, I'll have a look at that chapter later. I have already learnt lots on ado.net and database work in C# and so now want to move into something different. When I said I was just beginning, I meant beginning GDI stuff, not C#. But thankyou for the links.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              You're welcome.

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups