Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
 
 
 
 
TitleShow a TextBox's vertical scrollbar only when it is necessary
Keywordsscrollbar, show, hide, TextBox
CategoriesControls
 
When the form loads, set a hidden PictureBox's Font property to match the TextBox's.

In the TextBox's Change event handler, use the PictureBox's TextHeight function to see how tall the text is. If the text is too big to fit in the TextBox, we need to display the scrollbar. If the need for the scrollbar has changed, use the ShowScrollBar API function to show or hide it.

 
Private m_ScrollBarVisible As Boolean

Private Declare Function ShowScrollBar Lib "user32" (ByVal _
    hwnd As Long, ByVal wBar As Long, ByVal bShow As Long) _
    As Long
Private Const SB_VERT = 1

Private Sub Form_Load()
    ' Make picHidden use the TextBox's font.
    picHidden.Font = txtValue.Font

    ' Perform the initial startup check.
    txtValue_Change
End Sub

Private Sub txtValue_Change()
Dim needs_scrollbar As Boolean

    ' See if we need the scrollbar.
    needs_scrollbar = _
        picHidden.TextHeight(txtValue.Text) > _
        txtValue.Height - 60

    ' See if the need has changed.
    If needs_scrollbar <> m_ScrollBarVisible Then
        ' Show or hide the scrollbar.
        m_ScrollBarVisible = needs_scrollbar
        ShowScrollBar txtValue.hwnd, SB_VERT, _
            m_ScrollBarVisible
    End If
End Sub
 
 
Copyright © 1997-2001 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated