정규식을 사용하여 Richtextbox Editor를 작성하여 몇 가지 특정 단어를 형식화합니다. 내가 코드 아래 사용하고 있습니다 : 텍스트 편집에 대한 지속적인 깜박임이 있었다C#에서 richtextbox를 사용하여 Regex를 사용하는 중에 예외가 발생했습니다.
private void myrichTextBox1_TextChanged(object sender, EventArgs e)
{
// getting keywords/functions
string keywords = @"\b(public|private|sendln|static|namespace|Wait|using|void|foreach|in|OK|ERROR)\b";
MatchCollection keywordMatches = Regex.Matches(richTextBox1.Text, keywords);
// getting types/classes from the text
string types = @"\b(Console)\b";
MatchCollection typeMatches = Regex.Matches(richTextBox1.Text, types);
// getting comments (inline or multiline)
string comments = @"(\/\/.+?$|\/\*.+?\*\/)";
MatchCollection commentMatches = Regex.Matches(richTextBox1.Text, comments, RegexOptions.Multiline);
// getting strings
string strings = "\".+?\"";
MatchCollection stringMatches = Regex.Matches(richTextBox1.Text, strings);
// saving the original caret position + forecolor
int originalIndex = richTextBox1.SelectionStart;
int originalLength = richTextBox1.SelectionLength;
Color originalColor = Color.Black;
// MANDATORY - focuses a label before highlighting (avoids blinking)
titleLabel.Focus();
// removes any previous highlighting (so modified words won't remain highlighted)
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = richTextBox1.Text.Length;
richTextBox1.SelectionColor = originalColor;
// scanning...
foreach (Match m in keywordMatches)
{
richTextBox1.SelectionStart = m.Index;
richTextBox1.SelectionLength = m.Length;
richTextBox1.SelectionColor = Color.Blue;
}
foreach (Match m in typeMatches)
{
richTextBox1.SelectionStart = m.Index;
richTextBox1.SelectionLength = m.Length;
richTextBox1.SelectionColor = Color.DarkCyan;
}
foreach (Match m in commentMatches)
{
richTextBox1.SelectionStart = m.Index;
richTextBox1.SelectionLength = m.Length;
richTextBox1.SelectionColor = Color.Green;
}
foreach (Match m in stringMatches)
{
richTextBox1.SelectionStart = m.Index;
richTextBox1.SelectionLength = m.Length;
richTextBox1.SelectionColor = Color.Brown;
}
// restoring the original colors, for further writing
richTextBox1.SelectionStart = originalIndex;
richTextBox1.SelectionLength = originalLength;
richTextBox1.SelectionColor = originalColor;
// giving back the focus
richTextBox1.Focus();
}
때문에, 따라서 titleLabel.Focus();
레이블에 포커스를 이동했다. 그러나 발사에 예외 아래의 기부 형식 'System.StackOverflowException'의
처리되지 않은 예외가 system.windows.forms.dll에서 발생했습니다.
titleLabel.Focus();
없이는 예외가 있지만 계속 깜박입니다.
TextChanged 이벤트에서 선택 속성을 변경하면 TextChanged 이벤트 ... ad infinitum을 발생시키는 선택 속성을 변경하는 TextChanged 이벤트가 발생합니다. [WinForms RichTextBox : TextChanged 이벤트를 발생시키지 않고 비동기 적으로 다시 포맷하는 방법]을 참조하십시오. (http://stackoverflow.com/questions/1457411/winforms-richtextbox-how-to-reformat-asynchronously-without-firing-) –
감사합니다. Yuriy for proper 형성. @ 알렉스, 여전히 솔루션에 대한 혼란 스러워요. –