none
Find out if the vertical scroll bar of a text box is at the beginning of the text box or the end RRS feed

  • Question

  • I have a wrapper class created around TextBox, and I want to find out if there is any way to test if the vertical scroll bar is at the beginning (the very top) of the text box and at the very end of the text box. Alternatively, I will be happy if only I can find out a way to check if a scroll bar is enabled or not in a textbox (note: it's not a RichTextbox).

    By enabled, I mean that scroll bar is actually enabled i.e. you can use the scroll bar to scroll the control, not just visible (I know we can check visibility by GetWindowLong)

    Thanks!

    Thursday, March 22, 2012 9:18 PM

Answers

  • Hi Babbup,

    Welcome to the MSDN Forum.

    Please refer to this code:

    Imports System.Runtime.InteropServices
    
    Public Class TestTextBoxForm
    
        <DllImport("user32.dll")> _
        Private Shared Function GetScrollPos(hWnd As IntPtr, nBar As Integer) As Integer
        End Function
    
        <DllImport("user32.dll", SetLastError:=True)> _
        Public Shared Function GetScrollInfo(hWnd As IntPtr, _
            <MarshalAs(UnmanagedType.I4)> fnBar As SBOrientation, _
            <MarshalAs(UnmanagedType.Struct)> ByRef lpsi As SCROLLINFO) As Integer
        End Function
    
        <Serializable(), StructLayout(LayoutKind.Sequential)> _
        Structure SCROLLINFO
            Public cbSize As UInteger
            <MarshalAs(UnmanagedType.U4)> Public fMask As ScrollInfoMask
            Public nMin As Integer
            Public nMax As Integer
            Public nPage As UInteger
            Public nPos As Integer
            Public nTrackPos As Integer
        End Structure
    
        Public Enum ScrollInfoMask As UInteger
            SIF_RANGE = &H1
            SIF_PAGE = &H2
            SIF_POS = &H4
            SIF_DISABLENOSCROLL = &H8
            SIF_TRACKPOS = &H10
            SIF_ALL = (SIF_RANGE Or SIF_PAGE Or SIF_POS Or SIF_TRACKPOS)
        End Enum
    
        Public Enum SBOrientation As UInteger
            SB_HORZ = 0
            SB_VERT = 1
            SB_CTL = 2
            SB_BOTH = 3
        End Enum
    
        Private Sub TestTextBoxForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.TextBox1.Text = "aaaaaa" & vbCrLf & "baaaaaaaa" & vbCrLf & "bbbbb" & vbCrLf & "bdddd" & vbCrLf & "bddd" & vbCrLf & "bddddddd" & vbCrLf & "b" & vbCrLf & "b"
            Me.TextBox1.Text &= Me.TextBox1.Text
            Me.TextBox1.Text &= Me.TextBox1.Text
            Me.TextBox1.Text &= Me.TextBox1.Text
            Me.TextBox1.Text &= Me.TextBox1.Text
            Me.TextBox1.Text &= Me.TextBox1.Text
    
            Me.TextBox1.ScrollBars = ScrollBars.Both
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Console.WriteLine(GetScrollPos(Me.TextBox1.Handle, SBOrientation.SB_VERT))
            Dim scinfo As SCROLLINFO
            scinfo.cbSize = Marshal.SizeOf(GetType(SCROLLINFO))
            scinfo.fMask = ScrollInfoMask.SIF_ALL
    
            GetScrollInfo(Me.TextBox1.Handle, SBOrientation.SB_VERT, scinfo)
    
            Dim total As Integer = scinfo.nMax - scinfo.nPage + 1
            Dim perc As Double = scinfo.nPos / total
    
            If perc = 1.0 Then
                Console.WriteLine("At the bottom")
            ElseIf perc = 0.0 Then
                Console.WriteLine("At the beginning")
            Else
                Console.WriteLine("At " & perc * 100 & "%")
            End If
        End Sub
    End Class

    Before you test this code, please drag and drop one button and one textbox in a form.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Proposed as answer by pradeep1210 Friday, March 23, 2012 10:07 AM
    • Marked as answer by BabbuP Friday, March 23, 2012 3:03 PM
    • Edited by Mike FengModerator Tuesday, March 27, 2012 9:51 AM Modify the code
    Friday, March 23, 2012 9:58 AM
    Moderator

All replies

  • Hi Babbup,

    Welcome to the MSDN Forum.

    Please refer to this code:

    Imports System.Runtime.InteropServices
    
    Public Class TestTextBoxForm
    
        <DllImport("user32.dll")> _
        Private Shared Function GetScrollPos(hWnd As IntPtr, nBar As Integer) As Integer
        End Function
    
        <DllImport("user32.dll", SetLastError:=True)> _
        Public Shared Function GetScrollInfo(hWnd As IntPtr, _
            <MarshalAs(UnmanagedType.I4)> fnBar As SBOrientation, _
            <MarshalAs(UnmanagedType.Struct)> ByRef lpsi As SCROLLINFO) As Integer
        End Function
    
        <Serializable(), StructLayout(LayoutKind.Sequential)> _
        Structure SCROLLINFO
            Public cbSize As UInteger
            <MarshalAs(UnmanagedType.U4)> Public fMask As ScrollInfoMask
            Public nMin As Integer
            Public nMax As Integer
            Public nPage As UInteger
            Public nPos As Integer
            Public nTrackPos As Integer
        End Structure
    
        Public Enum ScrollInfoMask As UInteger
            SIF_RANGE = &H1
            SIF_PAGE = &H2
            SIF_POS = &H4
            SIF_DISABLENOSCROLL = &H8
            SIF_TRACKPOS = &H10
            SIF_ALL = (SIF_RANGE Or SIF_PAGE Or SIF_POS Or SIF_TRACKPOS)
        End Enum
    
        Public Enum SBOrientation As UInteger
            SB_HORZ = 0
            SB_VERT = 1
            SB_CTL = 2
            SB_BOTH = 3
        End Enum
    
        Private Sub TestTextBoxForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.TextBox1.Text = "aaaaaa" & vbCrLf & "baaaaaaaa" & vbCrLf & "bbbbb" & vbCrLf & "bdddd" & vbCrLf & "bddd" & vbCrLf & "bddddddd" & vbCrLf & "b" & vbCrLf & "b"
            Me.TextBox1.Text &= Me.TextBox1.Text
            Me.TextBox1.Text &= Me.TextBox1.Text
            Me.TextBox1.Text &= Me.TextBox1.Text
            Me.TextBox1.Text &= Me.TextBox1.Text
            Me.TextBox1.Text &= Me.TextBox1.Text
    
            Me.TextBox1.ScrollBars = ScrollBars.Both
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Console.WriteLine(GetScrollPos(Me.TextBox1.Handle, SBOrientation.SB_VERT))
            Dim scinfo As SCROLLINFO
            scinfo.cbSize = Marshal.SizeOf(GetType(SCROLLINFO))
            scinfo.fMask = ScrollInfoMask.SIF_ALL
    
            GetScrollInfo(Me.TextBox1.Handle, SBOrientation.SB_VERT, scinfo)
    
            Dim total As Integer = scinfo.nMax - scinfo.nPage + 1
            Dim perc As Double = scinfo.nPos / total
    
            If perc = 1.0 Then
                Console.WriteLine("At the bottom")
            ElseIf perc = 0.0 Then
                Console.WriteLine("At the beginning")
            Else
                Console.WriteLine("At " & perc * 100 & "%")
            End If
        End Sub
    End Class

    Before you test this code, please drag and drop one button and one textbox in a form.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Proposed as answer by pradeep1210 Friday, March 23, 2012 10:07 AM
    • Marked as answer by BabbuP Friday, March 23, 2012 3:03 PM
    • Edited by Mike FengModerator Tuesday, March 27, 2012 9:51 AM Modify the code
    Friday, March 23, 2012 9:58 AM
    Moderator
  • Hi Mike,

    I just noticed that scinfo.nPage does not tell me that I am at the end of scroll bar or not, - I should have mentioned that my textbox is very big and does not fit into the screen. I would like to know if the scrollbar has reached the very end of text box or not. Can you please help me?

    Monday, March 26, 2012 2:07 PM
  • Hi Babbup,

    Did you see this:

            If scinfo.nPos = scinfo.nPage Then
                Console.WriteLine("At the bottom")
            ElseIf scinfo.nPos = 0 Then
                Console.WriteLine("At the beginning")
            Else
                Console.WriteLine("At " & scinfo.nPos / scinfo.nPage)
            End If

    Does it work for you?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 27, 2012 6:03 AM
    Moderator