Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleShow a TextBox's vertical and horizontal scrollbars only when they are necessary
Keywordsscrollbar, scrollbars, show, hide, TextBox
CategoriesControls
 
At design time, set the TextBox's ScrollBars property to 1 - Horizontal. If you don't do this, then the TextBox wraps text and the horizontal scrollbar doesn't work properly when it is visible.

When the form loads, set a hidden PictureBox's Font property to match the TextBox's. Set m_NeededHScrollbar to True to indicate that the TextBox initially has its horizontal scrollbar displayed. Then call txtValue_Change to make the program determine which scrollbars are initially needed.

In the TextBox's Change event handler, use the PictureBox's TextHeight and TextWidth functions to see how big the text is. Use those values to determine whether the TextBox needs scrollbars. Note that displaying one scrollbar takes away room from the TextBox and may make the other scrolbar necessary.

If the need for either scrollbar has changed, use the ShowScrollBar API function to show or hide it.

 
Option Explicit

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

Private m_NeededVScrollbar As Boolean
Private m_NeededHScrollbar As Boolean

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

    ' Initially the horizontal scrollbar is visible.
    m_NeededHScrollbar = True

    ' Perform the initial startup check.
    txtValue_Change
End Sub

Private Sub txtValue_Change()
Const BORDER_SIZE As Single = 60
Const SCROLLBAR_WIDTH As Single = 255

Dim got_wid As Single
Dim got_hgt As Single
Dim need_wid As Single
Dim need_hgt As Single
Dim needs_vscrollbar As Boolean
Dim needs_hscrollbar As Boolean

    ' Get the control's width and height.
    got_wid = txtValue.Width - BORDER_SIZE
    got_hgt = txtValue.Height - BORDER_SIZE

    ' See how much room we need.
    need_wid = picHidden.TextWidth(txtValue.Text)
    need_hgt = picHidden.TextHeight(txtValue.Text)

    ' See if we need the vertical scrollbar.
    If need_hgt > got_hgt Then
        needs_vscrollbar = True
        got_wid = got_wid - SCROLLBAR_WIDTH
    End If

    ' See if we need the horizontal scrollbar.
    If need_wid > got_wid Then
        needs_hscrollbar = True
        got_hgt = got_hgt - SCROLLBAR_WIDTH

        ' See if we need the vertical scrollbar now.
        needs_vscrollbar = need_hgt > got_hgt
    End If

    ' See which scrollbars needs have changed.
    If m_NeededVScrollbar <> needs_vscrollbar Then
        m_NeededVScrollbar = needs_vscrollbar
        ShowScrollBar txtValue.hwnd, SB_VERT, _
            m_NeededVScrollbar
    End If
    If m_NeededHScrollbar <> needs_hscrollbar Then
        m_NeededHScrollbar = needs_hscrollbar
        ShowScrollBar txtValue.hwnd, SB_HORZ, _
            m_NeededHScrollbar
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated