开发者

RichTextBox Vertical Scrollbar manipulation in visual studio

开发者 https://www.devze.com 2023-01-02 06:41 出处:网络
I\'ve searched through related questions but can\'t find what I need. I have a richtextbox control. I need to trigger an event when the vertical scrollbar reaches a certain position (say 90% down to

I've searched through related questions but can't find what I need.

I have a richtextbox control. I need to trigger an event when the vertical scrollbar reaches a certain position (say 90% down to the bottom). I've been playing around with the events for the rich textbox but have ye开发者_如何学运维t to find anything.

Any help would be greatly appreciated.


You can handle VScoll event to detect vertical scrolling and use function

private static double GetRichTextBoxScrolPos(RichTextBox textBox)
{
    if(textBox1.TextLength == 0) return 0;
    var p1 = textBox.GetPositionFromCharIndex(0);
    var p2 = textBox.GetPositionFromCharIndex(textBox.TextLength - 1);

    int scrollPos = -p1.Y;
    int maxScrolPos = p2.Y - p1.Y - textBox.ClientSize.Height;

    if(maxScrolPos <= 0) return 0;

    double d = 100.0 * (double)scrollPos / (double)maxScrolPos;
    if(d < 0) d = 0;
    else if(d > 100) d = 100;

    return d;
}

to determine scroll position. Result is in % (100% = fully scrolled to bottom).

Important note: This function is not absolutely accurate, but you may find result accurate enougth. It can be further improved by measuring bottom line height (using Graphics object for example). 100% reliable way is to aquire VScrollBar handle and query its position using WinAPI, but that will requre much more work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号