Hide and Show the Scroll Bar in a Multiline Text Box

Have you ever wanted to make that pesky scroll bar disappear when itis not needed, and reappear when the text exceeds the viewable area ofthe text box? Setting the scroll bar properties at run time won’t do it.You could use API calls, but how do you decide when the lines have exceededthe viewing area? Here’s a set of declarations and procedures that willdo just that for you:

 '=USER TYPES = Type pointapi x As Integer y As Integer End Type Type RECT '8 Bytes Left As Integer Top As Integer right As Integer bottom As Integer End Type Type TEXTMETRIC '31 Bytes TMHeight As Integer tmAscent As Integer tmDescent As Integer tmInternalLeading As Integer tmExternalLeading As Integer tmAveCharWidth As Integer tmMaxCharWidth As Integer tmWeight As Integer tmItalic As String * 1 tmUnderlined As String * 1 tmStruckOut As String * 1 tmFirstChar As String * 1 tmLastChar As String * 1 tmDefaultChar As String * 1 tmBreakChar As String * 1 tmPitchAndFamily As String * 1 tmCharSet As String * 1 tmOverhang As Integer tmDigitizedAspectX As Integer tmDigitizedAspectY As Integer End Type Global apnt As pointapi Declare Function SendMessage& Lib "User" _ (ByVal HWND%, ByVal wmsg%, ByVal wparam%, _ lparam As Any) Declare Function GetTextMetrics% Lib "GDI" _ (ByVal hDC%, lpMetrics As TEXTMETRIC) Declare Function GetDC% Lib "User" (ByVal HWND%) Declare Function SelectObject% Lib "GDI" _ (ByVal hDC%, ByVal hObject%) Declare Function ReleaseDC% Lib "User" _ (ByVal HWND%, ByVal hDC%) Declare Function WindowFromPoint% Lib "User" _ (ByVal x As Any) Declare Sub SHOWSCROLLBAR Lib "User" _ (ByVal HWND%, ByVal wbar%, ByVal bshow%) '==============API CONSTANTS==================== Global Const WM_USER = &H400 Global Const EM_SETREADONLY = (WM_USER + 31) Global Const EM_GETLINECOUNT = (WM_USER + 10) Global Const EM_GETFIRSTVISIBLELINE = _ (WM_USER + 30) Global Const EM_GETRECT = (WM_USER + 2) Global Const WM_GETFONT = &H31 

This sub is called from your form to make the scroll bars visibleor invisible, depending on whether the lines of text have exceeded theviewable area in the text box. You must pass it the hWnd of the text boxand the scroll bar type that the text box is using:

 Sub Scroll_show (thwnd%, bartype%) '============================================== 'This Subroutine controls the visibility of the ' scrollbars in the designated Edit Control. The ' requirements for this subroutine are: ' BarType%.......Integer selecting scrollbar type ' thwnd%.........Hwnd of the edit control ' API CONSTANTS ' EM_GETLINECOUNT ' EM_GETFIRSTVISIBLELINE ' !! MUST BE A TRUETYPE FONT IN USE !! '============================================== LinECount% = SendMessage(thwnd%, _ EM_GETLINECOUNT, 0, 0) FirstVisible% = SendMessage(thwnd%, _ EM_GETFIRSTVISIBLELINE, 0, 0) If LinECount% > GETVISIBLELINES(thwnd%) Then SHOWSCROLLBAR thwnd%, 1, True Else SHOWSCROLLBAR thwnd%, 1, False End If End Sub 

This function returns the number of visible lines in a text boxwhen you pass it the hWnd property of the text box:

 Function GETVISIBLELINES% (thwnd%) '============================================== 'This function returns the number of visible ' lines in an edit control. It requires: ' RC.........User Defined Type RECT ' TM.........User Defined Type TEXTMETRIC ' THWND%.....The hwnd of the edit control ' API FUNCTIONS: ' SendMessage() ' GetDC() ' SelectObject() ' ReleaseDC() ' API CONSTANTS: ' EM_GETRECT ' WM_GETFONT '============================================== Dim RC As RECT Dim hDC% Dim LFONT%, OLFFONT% Dim TM As TEXTMETRIC Dim DI% 'GET THE RECTANGLE LC% = SendMessage(thwnd%, EM_GETRECT, 0, RC) 'GET THE FONT HANDLE LFONT% = SendMessage(thwnd%, WM_GETFONT, 0, 0&) 'GET DEVICE CONTEXT hDC% = GetDC(thwnd%) 'SELECT LOGICAL FONT If LFONT% <> 0 Then OLDFONT% = SelectObject_ (hDC%, LFONT%) DI% = GetTextMetrics(hDC%, TM) If LFONT% <> 0 Then LFONT% = SelectObject_ (hDC%, LFONT%) 'GET VISIBLE LINES GETVISIBLELINES% = _ (RC.bottom - RC.Top) / TM.TMHeight DI% = ReleaseDC(thwnd%, hDC%) End Function 

This sub is the one that actually makes the scroll bars visibleor invisible, depending on the value of the flag variable. It requiresthe hWnd property of the control:

 Sub ListScrollShow (thwnd%, flag As Integer) '============================================== 'Makes the control's scrollbar visible or 'invisible depending on flag value. 'Flag = True to show 'FlAG = fALSE TO HIDE '============================================== SHOWSCROLLBAR thwnd%, 1, flag End Sub 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS