How to Color RichTextBox Content for programming Purpose like Notepad++

Elrouby

New member
Joined
Sep 17, 2019
Messages
1
Programming Experience
Beginner
I am trying to make a GameFiles Editor
How to color between " " like string in Visual Studio or Notepad++ C# Mode
Without using Selecting then Coloring Selection Properties
This way is so slow and show the User when coloring the lines so Boring and Use lots of CPU Average
Can any one tell me please how?

 
Pardon the pun, but you've boxed yourself in by restricting yourself to using the RichTextBox. By choosing that as your base platform, you'll basically have to use the APIs made available to you by that control.

Chances are that your slow performance and high CPU usage is due to one or more of the following:
* not disabling re-draws while you are formatting/coloring sections
* inefficient algorithm for search for the strings to format

The CPU shouldn't really be pegged by a single pass of formatting even using the managed APIs. I suspect that you are doing something wrong.

If you are willing to look beyond the RichTextBox, there is ScintillaNET.
 
If you are willing to look beyond the RichTextBox, there is ScintillaNET.

Let it be noted, this is what Notpad ++ is based on, If I'm not mistaken it for another one and as far as I am aware, without clicking the link, they provide a public library?
 
On a side note, you can also use multi-threading which should help in some places. You would also be best doing this in WPF, as I believe it is actually easier, and also more suitable UI wise in regards performance. On the downside, not everything in WPF is as it is in Winforms, but if you're a sharp dev, you should have no problem picking up some new habits and ways of doing things. If you're persistent to stick with a dated and nearing end of life platform such as wf. You will need to use the append text method which allows you to set text with a color. Loops and hashtables, atleast that's how I'd go about it in wf.

Curious, you say in one breath : How to color between " " like string in Visual Studio or Notepad++ but then say : This way is so slow and show the User when coloring the lines so Boring and Use lots of CPU. Implies you're asking how to do something you've tried. Why not show us what you've tried, and maybe we can give you some pointers?

As for this : How to color between " " and speaking of pointers - You need to get the positions in text where a string is enclosed in between two symbols. Consider that a RichTextBox has a Text.Length property. You could use this to identify the end point for " along with String.IndexOf Method (System) to find the start point of "
 
Last edited:
Also, If you're running on a slow PC or previous versions of Windows 8, and if your app has to much going on in the UI, you could try using ws_ex_composited to reduce flicker or scaling along with double buffering on other controls that support that feature. Its not widely known that Aero explicitly does not implement or support WS_EX_COMPOSITED unless DwmEnableComposition function is set as disabled (only for versions before Windows 8). Essentially this is for turning off Aero glass with the desktop window manager, and it doesn't work on newer windows versions. But I'd imagine if you are building an app like Notepad++ or VS, it will meet the requirements of modern functionality and not follow the principles of this post. ;)

Just thought that would be helpful for anyone using or targeting older versions of windows.
 
Back
Top Bottom