3

I found this link WPF_Example, but it is written in WPF. I am not programming in WPF, I am doing this in Windows Forms, and have no real reason to want to embed a WPF RichTextBox into my app just to obtain the answer I need.

Is there not a way, using WindowsForms (NOT WPF), to figure out if the RichTextBox scroll-bar thumb is at the bottom of the scroll bar?

The purpose of this is to allow our users, who are viewing the chat in the RTF box, to scroll up, and when text is added, NOT SCROLL DOWN, if they are scrolled up. Think of how mIRC handles chat; if you are at the bottom of the chat box, text will auto scroll into view; if you move up even one line, text is added w/o having to scroll.

I need to replicate that. I did find this link here on SO: List_ViewScroll, but i am not sure if it applies in this case.

Any help would be greatly appriciated :)

RESOLUTION

Using this class, I was able to get it to work. Thank you very much to the person below who pointed it out, and clarified some bits of it:

internal class Scrollinfo
{
    public const uint ObjidVscroll = 0xFFFFFFFB;

    [DllImport("user32.dll", SetLastError = true, EntryPoint = "GetScrollBarInfo")]
    private static extern int GetScrollBarInfo(IntPtr hWnd,
                                               uint idObject,
                                               ref Scrollbarinfo psbi);

    internal static bool CheckBottom(RichTextBox rtb)
    {


        var info = new Scrollbarinfo();
        info.CbSize = Marshal.SizeOf(info);

        var res = GetScrollBarInfo(rtb.Handle,
                                   ObjidVscroll,
                                   ref info);

        var isAtBottom = info.XyThumbBottom > (info.RcScrollBar.Bottom - info.RcScrollBar.Top - (info.DxyLineButton*2));
        return isAtBottom;
    }
}

public struct Scrollbarinfo
{
    public int CbSize; 
    public Rect RcScrollBar;
    public int DxyLineButton;
    public int XyThumbTop;
    public int XyThumbBottom;
    public int Reserved;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
    public int[] Rgstate;
}

public struct Rect
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}       
5
  • Please don't answer your question in the question, but in your own answer.
    – CharlesB
    Oct 21, 2011 at 4:29
  • I always provide a resolution in my question. Sorry :( it's just what I do so others can see what was done. Sometimes the solutions do not always mirror what others answered. And answering your own question can be seen as....silly. So providing the detail here = good information for future searches. You can flag me if you want, i don't mind. But i do not feel that I'm in the wrong here. Thanks!
    – user674311
    Oct 21, 2011 at 5:45
  • It's not silly, it's the way stackoverflow works. Providing your solution as an answer to this question is more logic and if you mark it as accepted it is shown on top of answers. You can also get reputation if your answer gets upvoted.
    – CharlesB
    Oct 21, 2011 at 5:52
  • of course the only downfall to that, is that you loose reputation when u answer your own question (right now, it's -2 every time you do it) lol kinda annoying.
    – user674311
    Oct 21, 2011 at 18:05
  • answering your own question as an answer is how the site works, and how everyone use it, so you're free to act differently but it's' not how the community works.
    – CharlesB
    Oct 22, 2011 at 8:18

2 Answers 2

5

So, the answer to this question is not incredibly complicated, but it is fairly verbose. The key is the Win32 API function GetScrollBarInfo, which is fairly easy to call from C#. You'll need to the following definitions in your form to make the call...

[DllImport("user32.dll", SetLastError = true, EntryPoint = "GetScrollBarInfo")]
private static extern int GetScrollBarInfo(IntPtr hWnd,
    uint idObject, ref SCROLLBARINFO psbi);

public struct SCROLLBARINFO {
    public int cbSize;
    public RECT rcScrollBar;
    public int dxyLineButton;
    public int xyThumbTop;
    public int xyThumbBottom;
    public int reserved;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
    public int[] rgstate;
}

public struct RECT {
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}        

To test out GetScrollBarInfo, consider creating a form with a RichTextBox and a button. In the click event for the button, make the following call (assuming your RichTextBox is named "richTextBox1")...

uint OBJID_VSCROLL = 0xFFFFFFFB;

SCROLLBARINFO info = new SCROLLBARINFO();
info.cbSize = Marshal.SizeOf(info);

int res = GetScrollBarInfo(richTextBox1.Handle, OBJID_VSCROLL, ref info);
bool isAtBottom = info.xyThumbBottom >
    (info.rcScrollBar.Bottom - info.rcScrollBar.Top - 20);

After the call, a simple formula can determine whether or not the scroll bar thumb is at the bottom. Essentially, info.rcScrollBar.Bottom and info.rcScrollBar.Top are positions on the screen and the difference between them will tell you the size of the scroll bar no matter where it is on the screen. Meanwhile, info.xyThumbBottom marks the position of the bottom of the thumb button. The "20" is basically guess as to the size of the scroll bar's down arrow. You see, the bottom of the thumb button will never actually be all the way to the bottom of the scroll bar, (which is what the difference gives you) so you have to take off an additional amount for the down button. This is admittedly somewhat volatile given that the button's size will be different depending on users' configuration, but this should be enough to get you started.

6
  • I think it is an elegant solution, just finding the size of that little box may be an issue, given that i have to run this on other propels machines with varying DPI/resolutions. But, that is pretty much what was in the other post, just more tailored, so i'll use it. Thanks so much :)
    – user674311
    Oct 21, 2011 at 3:38
  • So i went ahead and adjusted it to use: var isAtBottom = info.XyThumbBottom > (info.RcScrollBar.Bottom - info.RcScrollBar.Top - (info.DxyLineButton*2)); and it seems to work quite well. Thank you very much
    – user674311
    Oct 21, 2011 at 4:05
  • Good catch. It would be interesting to see how well this works from one machine to the next. Also, it occurs to me that a good long term solution for reuse would be to create an extension method called IsScrolledDown. It would be fairly trivial to do both for RichTextBox objects as well as other scrollable controls. Oct 21, 2011 at 13:54
  • In my class above, i changed the internal static bool CheckBottom(RichTextBox rtb) to internal static bool CheckBottom(Control ctrl) and it seems to work for many items. Good idea, and so far, over 50 people are using it, and it seems to be working just fine. Thanks so much for the clarification on that :)
    – user674311
    Oct 21, 2011 at 17:47
  • Make that 51, I'm using it too. I thought of a use for it with something I'm working on. Oct 22, 2011 at 13:51
1

My friend (expert programmer) gave me this solution, which works perfectly (better than the solution offered years ago):

var isAtBottom = rt.GetPositionFromCharIndex(rt.Text.Length).Y < rt.Height;

2
  • This solution works and is much simpler than the accepted solution.
    – Renato
    Aug 18, 2020 at 3:37
  • This solution worked for me on a TextBox but it's actually rt.Text.Length - 1 Mar 15, 2022 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy