Dynamic textcolor change in RichTextBox in visual C#
-
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.
-
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.
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] }
-
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] }
-
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.
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] }
-
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] }
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.
-
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.
You're welcome.
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }