2014-11-06 2 views
1

정규식을 사용하여 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(); 없이는 예외가 있지만 계속 깜박입니다.

+4

TextChanged 이벤트에서 선택 속성을 변경하면 TextChanged 이벤트 ... ad infinitum을 발생시키는 선택 속성을 변경하는 TextChanged 이벤트가 발생합니다. [WinForms RichTextBox : TextChanged 이벤트를 발생시키지 않고 비동기 적으로 다시 포맷하는 방법]을 참조하십시오. (http://stackoverflow.com/questions/1457411/winforms-richtextbox-how-to-reformat-asynchronously-without-firing-) –

+0

감사합니다. Yuriy for proper 형성. @ 알렉스, 여전히 솔루션에 대한 혼란 스러워요. –

답변

1

가장 좋은 방법은 서식 지정 코드를 변경하여 텍스트의 변경 내용을 실제로 작성해야하는지 파악하고 텍스트 상자 내용이 변경된 경우에만 적용하는 것입니다. 이 방법은 불필요한 업데이트를 피함으로써 컨트롤의 효율성을 향상시킵니다.

다소 복잡하지만 구현하기가 훨씬 쉬운 대체 방법은 이벤트를 처리하는 중일 때 플래그를 설정하는 것이므로 이벤트가 다시 발생하면 이벤트를 무시하는 것이 좋습니다. 예 :

private bool _updatingTextBox; 

private void myrichTextBox1_TextChanged(object sender, EventArgs e) 
{ 
    if (_updatingTextBox) 
    { 
     return; 
    } 

    _updatingTextBox = true; 

    try 
    { 
     // all your updating code goes here 
    } 
    finally 
    { 
     _updatingTextBox = false; 
    } 
}