如何将RichTextBox注释内文本设为注释颜色?注释仅识别/与-
Got it, let's work through these two issues with Patrik's SyntaxRichTextBox from CodeProject. I've dealt with similar setups before, so here's how to fix both problems:
Problem 1: Coloring the Entire Comment Text (Not Just Markers)
It sounds like the default setup only applies your CommentColor to the comment symbols themselves, not the full text of the comment. To fix this, you’ll need to tweak the syntax parsing logic to extend the colorization from the comment start marker to the end of the line (or closing block comment, if you need that).
If you have access to the control’s source code, here’s a straightforward way to adjust the line-comment parsing:
private void ParseLine(string line, int lineIndex) { int currentPos = 0; bool inComment = false; while (currentPos < line.Length) { if (!inComment) { // Check for line comment start markers (adjust these to match your needs) if (line.StartsWith("//", currentPos) || line.StartsWith("#", currentPos)) { // Color EVERYTHING from this marker to the end of the line SetTextColor(lineIndex, currentPos, line.Length - currentPos, M_syntaxRichTextBox.Settings.CommentColor); inComment = true; break; // No need to parse further—rest of the line is a comment } // Add block comment checks here if you need /* ... */ support } // Handle other syntax (keywords, strings, etc.) only if not in a comment currentPos++; } }
If you don’t want to modify the source, double-check if the control has a hidden setting like CommentFullLineColor—some versions include this, but if not, source modification is the most reliable fix.
Problem 2: Adding More Comment Identifiers
The default implementation probably only hardcodes / and - as comment triggers. To add more symbols (like #, --, or //), you’ll need to update the marker detection logic.
First, define all the comment markers you want to support, then update the parsing code to check for them:
// Inside your line-parsing method if (!inComment) { // Add all your desired comment markers here var commentMarkers = new List<string> { "//", "#", "--", "/*" }; foreach (var marker in commentMarkers) { if (line.StartsWith(marker, currentPos)) { // Color the marker and the rest of the comment (adjust for block comments) SetTextColor(lineIndex, currentPos, line.Length - currentPos, M_syntaxRichTextBox.Settings.CommentColor); inComment = true; break; } } }
If the control has a configurable property for comment markers (like Settings.CommentMarkers), you can populate that instead of hardcoding:
// Add this right after enabling comments M_syntaxRichTextBox.Settings.CommentMarkers = new List<string> { "//", "#", "--", "/*" };
Bonus: Block Comment Support
If you need multi-line block comments (e.g., /* ... */), you’ll need to track the comment state across lines with a class-level variable:
private bool inBlockComment = false; private void ParseLine(string line, int lineIndex) { int currentPos = 0; while (currentPos < line.Length) { if (inBlockComment) { // Check for block comment end if (line.StartsWith("*/", currentPos)) { SetTextColor(lineIndex, currentPos, 2, M_syntaxRichTextBox.Settings.CommentColor); currentPos += 2; inBlockComment = false; } else { // Color every character in the block comment SetTextColor(lineIndex, currentPos, 1, M_syntaxRichTextBox.Settings.CommentColor); currentPos++; } } else { // Check for block comment start if (line.StartsWith("/*", currentPos)) { SetTextColor(lineIndex, currentPos, 2, M_syntaxRichTextBox.Settings.CommentColor); currentPos += 2; inBlockComment = true; } // Check line comments here... } } }
内容的提问来源于stack exchange,提问作者user9457114

