Hide and Show the Scroll Bar in a Multiline Text Box

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as