【发布时间】:2016-12-01 17:50:21
【问题描述】:
我正在与this code 合作。它用于在RichTextBox 中突出显示语法。我专门查看函数ProcessLine() 和OnTextChanged(),我已将其修改为:
protected override void OnTextChanged(EventArgs e)
{
// Calculate stuff here.
m_nContentLength = this.TextLength;
int nCurrentSelectionStart = SelectionStart;
int nCurrentSelectionLength = SelectionLength;
m_bPaint = false;
// Find the start of the current line.
m_nLineStart = nCurrentSelectionStart;
while ((m_nLineStart > 0) && (Text[m_nLineStart - 1] != '\n'))
m_nLineStart--;
// Find the end of the current line.
m_nLineEnd = nCurrentSelectionStart;
while ((m_nLineEnd < Text.Length) && (Text[m_nLineEnd] != '\n'))
m_nLineEnd++;
// Calculate the length of the line.
m_nLineLength = m_nLineEnd - m_nLineStart;
// Get the current line.
m_strLine = Text.Substring(m_nLineStart, m_nLineLength);
// Process this line.
ProcessLine();
m_bPaint = true;
}
// Process a line.
private void ProcessLine()
{
// Save the position and make the whole line black
int nPosition = SelectionStart;
SelectionStart = m_nLineStart;
SelectionLength = m_nLineLength;
SelectionColor = Color.Black;
/*// Process the keywords
ProcessRegex(m_strKeywords, Settings.KeywordColor);
// Process numbers
if(Settings.EnableIntegers)
ProcessRegex("\\b(?:[0-9]*\\.)?[0-9]+\\b", Settings.IntegerColor);
// Process strings
if(Settings.EnableStrings)
ProcessRegex("\"[^\"\\\\\\r\\n]*(?:\\\\.[^\"\\\\\\r\\n]*)*\"", Settings.StringColor);
// Process comments
if(Settings.EnableComments && !string.IsNullOrEmpty(Settings.Comment))
ProcessRegex(Settings.Comment + ".*$", Settings.CommentColor);*/
SelectionStart = nPosition;
SelectionLength = 0;
SelectionColor = Color.Red;
m_nCurSelection = nPosition;
}
我的第一个问题是,当我在
OnTextChanged()中输入ProcessLine()时,m_strLine的末尾是否总是有一个换行符?最小值或m_strLine会是“\n”和最大的“any#ofchars+\n”吗?-
所以我有这个权利,如果
SelectionLength为零,则SelectionStart是我的插入符号位置,如果SelectionLength大于零,我的插入符号位于SelectStart+SelectionLength? 我正在尝试修改此代码以为大量不同的语法表达式着色,并且我计划为每一行一次处理一个字符。粘贴或加载超过 20k 行的文件时,这如何公平?
【问题讨论】:
-
这是一种非常基本的方法,不适用于较大的 sn-ps。它将非常缓慢,并且有很多闪烁和怪癖。与开箱即用的 RTF 控件相比,从更好的基础开始会更好!
-
我最好尝试正则表达式每一行,就像他们在这里尝试做的那样?
-
另外,只有在加载文件时,我才必须遍历每一行。另一方面,在编辑文件时,我一次只需要检查一行,但仍会重新绘制,但只有用户可以在 textarea 中看到的文本区域。这真的会像大家想象的那么慢吗?
-
如果你想自己做 Costin Boldisor 在这里有一篇博文blogs.msdn.microsoft.com/cobold/2011/01/31/…
标签: c# richtextbox