views:

266

answers:

3

OK, Im trying to highlight keywords in a richtextbox, the problem is I've got the code to highlight only the visible text on textChanged event,so I tryed putting the code in the richtextbox VScroll, so when I scrolled up it would highlight the text that wasn't visible before, but every time I start to scroll I get this error: "An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll" Does any one know why? Or maybe a way I could highlight the words while scrolling? Thanks, Tanner.

        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);
+1  A: 

You're probably triggering the VScroll event by this code, so that your code gets called again, and then triggers the event again, and gets called again, and so on, and so in the end your stack ends.

To tell more specifically, I need to see your call stack at the moment of exception.

Fyodor Soikin
+1  A: 

Almost certainly an event loop. Probably the richTextBox1.select() call is causing the widget to attempt to scroll, which issues a new VScroll event, ad infinitum (or ad stack space). There are various ways to handle this, but the easiest is usually to set a flag on the first time through the event, then wrap your handling code in a conditional so it only executes if the flag isn't set.

Dan Menes
+1 for including the solution, and "ad stack space"—the computer version of "ad nauseum".
Jeffrey L Whitledge
A: 

Ok what do you need to see exactly? Here is my full code:

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


    }
Tanner