0

I have a RichTextBox that sometimes contains a large amount of text requiring scrolling to see all of it. Once the text is loaded/entered and a button is clicked the application steps through the text, a character at a time, making changes.

I am trying to scroll the contents of the RichTextBox to keep the row on which the change is currently taking place in the middle of the box.

I can move the caret to the top, and to the bottom, which moves the text down and puts the caret at the top of the box or moves it up and puts the caret at the bottom of the box respectively.

I can place the caret at a given character but depending on where that character is on in the visible section (within the text box) of text is the caret could be , near the top or near the bottom of the box, How do I move the whole of the text so that the line the caret is on, is scrolled to the vertical middle of the box?

Hope all that makes sense.

4

1 回答 1

0

为了模拟“做出改变”,我一次只移动一个字符,以灰色突出显示文本。

您将不得不根据您的用户界面(即您的富文本框的大小)更改“const int offset”的值。将其设置为控件垂直中点的字符数,即((RichTextBox 中的行数) / 2) * (每行的字符数)。我使用了一个 5 行文本框,每行大约 50 个字符,所以我将其设置为 100。

如果对您有帮助,请不要忘记接受答案。谢谢

    private void btnReset_Click(object sender, EventArgs e)
    {       
        richTextBox1.SelectAll();
        richTextBox1.SelectionBackColor = Color.White;
        richTextBox1.ScrollToCaret();
    }

    private void btnHighlight_Click(object sender, EventArgs e)
    {
        const int offset = 100;

        for (int i = 0; i < richTextBox1.TextLength; i++)
        {
            richTextBox1.Select(i, 1);
            richTextBox1.SelectionBackColor = Color.LightGray;

            if (i - offset > 0)
                richTextBox1.Select(i - offset, 1);
            else
                richTextBox1.Select(0, 1);

            richTextBox1.ScrollToCaret();

            Application.DoEvents();

            System.Threading.Thread.Sleep(50);

        }

    }
于 2013-06-10T19:30:16.917 回答