Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

Show a TextBox's vertical scrollbar only when it is necessary
 
.
Summary

C# Signature:

[DllImport("user32.dll")]

[return: MarshalAs(UnmanagedType.Bool)]

public static extern bool ShowScrollBar(IntPtr hWnd, int wBar, [MarshalAs(UnmanagedType.Bool)] bool bShow);

User-Defined Types:

None.

Notes:

For reference:

SB_HORZ = 0

SB_VERT = 1

SB_CTL = 2

SB_BOTH = 3

Tips & Tricks:

Please add some!

Sample Code:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

VB Signature:

Private Declare Function ShowScrollBar Lib "user32" (ByVal hwnd As Long, ByVal wBar As Long, ByVal bShow As Long) As Long

Tips & Tricks:

http://www.vb-helper.com/howto_show_hide_text_scrollbar.html

http://www.vb-helper.com/howto_show_hide_both_scrollbars.html

Sample Code:

Title
Show a TextBox's vertical scrollbar only when it is necessary
Keywords
Categories
Controls

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

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions