 |
|
 |
when I comment out GetscrollPos in C# or vb.net it does not affect multi-line coloring.
Is there a reason why it is coloring only ToEOL when it is converted to Vb.net?
(Trying to color for Chr(34) and C# didn't have that symbol)
|
|
|
|
 |
|
|
 |
|
 |
idiot - at the very least explain then problems you encounter
|
|
|
|
 |
|
 |
Many thanks for this - it has saved me lots of work. (I'm writing an SQL client, so highlighting key words)
|
|
|
|
 |
|
 |
Instead of in a separated class. I just erased the SyntaxSettings class and put all the settings in SyntaxRichTextBox, so now I can edit them in the Properties panel. A lot handier . Great job by the way.
|
|
|
|
 |
|
 |
Hello (again),
another question: how can I edit the properties in the (form) editing mode (not like actually in the code section?) Is there a way to realize this?
Best regards
Tim
|
|
|
|
 |
|
 |
Just move all the settings from SyntaxSettings to SyntaxRichTextBox and then erease all occurrences of "Settings." for "". Voila! (Well, almost actually there is some stuff to be done still).
|
|
|
|
 |
|
 |
Can't enabled the script to work within an "if" statement..
|
|
|
|
 |
|
 |
You say that performance problems solved but only during Load file Ok.
But do you have a solution when you change the text after loading ?
I try the same way with the property selectedRtf but it's to slow because I must use the select method. :(
|
|
|
|
 |
|
 |
do you mean to clear the textbox and paste new text from clipboard?
for this problem i dont have an solution ... (at the moment) sorry
|
|
|
|
 |
|
 |
hi ...
i was searching a lot for the reasing why the syntaxhighlight is that slow.
the problem is not the regex, but the richTextBox.Select. it takes a lomg time to select each keyword an change its color.
so i wrote a helper-class to color very large documents.
it uses the same Settings and should ONLY be used to perform the 'init-coloring'.
example: SyntaxTester -> MainForm -> MainFormLoad()
[...]
// Load a file and update the syntax highlighting.
m_syntaxRichTextBox.LoadFile("../script.lua", RichTextBoxStreamType.PlainText);
//m_syntaxRichTextBox.ProcessAllLines();
SyntaxHighlighter.RtfColoring.ProcessRtfColoring(m_syntaxRichTextBox);
all other methods are ok. so for the first init i use the rtf-coloring and for live-editing i usw the existing richTextBox.Select(...
using System;
using System.Text.RegularExpressions;
namespace SyntaxHighlighter
{
/// <summary>
/// enabes syntaxhighlighting for a richtextbox by using rtf
/// </summary>
public static class RtfColoring
{
public static void ProcessRtfColoring(SyntaxRichTextBox richTextBox)
{
SyntaxSettings Settings = richTextBox.Settings;
if (Settings.Keywords.Count < 1)
return;
// reset the text to get a clear rtf
string strTextToAdd = richTextBox.Text;
richTextBox.Clear();
richTextBox.AppendText(strTextToAdd);
string strRTF = richTextBox.Rtf;
// find index of start of header
int iRTFLoc = strRTF.IndexOf("\\rtf");
// get index of where we'll insert the colour table
// try finding opening bracket of first property of header first
int iInsertLoc = strRTF.IndexOf('{', iRTFLoc);
// if there is no property, we'll insert colour table
// just before the end bracket of the header
if (iInsertLoc == -1) iInsertLoc = strRTF.IndexOf('}', iRTFLoc) - 1;
string strCommentColor = "\\red" + Settings.CommentColor.R.ToString() +
"\\green" + Settings.CommentColor.G.ToString() +
"\\blue" + Settings.CommentColor.B.ToString();
string strStringColor = "\\red" + Settings.StringColor.R.ToString() +
"\\green" + Settings.StringColor.G.ToString() +
"\\blue" + Settings.StringColor.B.ToString();
string strKeywordColor = "\\red" + Settings.KeywordColor.R.ToString() +
"\\green" + Settings.KeywordColor.G.ToString() +
"\\blue" + Settings.KeywordColor.B.ToString();
string strIntegerColor = "\\red" + Settings.IntegerColor.R.ToString() +
"\\green" + Settings.IntegerColor.G.ToString() +
"\\blue" + Settings.IntegerColor.B.ToString();
// insert the colour table at our chosen location
strRTF = strRTF.Insert(iInsertLoc, "{\\colortbl ;" + strCommentColor + ";" + strStringColor + ";" + strKeywordColor + ";" + strIntegerColor + ";}");
// build the keywords regex
string strKeywords = String.Empty;
foreach (string keyword in Settings.Keywords)
{
strKeywords += keyword + ",";
}
strKeywords = strKeywords.Substring(0, strKeywords.Length - 1);
Regex r = new Regex(@", ?");
strKeywords = @"\b(" + r.Replace(strKeywords, @"|") + @")\b";
// start coloring
// Keywords
r = new Regex(strKeywords, RegexOptions.Singleline | RegexOptions.IgnoreCase);
strRTF = r.Replace(strRTF, new MatchEvaluator(MatchKeyword));
// Integers
r = new Regex("(\\b(?:[0-9]*\\.)?[0-9]+\\b)", RegexOptions.IgnoreCase);
strRTF = r.Replace(strRTF, new MatchEvaluator(MatchInteger));
// Comments
r = new Regex("("+Settings.Comment+".*$)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
strRTF = r.Replace(strRTF, new MatchEvaluator(MatchComment));
// Strings
r = new Regex("(\"[^\"\\\\\\r\\n]*(?:\\\\.[^\"\\\\\\r\\n]*)*\"|\'[^\'\\\\\\r\\n]*(?:\\\\.[^\'\\\\\\r\\n]*)*\')", RegexOptions.Singleline | RegexOptions.IgnoreCase);
strRTF = r.Replace(strRTF, new MatchEvaluator(MatchString));
richTextBox.Rtf = strRTF;
}
private static string MatchKeyword(Match match)
{
if (match.Groups[1].Success)
{
return @"\cf3 " + RemoveRtfColors(match.Value) + @"\cf0 ";
}
return String.Empty;
}
private static string MatchString(Match match)
{
if (match.Groups[1].Success)
{
return @"\cf2 " + RemoveRtfColors(match.Value) + @"\cf0 ";
}
return String.Empty;
}
private static string MatchComment(Match match)
{
if (match.Groups[1].Success)
{
return @"\cf1 " + RemoveRtfColors(match.Value) + @"\cf0 ";
}
return String.Empty;
}
private static string MatchInteger(Match match)
{
if (match.Groups[1].Success)
{
return @"\cf4 " + RemoveRtfColors(match.Value) + @"\cf0 ";
}
return String.Empty;
}
/// <summary>
/// remove all rtf-colors from a string
/// </summary>
/// <param name="strText"></param>
/// <returns>String</returns>
private static string RemoveRtfColors(string strText)
{
Regex r = new Regex(@"\\cf[0-9] ");
strText = r.Replace(strText, "");
return strText;
}
}
}
have fun
|
|
|
|
 |
|
 |
Where do I put that code? I made a new class and put the code you pasted in there. The speed is still slow though, what do I need to do afterwards?
Sorry, I am new to C# so it's difficult for me
|
|
|
|
 |
|
 |
Good Work man, super fast now, congrats to the original control author
|
|
|
|
 |
|
 |
Could you show me / upload how this is supposed to be?
Thanks
Email:
ZoMbiESsSZ@Gmail.com
|
|
|
|
 |
|
 |
your code doesn't color first line in rtb?
|
|
|
|
 |
|
 |
Hi,
I tried this too but it makes it quite complex.
I have a simple solution which is to use a temporary richTextBox which is not shown and do the changes to it.
Finally copy rtTemp RTF back to original richTextBox.
The speed is not incredibly better but no flicker.
Here is how I did it:
private static bool isAutoChange = false;
private void richTextBox_TextChanged(object sender, EventArgs e)
{
if (!isAutoChange)
Highlight();
}
private void Highlight()
{
RichTextBox rtTemp = new RichTextBox();
rtTemp.Font = richTextBox.Font;
rtTemp.Rtf = richTextBox.Rtf;
rtTemp.Visible = false;
rtTemp.HideSelection = true;
isAutoChange = true;
richTextBox.Rtf = rtTemp.Rtf;
isAutoChange = false;
}
--
Cui Bono?
|
|
|
|
 |
|
 |
Hello,
Thanks for your article and sources, it works great!
I have a problem when I try to use it to color code in SAS language. The following keywords are not colored, perhaps because of the special character percent: "%let", "%nrbquote("?
Comment lines starting with the star character are not colored either. Would you have any idea to fix this problem?
txt.Settings.Keywords.Add("%let");
txt.Settings.Keywords.Add("%nrbquote(");
txt.Settings.Keywords.Add(");");
txt.Settings.Comment = "/*";
txt.Settings.Comment = "*";
Thank you in advance
Julien
|
|
|
|
 |
|
 |
I am using it for a SQL editor and works great
Kevin S. Gallagher
Programming is an art form that fights back
|
|
|
|
 |
|
|
 |
|
 |
Hi,
can you please post also the extra function for the comments.
Thanks
|
|
|
|
 |
|
 |
well, I changed the code again and now its very simple:
now i use the old methode for integers, strings and commentsprivate void ProcessRegex(string strRegex, Color color)
{
Regex regKeywords = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
Match regMatch;
for (regMatch = regKeywords.Match(m_strLine); regMatch.Success; regMatch = regMatch.NextMatch())
{
int nStart = m_nLineStart + regMatch.Index;
int nLenght = regMatch.Length;
SelectionStart = nStart;
SelectionLength = nLenght;
SelectionColor = color;
}
}
and just the new one for highlightingprivate void HighLightRegex(string strRegex, Color color)
{
Regex regKeywords = new Regex(@"\w+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Match regMatch;
for (regMatch = regKeywords.Match(m_strLine); regMatch.Success; regMatch = regMatch.NextMatch())
{
string keyword;
if (Settings.Keywords.TryGetValue(regMatch.Value, out keyword))
{
int nStart = m_nLineStart + regMatch.Index;
int nLenght = regMatch.Length;
SelectionStart = nStart;
SelectionLength = nLenght;
SelectionColor = color;
}
}
}
it's still faster then the original code, because the comments, integers and strings aren't using the keywords.
Also I changed the strRegex for strings. Now it also highlights chars (e.g. 'a'):"(\"[^\"\\\\\\r\\n]*(?:\\\\.[^\"\\\\\\r\\n]*)*\"|\'[^\'\\\\\\r\\n]*(?:\\\\.[^\'\\\\\\r\\n]*)*\')"
if there is an easier way: post reply, please
no_morser
|
|
|
|
 |
|
 |
Hello,
I'm trying to add your new code. But now I've got the problem, that the line with "TryGetValue" occurs an error:
Error1 "System.Collections.Generic.List" has now definition for "TryGetValue",and there is no expanded method with "TryGetValue" found in the code, there is no argument "System.Collections.Generic.List" accepted. (Maybe there are some missing Using-Directive or Assembly?)
the using-directive "using System.Collections.Generic;" is set, so maybe you know whats wrong? Or you update you Download-Code to see whats wrong (Maybe I placed it wrong?!? (I place your new routine right after the old one)
Best regards
Tim
|
|
|
|
 |
|
 |
Can you explain more about this string: "\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\""
Thanks
Tran Ho Le NGuyen
|
|
|
|
 |
|
 |
just like visual studio highlight, the functions are highlighted in different colors as keywords.
it is easy to add this term.
|
|
|
|
 |
|
 |
Hi.
I'm using your excellent control inside my editor.
I've just dragged it to my MainForm and I've registered a method (MainForm_TextChanged)to the TextChanged event.
Then I've implemented MainForm_TextChanged.
But it seems that the application doesn't reach this method when I changes the text of the SyntaxRichTextBox. Do you have any idea what privents MainForm of listening to the TextChanged event?
|
|
|
|
 |