Improve RichTextBox performance
-
Hello, I just found out that, my editor made of rtb lags really bad when the number of lines is longer than 100 for example. Could it be because im using the syntax highlighting textbox library to highlight syntax inside the editor? Is it possible to increase the performance of it somehow so it doesnt lag ever sec as you type.
-
Hello, I just found out that, my editor made of rtb lags really bad when the number of lines is longer than 100 for example. Could it be because im using the syntax highlighting textbox library to highlight syntax inside the editor? Is it possible to increase the performance of it somehow so it doesnt lag ever sec as you type.
I am not surprised since a textbox is holding all its data in one string; doing anything fancy to it does not scale well, it is at least quadratic: when the text grows, so does the number of operations required, AND the cost of each operation (creating a new string copying all the characters). I see no easy way out; I did my own line-oriented text editor (with syntax highlighting and everyting) in a plain Panel; and I do all screen-based logging to a ListBox. I am not a big fan of either TextBoxes or RichTextBoxes because of their degrading performance. I only use them for short texts (say 50 lines), and preferably texts that dont change. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
I am not surprised since a textbox is holding all its data in one string; doing anything fancy to it does not scale well, it is at least quadratic: when the text grows, so does the number of operations required, AND the cost of each operation (creating a new string copying all the characters). I see no easy way out; I did my own line-oriented text editor (with syntax highlighting and everyting) in a plain Panel; and I do all screen-based logging to a ListBox. I am not a big fan of either TextBoxes or RichTextBoxes because of their degrading performance. I only use them for short texts (say 50 lines), and preferably texts that dont change. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
How can i create a line-oriented text editor of my own with syntax highlighting ability?
If you are not happy with what you have, I can tell you how I did it, but I must warn you, it has been a big effort. I did it as part of a larger project; relevant steps include: - text lines stored in memory basically in an ArrayList, one item per line; - each line also holds two flags to indicate comment status; these get set when the line is input/changed; - text drawn in a Panel, using OnPaint handler, and Graphics.DrawString; - all coloring/syntax highlighting handled inside OnPaint, based on a simple parser that knows the list of keywords (and their color) for each of the supported programming languages. Steps taken to get maximum performance: - only the visible lines are parsed, colored and painted; lines scrolled outside the panel are skipped (except for the comment flags). A typical Panel shows up to 60 lines, a very small number with respect to an entire source file. - the comment flags help in starting the first visible line either as continued comment (green background) or not. - the simple parsers are not full fledged parsers, they dont understand expressions, dont return a parse tree, etc. They must only process a single line and know about comments, string literals, and keywords. - the parsers return tokens (identifiers, constants, operators, etc) as a string plus Color; Graphics.DrawString paints these tokens individually. - as a result of all this, nearly linear behavior, and no delay whatsoever. All in all a couple thousand lines of C# code (and based on a growing library of low-level classes that I use in all my apps and that is not included in the estimate). Hope this clarifies things. :)
Luc Pattyn [My Articles] [Forum Guidelines]