How to know if RichTextBox vertical Scrollbar reached the max value?

血红的双手。 提交于 2019-12-01 13:18:36

You have to deal with a little Win32. The win32 method GetScrollInfo is what we need. With that we can get the maximum range, the current position of the thumb and the Page size (which is the thumb size). So we have this formula:

Max Position = Max Range - Thumb size

Now it's the code for you:

//Must add using System.Runtime.InteropServices;
//We can define some extension method for this purpose
public static class RichTextBoxExtension {
    [DllImport("user32")]
    private static extern int GetScrollInfo(IntPtr hwnd, int nBar, 
                                            ref SCROLLINFO scrollInfo);

    public struct SCROLLINFO {
      public int cbSize;
      public int fMask;
      public int min;
      public int max;
      public int nPage;
      public int nPos;
      public int nTrackPos;
    }
    public static bool ReachedBottom(this RichTextBox rtb){
       SCROLLINFO scrollInfo = new SCROLLINFO();
       scrollInfo.cbSize = Marshal.SizeOf(scrollInfo);
       //SIF_RANGE = 0x1, SIF_TRACKPOS = 0x10,  SIF_PAGE= 0x2
       scrollInfo.fMask = 0x10 | 0x1 | 0x2;
       GetScrollInfo(rtb.Handle, 1, ref scrollInfo);//nBar = 1 -> VScrollbar
       return scrollInfo.max == scrollInfo.nTrackPos + scrollInfo.nPage;
    }
}
//Usage:
if(!yourRichTextBox.ReachedBottom()){
   yourRichTextBox.ScrollToCaret();
   //...
}

This is the @King King version I've translated it to VB.NET and also added more functions, I thinkg all of them works right:

Public Class ScrollBarInfo

<System.Runtime.InteropServices.DllImport("user32")> _
Private Shared Function GetScrollInfo(hwnd As IntPtr, nBar As Integer, ByRef scrollInfo As SCROLLINFO) As Integer
End Function

Private Shared scrollInf As New SCROLLINFO()

Private Structure SCROLLINFO
    Public cbSize As Integer
    Public fMask As Integer
    Public min As Integer
    Public max As Integer
    Public nPage As Integer
    Public nPos As Integer
    Public nTrackPos As Integer
End Structure

Private Shared Sub Get_ScrollInfo(control As Control)
    scrollInf = New SCROLLINFO()
    scrollInf.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(scrollInf)
    scrollInf.fMask = &H10 Or &H1 Or &H2
    GetScrollInfo(control.Handle, 1, scrollInf)
End Sub

Public Shared Function ReachedBottom(control As Control) As Boolean
    Get_ScrollInfo(control)
    Return scrollInf.max = scrollInf.nTrackPos + scrollInf.nPage
End Function

Public Shared Function ReachedTop(control As Control) As Boolean
    Get_ScrollInfo(control)
    Return scrollInf.nTrackPos < 0
End Function

Public Shared Function IsAtBottom(control As Control) As Boolean
    Get_ScrollInfo(control)
    Return scrollInf.max = (scrollInf.nTrackPos + scrollInf.nPage) - 1
End Function

Public Shared Function IsAtTop(control As Control) As Boolean
    Get_ScrollInfo(control)
    Return scrollInf.nTrackPos = 0
End Function

End Class
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!