System.Windows.Forms.dll中发生了未处理的“System.StackOverflowException”类型异常

时间:2010-05-11 17:20:00

标签: c# syntax richtextbox syntax-highlighting

好的,我试图在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);

3 个答案:

答案 0 :(得分:2)

您可能通过此代码触发VScroll事件,以便再次调用您的代码,然后再次触发事件,然后再次调用,依此类推,最后堆栈结束。

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

答案 1 :(得分:1)

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

答案 2 :(得分: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); }


    }