2010-05-11 11 views
0

좋아요, richtextbox에서 키워드를 강조하려고하는데 문제는 textChanged에 표시된 텍스트 만 강조 표시하는 코드가 있다는 것입니다. 이벤트, 그래서 richtextbox VScroll에 코드를 넣으려고 했으므로 스크롤 할 때 전에는 볼 수 없었던 텍스트가 강조 될 것이지만 스크롤 할 때마다이 오류가 발생합니다. "처리되지 않은 'System .StackOverflowException 'System.Windows.Forms.dll에서 발생했습니다. "이유를 아는 사람이 있습니까? 아니면 스크롤하면서 단어를 강조 표시하는 방법일까요? 고마워, 태너.처리되지 않은 'System.StackOverflowException'형식의 예외가 System.Windows.Forms.dll에서 발생했습니다.

 int selectionstart = richTextBox1.Selectionstart; 
     int topIndex = richTextBox1.GetCharIndexFromPosition(new Point(1, 1));//This is where I get the error. 
     int bottomIndex = richTextBox1.GetCharIndexFromPosition(new Point(1, richTextBox1.Height - 1)); 

     int topLine = richTextBox1.GetLineFromCharIndex(topIndex); 
     int bottomLine = richTextBox1.GetLineFromCharIndex(bottomIndex); 

     int start = richTextBox1.GetFirstCharIndexFromLine(topLine); 
     int end = richTextBox1.GetFirstCharIndexFromLine(bottomLine); 

     int numLinesDisplayed = (bottomLine - topLine) + 2; 
     richTextBox1.Focus(); 
     richTextBox1.Select(start, end); 
+2

일반 팁 : 스택 오버플로는 거의 항상 무한 재귀에 의해 발생합니다. – Syntactic

답변

2

당신은 결국 당신의 스택 등등, 그래서 코드가 다시 불려 가도록 (듯이), 아마,이 코드에 의해 VScroll 이벤트를 트리거 한 후 다시 이벤트를 트리거하고 다시 호출되는, 그리고있어 끝.

더 구체적으로 말하면, 예외가 발생하는 순간 호출 스택을 확인해야합니다.

1

거의 확실히 이벤트 루프. 아마도 richTextBox1.select() 호출로 인해 위젯이 스크롤하려고 시도하고 새로운 VScroll 이벤트 인 ad infinitum (또는 광고 스택 공간)을 발행합니다. 이것을 처리 할 수있는 여러 가지 방법이 있지만 가장 쉬운 방법은 일반적으로 이벤트를 통해 처음으로 플래그를 설정 한 다음 핸들러 코드를 조건부로 래핑하여 플래그가 설정되지 않은 경우에만 실행합니다.

+0

+1, "광고 스택 공간"- 컴퓨터 버전의 "광고 nauseum". –

0

좋아요. 정확히 무엇을보고 싶습니까? 여기 내 코드는 다음과 같습니다.

[DllImport("user32.dll")] // import lockwindow to remove flashing 
    public static extern bool LockWindowUpdate(IntPtr hWndLock); 

public void Markup(RichTextBox RTB) 
    { 
     try 
     { 
      int selectionstart = richTextBox1.SelectionStart; 
      Point pos = richTextBox1.Location; 
      richTextBox1.Focus(); 
      int topIndex = richTextBox1.GetCharIndexFromPosition(new Point(1, 1)); 
      //int topIndex = richTextBox1.GetCharIndexFromPosition(point); 
      int bottomIndex = richTextBox1.GetCharIndexFromPosition(new Point(1, richTextBox1.Height - 1)); 

      int topLine = richTextBox1.GetLineFromCharIndex(topIndex); 
      int bottomLine = richTextBox1.GetLineFromCharIndex(bottomIndex); 

      int start = richTextBox1.GetFirstCharIndexFromLine(topLine); 
      int end = richTextBox1.GetFirstCharIndexFromLine(bottomLine); 

      int numLinesDisplayed = (bottomLine - topLine) + 2; 
      richTextBox1.Focus(); 
      richTextBox1.Select(start, end); 



      Regex rex = new Regex("<html>|</html>|<head.*?>|</head>|<body.*?>|</body>|<div.*?>|</div>|<span.*?>|</span>|<title.*?>|</title>|<style.*?>|</style>|<script.*?>|</script>|<link.*?/>|<meta.*?/>|<base.*?/>|<center.*?>|</center>"); 
      foreach (Match m in rex.Matches(richTextBox1.SelectedText)) 
      { 
       richTextBox1.Select(m.Index + start, m.Value.Length); 
       richTextBox1.SelectionColor = Color.Blue; 
       richTextBox1.Select(selectionstart, -1); 
       richTextBox1.SelectionColor = Color.Black; 
      } 
      richTextBox1.DeselectAll(); 
      richTextBox1.SelectionStart = selectionstart; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error: " + ex); 
     } 
    } 


    private void richTextBox1_VScroll(object sender, EventArgs e) 
    { 


      try 
      { 


       LockWindowUpdate(richTextBox1.Handle);//Stop flashing 
       Markup(richTextBox1); 
       Elements(richTextBox1); 
       FormsTabels(richTextBox1); 
       Attributes(richTextBox1); 
       Comments(richTextBox1); 

      } 
      finally { LockWindowUpdate(IntPtr.Zero); } 


    } 
관련 문제