【问题标题】:An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dllSystem.Windows.Forms.dll 中出现“System.StackOverflowException”类型的未处理异常
【发布时间】:2010-05-12 01:31:00
【问题描述】:

好的,我试图在 Richtextbox 中突出显示关键字,问题是我的代码仅突出显示 textChanged 事件上的可见文本,所以我尝试将代码放在richtextbox VScroll 中,所以当我向上滚动时会突出显示以前不可见的文本,但是每次我开始滚动时都会收到此错误:“System.Windows.Forms.dll 中发生了'System.StackOverflowException'类型的未处理异常” 有​​人知道为什么吗?或者也许我可以在滚动时突出显示单词? 谢谢,坦纳。

        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);

【问题讨论】:

  • 一般提示:堆栈溢出几乎总是由无限递归引起的。

标签: c# syntax richtextbox syntax-highlighting


【解决方案1】:

您可能正在通过此代码触发 VScroll 事件,因此您的代码被再次调用,然后再次触发事件,并再次被调用,依此类推,最终您的堆栈结束。

更具体地说,我需要在异常时查看您的调用堆栈。

【讨论】:

    【解决方案2】:

    几乎可以肯定是一个事件循环。可能是richTextBox1.select() 调用导致小部件尝试滚动,这会发出一个新的VScroll 事件,无限期(或广告堆栈空间)。有多种处理方法,但最简单的通常是在第一次通过事件时设置一个标志,然后将处理代码包装在条件中,以便仅在未设置标志时执行。

    【讨论】:

    • +1 用于包含解决方案,以及“广告堆栈空间”——“ad nauseum”的计算机版本。
    【解决方案3】:

    好的,您需要确切地看到什么?这是我的完整代码:

     [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); }
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多